Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Node to 6.13.0 LTS + current lib versions #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
3 changes: 0 additions & 3 deletions 404.jade

This file was deleted.

122 changes: 38 additions & 84 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

var express = require('express')
, stylus = require('stylus')
, sio = require('socket.io')
, base60 = require('./base60')
, jadevu = require('jadevu')
, http = require('http')
, base60 = require('./lib/base60')
, morgan = require('morgan')
, bodyParser = require('body-parser')
, errorHandler = require('errorhandler')
, crypto = require('crypto')
, url = require('url')
, nib = require('nib')
Expand All @@ -19,13 +21,18 @@ var express = require('express')

var env = process.env.NODE_ENV || 'development';

const config = {
port: process.env.PORT || 3000,
domain: process.env.SHORTY_DOMAIN || 'https://lrn.cc',
redis_url: process.env.SHORTY_REDIS_URL || 'redis://localhost:6379',

}

/**
* Create db.
*/

redis = require('redis').createClient(
process.env.SHORTY_REDIS_URL
);
redis = require('redis').createClient(config.redis_url);

/**
* Redis lock.
Expand All @@ -39,32 +46,21 @@ var lock = require('redis-lock')(redis).bind(null, 'shorty-lock', 10000);
* Create app.
*/

app = module.exports = express.createServer();

var app = module.exports = express();
var server = http.createServer(app);
/**
* Basic middleware.
*/

if ('development' == env) {
app.use(express.logger('dev'));
}
if (process.env.SHORTY_BASIC_AUTH) {
app.use(express.basicAuth.apply(null, process.env.SHORTY_BASIC_AUTH.split(':')));
app.use(morgan('dev'));
}
app.use(express.bodyParser());

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(stylus.middleware({ src: __dirname + '/public/', compile: css }));
app.use(express.static(__dirname + '/public'));

/**
* Socket.IO
*/

var io = sio.listen(app);

// quiet :)

io.set('log level', 0);

/**
* Reads a file
*
Expand All @@ -91,27 +87,9 @@ function css (str, path) {
* Configure app.
*/

app.configure(function () {
app.set('views', __dirname);
app.set('view engine', 'jade');
app.set('domain', process.env.SHORTY_DOMAIN || 'https://lrn.cc');
});

/**
* Development configuration.
*/

app.configure('development', function () {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

/**
* Production configuration.
*/

app.configure('production', function () {
app.use(express.errorHandler());
});
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.set('domain', config.domain);

/**
* GET index page.
Expand Down Expand Up @@ -176,8 +154,6 @@ app.post('/', validate, exists, function (req, res, next) {
if (err) return next(500);

obj.parsed = parsed;
io.of('/main').volatile.emit('total', length + 1);
io.of('/stats').volatile.emit('url created', short, parsed, Date.now());
res.send({ short: app.set('domain') + '/' + short });

process.nextTick(unlock);
Expand Down Expand Up @@ -226,41 +202,14 @@ function exists (req, res, next) {
});
}

/**
* GET statistics.
*/

app.get('/stats', accept('html'), function (req, res, next) {
redis.lrange('transactions', 0, 100, function (err, vals) {
if (err) return next(err);
res.render('stats', { transactions: vals ? vals.map(function (v) {
v = JSON.parse(v);
v.parsed = url.parse(v.url);
delete v.url;
return v;
}).reverse() : [] });
});
});

/**
* GET JSON statistics.
*/

app.get('/stats', accept('json'), function (req, res, next) {
redis.lrange('transactions', 0, 100, function (err, vals) {
if (err) return next(err);
res.send(vals.map(JSON.parse));
});
});

/**
* GET :short url to perform redirect.
*/

app.get('/:short', function (req, res, next) {
redis.hget('urls', req.params.short, function (err, val) {
if (err) return next(err);
if (!val) return res.render('404');
if (!val) return res.status(404).render('404');

lock(function (unlock) {
redis.lpush('transactions', JSON.stringify({
Expand All @@ -274,28 +223,33 @@ app.get('/:short', function (req, res, next) {
if (err) console.error(err);
});

io.of('/stats').volatile.emit(
'url visited'
, req.params.short
, url.parse(val)
, Date.now()
);

res.redirect(val);

process.nextTick(unlock);
});
});
});


/**
* Erorr handlers
*/
if (env === 'development') {
app.use(errorHandler());

} else if (env === 'production') {
app.use(function (err, req, res, next) {
res.status(500).json({ status: 'error' });
})
}

/**
* Listen.
*/

if (!module.parent) {
app.listen(process.env.PORT || 3000, function () {
var addr = app.address();
console.error(' app listening on ' + addr.address + ':' + addr.port);
server.listen(config.port, function () {
console.error(' app listening on http://localhost:' + config.port);
});

process.on('uncaughtException', function (e) {
Expand Down
5 changes: 0 additions & 5 deletions index.jade

This file was deleted.

22 changes: 0 additions & 22 deletions layout.jade

This file was deleted.

File renamed without changes.
Loading