This commit is contained in:
Martin Donnelly 2017-09-05 15:53:07 +01:00
parent eb0b076fed
commit d4bab117da
6 changed files with 159 additions and 115 deletions

View File

@ -1,24 +1,46 @@
{
"plugins": [
"react"
],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"es6": true,
"browser": true,
"node": true,
"mocha": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"rules": {
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": false
}
}
},
"env": {
"browser": true,
"node": true,
"es6": true
},
"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"]
}
}

92
app.js
View File

@ -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,88 +40,91 @@ 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){
const longUrl = req.body.url;
let shortUrl = '';
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){
// check if url already exists in database
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({
long_url: longUrl
});
const newUrl = Url({
long_url: longUrl
});
// save the new link
newUrl.save(function (err) {
if (err)
logger.error(err);
// save the new link
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) {
logger.debug(doc);
}
})
function getList(req, res) {
Url.find({}, (err, doc) => {
if (doc)
logger.debug(doc);
});
}
});
function getEncodedID(req, res) {
app.get('/:encoded_id', function(req, res){
const base58Id = req.params.encoded_id;
const base58Id = req.params.encoded_id;
const id = base58.decode(base58Id);
const id = base58.decode(base58Id);
// check if url already exists in database
Url.findOneAndUpdate({_id: id}, {$inc:{visits:1}}, function (err, doc){
// check if url already exists in database
Url.findOneAndUpdate({ _id: id }, { $inc: { visits: 1 } }, (err, doc) => {
if (doc) {
logger.debug(`Redirect: ${doc.long_url}`);
logger.debug(`Redirect: ${doc.long_url}`);
res.redirect(doc.long_url);
} else {
} else
res.redirect(config.webhost);
}
});
});
}
const server = app.listen(config.port, function () {
logger.info(`Server listening on port ${config.port}`);
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}`);
});

View File

@ -20,50 +20,50 @@ 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”
properties: true, // Optimize property access: a["foo"] → a.foo
dead_code: true, // Discard unreachable code
drop_debugger: true, // Discard “debugger” statements
unsafe: false, // Some unsafe optimizations (see below)
conditionals: true, // Optimize if-s and conditional expressions
comparisons: true, // Optimize comparisons
evaluate: true, // Evaluate constant expressions
booleans: true, // Optimize boolean expressions
loops: true, // Optimize loops
unused: true, // Drop unused variables/functions
hoist_funs: true, // Hoist function declarations
hoist_vars: false, // Hoist variable declarations
if_return: true, // Optimize if-s followed by return/continue
join_vars: true, // Join var declarations
cascade: true, // Try to cascade `right` into `left` in sequences
side_effects: true, // Drop side-effect-free statements
warnings: true, // Warn about potentially dangerous optimizations/code
global_defs: {} // global definitions
}}))
.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
unsafe: false, // Some unsafe optimizations (see below)
conditionals: true, // Optimize if-s and conditional expressions
comparisons: true, // Optimize comparisons
evaluate: true, // Evaluate constant expressions
booleans: true, // Optimize boolean expressions
loops: true, // Optimize loops
unused: true, // Drop unused variables/functions
hoist_funs: true, // Hoist function declarations
hoist_vars: false, // Hoist variable declarations
if_return: true, // Optimize if-s followed by return/continue
join_vars: true, // Join var declarations
cascade: true, // Try to cascade `right` into `left` in sequences
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'));
});
gulp.task('vendor', function() {
return gulp.src([
'bower_components/jquery/dist/jquery.min.js',
'bower_components/mui/packages/cdn/js/mui.min.js'
])
return gulp.src([
'bower_components/jquery/dist/jquery.min.js',
'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'])
return gulp.src(['public/css/roboto.css', 'bower_components/mui/packages/cdn/css/mui.min.css', 'public/css/styles.css'])
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(cssnano())
.pipe(concat('app.css'))
.pipe(gulp.dest('live/css'));
});
});
gulp.task('migrate', function() {
@ -73,7 +73,7 @@ gulp.task('migrate', function() {
gulp.task('index', function() {
return gulp.src(['views/index.html'])
return gulp.src(['views/index.html'])
.pipe(htmlreplace({
mui: 'css/mui.min.css',
css: 'css/app.css',
@ -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');
});

View File

@ -2,28 +2,28 @@ 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},
long_url: String,
created_at: Date,
visits: {type: Number, default: 0}
_id: { type: Number, index: true },
long_url: String,
created_at: Date,
visits: { type: Number, default: 0 }
});
urlSchema.pre('save', function(next){
const doc = this;
counter.findByIdAndUpdate({_id: 'url_count'}, {$inc: {seq: 1} }, function(error, counter) {
if (error)
return next(error);
doc.created_at = new Date();
doc._id = counter.seq;
next();
urlSchema.pre('save', function(next) {
const doc = this;
counter.findByIdAndUpdate({ _id: 'url_count' }, { $inc: { seq: 1 } }, function(error, counter) {
if (error)
return next(error);
doc.created_at = new Date();
doc._id = counter.seq;
next();
});
});

View File

@ -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;
}

View File

@ -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">