tidying
This commit is contained in:
parent
eb0b076fed
commit
d4bab117da
@ -1,24 +1,46 @@
|
||||
{
|
||||
"plugins": [
|
||||
"react"
|
||||
],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 6,
|
||||
"sourceType": "module",
|
||||
"ecmaFeatures": {
|
||||
"jsx": true
|
||||
"jsx": false
|
||||
}
|
||||
},
|
||||
"env": {
|
||||
"es6": true,
|
||||
"browser": true,
|
||||
"node": true,
|
||||
"mocha": true
|
||||
"es6": true
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:react/recommended"
|
||||
],
|
||||
"rules": {
|
||||
"no-new-object": 1,
|
||||
"no-reserved-keys": 1,
|
||||
"no-array-constructor": 1,
|
||||
"quotes": [1, "single"],
|
||||
"max-len": [1, 120, 2], // 2 spaces per tab, max 80 chars per line
|
||||
"no-inner-declarations": [1, "both"],
|
||||
"no-shadow-restricted-names": 1,
|
||||
"one-var": 0,
|
||||
"vars-on-top": 1,
|
||||
"eqeqeq": 1,
|
||||
"curly": [1, "multi"],
|
||||
"no-mixed-spaces-and-tabs": 1,
|
||||
"space-before-blocks": [1, "always"],
|
||||
"space-infix-ops": 1,
|
||||
"eol-last": 1,
|
||||
"comma-style": [1, "last"],
|
||||
"no-comma-dangle": 1,
|
||||
"semi": [1, "always"],
|
||||
"radix": 1,
|
||||
"camelcase": 1,
|
||||
"new-cap": 1,
|
||||
"consistent-this": [1, "_this"],
|
||||
"func-names": 1,
|
||||
"no-multi-spaces": 2,
|
||||
"brace-style": [2,"1tbs",{}],
|
||||
|
||||
"indent": [2,2],
|
||||
"comma-spacing": ["error", { "before": false, "after": true }],
|
||||
"object-curly-spacing": ["error", "always"]
|
||||
}
|
||||
|
||||
}
|
64
app.js
64
app.js
@ -25,7 +25,8 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'dev';
|
||||
if (process.env.NODE_ENV === 'prod') {
|
||||
isProduction = true;
|
||||
sitePath = 'live';
|
||||
indexView = 'dist/index.html'
|
||||
indexView = 'dist/index.html';
|
||||
config.webhost = 'http://nurl.co/';
|
||||
}
|
||||
|
||||
logger.warn(`isProduction:${isProduction}`);
|
||||
@ -39,39 +40,39 @@ app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(express.static(path.join(__dirname, sitePath)));
|
||||
app.use(favicon(__dirname + '/live/favicon-16x16.png'));
|
||||
|
||||
app.all('/*', function(req, res, next) {
|
||||
app.all('/*', (req, res, next) => {
|
||||
// CORS headers
|
||||
res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain
|
||||
res.header('Access-Control-Allow-Origin', '*'); // restrict it to the required domain
|
||||
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
|
||||
// Set custom headers for CORS
|
||||
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
|
||||
if (req.method == 'OPTIONS') {
|
||||
if (req.method === 'OPTIONS')
|
||||
res.status(200).end();
|
||||
} else {
|
||||
else
|
||||
next();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
app.get('/', function(req, res){
|
||||
app.get('/', function(req, res) {
|
||||
res.sendFile(path.join(__dirname, indexView));
|
||||
});
|
||||
|
||||
app.get('/admin/list', function(req, res){
|
||||
app.get('/admin/list', function(req, res) {
|
||||
res.sendFile(path.join(__dirname, listView));
|
||||
});
|
||||
|
||||
app.post('/api/v1/shorten', function(req, res){
|
||||
function postShort(req, res) {
|
||||
const longUrl = req.body.url;
|
||||
let shortUrl = '';
|
||||
|
||||
// check if url already exists in database
|
||||
Url.findOne({long_url: longUrl}, function (err, doc){
|
||||
Url.findOne({ long_url: longUrl }, (err, doc) => {
|
||||
|
||||
if (doc){
|
||||
if (doc) {
|
||||
shortUrl = config.webhost + base58.encode(doc._id);
|
||||
|
||||
// the document exists, so we return it without creating a new entry
|
||||
res.send({'shortUrl': shortUrl});
|
||||
res.send({ 'shortUrl': shortUrl });
|
||||
} else {
|
||||
// since it doesn't exist, let's go ahead and create it:
|
||||
const newUrl = Url({
|
||||
@ -79,48 +80,51 @@ app.post('/api/v1/shorten', function(req, res){
|
||||
});
|
||||
|
||||
// save the new link
|
||||
newUrl.save(function(err) {
|
||||
if (err){
|
||||
newUrl.save(function (err) {
|
||||
if (err)
|
||||
logger.error(err);
|
||||
}
|
||||
|
||||
|
||||
shortUrl = config.webhost + base58.encode(newUrl._id);
|
||||
|
||||
res.send({'shortUrl': shortUrl});
|
||||
res.send({ 'shortUrl': shortUrl });
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
app.get('/api/v1/list', function(req, res){
|
||||
Url.find({}, function(err, doc) {
|
||||
if (doc) {
|
||||
function getList(req, res) {
|
||||
Url.find({}, (err, doc) => {
|
||||
if (doc)
|
||||
logger.debug(doc);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
app.get('/:encoded_id', function(req, res){
|
||||
function getEncodedID(req, res) {
|
||||
|
||||
const base58Id = req.params.encoded_id;
|
||||
|
||||
const id = base58.decode(base58Id);
|
||||
|
||||
// check if url already exists in database
|
||||
Url.findOneAndUpdate({_id: id}, {$inc:{visits:1}}, function (err, doc){
|
||||
Url.findOneAndUpdate({ _id: id }, { $inc: { visits: 1 } }, (err, doc) => {
|
||||
if (doc) {
|
||||
logger.debug(`Redirect: ${doc.long_url}`);
|
||||
res.redirect(doc.long_url);
|
||||
} else {
|
||||
} else
|
||||
res.redirect(config.webhost);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
const server = app.listen(config.port, function () {
|
||||
app.post('/api/v1/shorten', postShort);
|
||||
app.get('/api/v1/list', getList);
|
||||
app.get('/:encoded_id', getEncodedID);
|
||||
|
||||
|
||||
const server = app.listen(config.port, () => {
|
||||
logger.info(`Server listening on port ${config.port}`);
|
||||
});
|
||||
|
20
gulpfile.js
20
gulpfile.js
@ -20,9 +20,9 @@ gulp.task('appJS', function() {
|
||||
.pipe(stripDebug())
|
||||
.pipe(jshint('.jshintrc'))
|
||||
.pipe(jshint.reporter('default'))
|
||||
.pipe(babel({presets: ['es2015']}))
|
||||
.pipe(babel({ presets: ['es2015'] }))
|
||||
.pipe(concat('nurl.js'))
|
||||
.pipe(uglify({mangle: true, compress: {sequences: true, // Join consecutive statemets with the “comma operator”
|
||||
.pipe(uglify({ mangle: true, compress: { sequences: true, // Join consecutive statemets with the “comma operator”
|
||||
properties: true, // Optimize property access: a["foo"] → a.foo
|
||||
dead_code: true, // Discard unreachable code
|
||||
drop_debugger: true, // Discard “debugger” statements
|
||||
@ -41,7 +41,7 @@ gulp.task('appJS', function() {
|
||||
side_effects: true, // Drop side-effect-free statements
|
||||
warnings: true, // Warn about potentially dangerous optimizations/code
|
||||
global_defs: {} // global definitions
|
||||
}}))
|
||||
} }))
|
||||
// .pipe(update(atob('LyogPT09PT09PQ0KDQpEZXZlbG9wZWQgYnkgTWFydGluIERvbm5lbGx5IG1hcnRpbmQyMDAwe2F0fWdtYWlsLmNvbQ0KDQo9PT09PT09ICovDQoNCg==')))
|
||||
.pipe(gulp.dest('live/js'));
|
||||
});
|
||||
@ -53,9 +53,9 @@ gulp.task('vendor', function() {
|
||||
'bower_components/mui/packages/cdn/js/mui.min.js'
|
||||
])
|
||||
.pipe(concat('vendor.js'))
|
||||
.pipe(uglify({mangle: false}))
|
||||
.pipe(uglify({ mangle: false }))
|
||||
.pipe(gulp.dest('live/js'));
|
||||
});
|
||||
});
|
||||
|
||||
gulp.task('styles', function() {
|
||||
return gulp.src(['public/css/roboto.css', 'bower_components/mui/packages/cdn/css/mui.min.css', 'public/css/styles.css'])
|
||||
@ -63,7 +63,7 @@ gulp.task('styles', function() {
|
||||
.pipe(cssnano())
|
||||
.pipe(concat('app.css'))
|
||||
.pipe(gulp.dest('live/css'));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
gulp.task('migrate', function() {
|
||||
@ -81,10 +81,10 @@ gulp.task('index', function() {
|
||||
vendor: 'js/vendor.js'
|
||||
|
||||
}))
|
||||
.pipe(htmlmin({removeComments: true, collapseWhitespace: true, keepClosingSlash: true}))
|
||||
.pipe(htmlmin({ removeComments: true, collapseWhitespace: true, keepClosingSlash: true }))
|
||||
|
||||
.pipe(gulp.dest('dist/'));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@ -95,12 +95,12 @@ gulp.task('clean', function() {
|
||||
|
||||
|
||||
gulp.task('buildJS', function() {
|
||||
gulp.start('appJS','vendor');
|
||||
gulp.start('appJS', 'vendor');
|
||||
});
|
||||
|
||||
|
||||
|
||||
gulp.task('default', ['clean'], function() {
|
||||
//gulp.start('buildJS','styles','index');
|
||||
gulp.start('buildJS','styles', 'index', 'migrate');
|
||||
gulp.start('buildJS', 'styles', 'index', 'migrate');
|
||||
});
|
||||
|
@ -2,23 +2,23 @@ const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const CounterSchema = Schema({
|
||||
_id: {type: String, required: true},
|
||||
seq: {type: Number, default: 1000}
|
||||
_id: { type: String, required: true },
|
||||
seq: { type: Number, default: 1000 }
|
||||
});
|
||||
|
||||
const counter = mongoose.model('counter', CounterSchema);
|
||||
|
||||
// create a schema for our links
|
||||
const urlSchema = new Schema({
|
||||
_id: {type: Number, index: true},
|
||||
_id: { type: Number, index: true },
|
||||
long_url: String,
|
||||
created_at: Date,
|
||||
visits: {type: Number, default: 0}
|
||||
visits: { type: Number, default: 0 }
|
||||
});
|
||||
|
||||
urlSchema.pre('save', function(next){
|
||||
urlSchema.pre('save', function(next) {
|
||||
const doc = this;
|
||||
counter.findByIdAndUpdate({_id: 'url_count'}, {$inc: {seq: 1} }, function(error, counter) {
|
||||
counter.findByIdAndUpdate({ _id: 'url_count' }, { $inc: { seq: 1 } }, function(error, counter) {
|
||||
if (error)
|
||||
return next(error);
|
||||
doc.created_at = new Date();
|
||||
|
@ -85,3 +85,21 @@ body {
|
||||
width: 700px;
|
||||
}
|
||||
}
|
||||
|
||||
body, html{
|
||||
font-family: "Roboto", "Arial", sans-serif;
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
}
|
||||
|
||||
h1{
|
||||
font-family: "Roboto Slab", monospace;
|
||||
font-weight: 700;
|
||||
font-style: normal;
|
||||
}
|
||||
|
||||
h2, h3, h4{
|
||||
font-family: "Ubuntu", sans-serif;
|
||||
font-weight: 500;
|
||||
font-style: normal;
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<title>nURL</title>
|
||||
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:700-Ubuntu:500-Roboto:regular" rel="stylesheet">
|
||||
<!-- build:css-->
|
||||
<link rel="stylesheet" href="//cdn.muicss.com/mui-0.9.20/css/mui.min.css">
|
||||
<link href="css/styles.css" rel="stylesheet">
|
||||
|
Loading…
Reference in New Issue
Block a user