2015-05-04 03:53:29 -04:00
|
|
|
//app
|
|
|
|
//external modules
|
|
|
|
var express = require('express');
|
|
|
|
var toobusy = require('toobusy-js');
|
|
|
|
var ejs = require('ejs');
|
|
|
|
var passport = require('passport');
|
|
|
|
var methodOverride = require('method-override');
|
2015-06-01 06:04:25 -04:00
|
|
|
var cookieParser = require('cookie-parser');
|
2015-05-04 03:53:29 -04:00
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var compression = require('compression')
|
|
|
|
var session = require('express-session');
|
2016-04-20 06:03:55 -04:00
|
|
|
var SequelizeStore = require('connect-session-sequelize')(session.Store);
|
2015-05-15 00:58:13 -04:00
|
|
|
var fs = require('fs');
|
|
|
|
var imgur = require('imgur');
|
|
|
|
var formidable = require('formidable');
|
2015-06-01 06:04:25 -04:00
|
|
|
var morgan = require('morgan');
|
|
|
|
var passportSocketIo = require("passport.socketio");
|
2016-03-14 22:41:49 -04:00
|
|
|
var helmet = require('helmet');
|
2016-08-18 23:49:24 -04:00
|
|
|
var i18n = require('i18n');
|
2015-05-04 03:53:29 -04:00
|
|
|
|
|
|
|
//core
|
2016-04-20 06:03:55 -04:00
|
|
|
var config = require("./lib/config.js");
|
2015-06-01 06:04:25 -04:00
|
|
|
var logger = require("./lib/logger.js");
|
2015-05-04 03:53:29 -04:00
|
|
|
var auth = require("./lib/auth.js");
|
2016-10-10 08:51:46 -04:00
|
|
|
var history = require("./lib/history.js");
|
2015-05-04 03:53:29 -04:00
|
|
|
var response = require("./lib/response.js");
|
2016-04-20 06:03:55 -04:00
|
|
|
var models = require("./lib/models");
|
2015-05-04 03:53:29 -04:00
|
|
|
|
|
|
|
//server setup
|
2015-05-15 00:58:13 -04:00
|
|
|
if (config.usessl) {
|
|
|
|
var ca = (function () {
|
|
|
|
var i, len, results;
|
|
|
|
results = [];
|
|
|
|
for (i = 0, len = config.sslcapath.length; i < len; i++) {
|
|
|
|
results.push(fs.readFileSync(config.sslcapath[i], 'utf8'));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
})();
|
|
|
|
var options = {
|
|
|
|
key: fs.readFileSync(config.sslkeypath, 'utf8'),
|
|
|
|
cert: fs.readFileSync(config.sslcertpath, 'utf8'),
|
|
|
|
ca: ca,
|
2016-03-14 22:39:45 -04:00
|
|
|
dhparam: fs.readFileSync(config.dhparampath, 'utf8'),
|
2015-05-15 00:58:13 -04:00
|
|
|
requestCert: false,
|
|
|
|
rejectUnauthorized: false
|
|
|
|
};
|
|
|
|
var app = express();
|
|
|
|
var server = require('https').createServer(options, app);
|
|
|
|
} else {
|
|
|
|
var app = express();
|
|
|
|
var server = require('http').createServer(app);
|
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2015-06-01 06:04:25 -04:00
|
|
|
//logger
|
|
|
|
app.use(morgan('combined', {
|
|
|
|
"stream": logger.stream
|
|
|
|
}));
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2015-07-01 12:10:20 -04:00
|
|
|
//socket io
|
|
|
|
var io = require('socket.io')(server);
|
2016-11-17 23:18:29 -05:00
|
|
|
io.engine.ws = new (require('uws').Server)({
|
|
|
|
noServer: true,
|
|
|
|
perMessageDeflate: false
|
|
|
|
});
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
//others
|
|
|
|
var realtime = require("./lib/realtime.js");
|
|
|
|
|
2015-09-23 23:36:41 -04:00
|
|
|
//assign socket io to realtime
|
|
|
|
realtime.io = io;
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
//methodOverride
|
|
|
|
app.use(methodOverride('_method'));
|
|
|
|
|
|
|
|
// create application/json parser
|
2016-07-29 23:13:13 -04:00
|
|
|
var jsonParser = bodyParser.json({
|
|
|
|
limit: 1024 * 1024 * 10 // 10 mb
|
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
|
|
|
|
// create application/x-www-form-urlencoded parser
|
|
|
|
var urlencodedParser = bodyParser.urlencoded({
|
2016-07-29 23:13:13 -04:00
|
|
|
extended: false,
|
|
|
|
limit: 1024 * 1024 * 10 // 10 mb
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
|
2015-06-01 06:04:25 -04:00
|
|
|
//session store
|
2016-04-20 06:03:55 -04:00
|
|
|
var sessionStore = new SequelizeStore({
|
|
|
|
db: models.sequelize
|
|
|
|
});
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
//compression
|
|
|
|
app.use(compression());
|
|
|
|
|
2016-03-14 22:41:49 -04:00
|
|
|
// use hsts to tell https users stick to this
|
|
|
|
app.use(helmet.hsts({
|
|
|
|
maxAge: 31536000 * 1000, // 365 days
|
|
|
|
includeSubdomains: true,
|
|
|
|
preload: true
|
|
|
|
}));
|
|
|
|
|
2016-08-18 23:49:24 -04:00
|
|
|
i18n.configure({
|
2016-10-21 01:39:28 -04:00
|
|
|
locales: ['en', 'zh', 'fr', 'de', 'ja', 'es', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk', 'hi', 'sv'],
|
2016-08-18 23:49:24 -04:00
|
|
|
cookie: 'locale',
|
|
|
|
directory: __dirname + '/locales'
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(cookieParser());
|
|
|
|
|
|
|
|
app.use(i18n.init);
|
|
|
|
|
2016-04-20 06:14:28 -04:00
|
|
|
// routes without sessions
|
|
|
|
// static files
|
|
|
|
app.use('/', express.static(__dirname + '/public', { maxAge: config.staticcachetime }));
|
|
|
|
app.use('/vendor/', express.static(__dirname + '/bower_components', { maxAge: config.staticcachetime }));
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
//session
|
|
|
|
app.use(session({
|
|
|
|
name: config.sessionname,
|
|
|
|
secret: config.sessionsecret,
|
|
|
|
resave: false, //don't save session if unmodified
|
2015-12-30 00:34:32 -05:00
|
|
|
saveUninitialized: true, //always create session to ensure the origin
|
2016-07-05 04:06:18 -04:00
|
|
|
rolling: true, // reset maxAge on every response
|
2015-05-04 03:53:29 -04:00
|
|
|
cookie: {
|
2016-09-26 12:13:24 -04:00
|
|
|
maxAge: config.sessionlife
|
2015-05-04 03:53:29 -04:00
|
|
|
},
|
2015-06-01 06:04:25 -04:00
|
|
|
store: sessionStore
|
2015-05-04 03:53:29 -04:00
|
|
|
}));
|
|
|
|
|
2016-03-14 22:42:07 -04:00
|
|
|
// session resumption
|
|
|
|
var tlsSessionStore = {};
|
|
|
|
server.on('newSession', function (id, data, cb) {
|
|
|
|
tlsSessionStore[id.toString('hex')] = data;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
server.on('resumeSession', function (id, cb) {
|
|
|
|
cb(null, tlsSessionStore[id.toString('hex')] || null);
|
|
|
|
});
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
//middleware which blocks requests when we're too busy
|
|
|
|
app.use(function (req, res, next) {
|
|
|
|
if (toobusy()) {
|
|
|
|
response.errorServiceUnavailable(res);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//passport
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
|
|
|
|
//serialize and deserialize
|
|
|
|
passport.serializeUser(function (user, done) {
|
2016-04-20 06:03:55 -04:00
|
|
|
logger.info('serializeUser: ' + user.id);
|
|
|
|
return done(null, user.id);
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
passport.deserializeUser(function (id, done) {
|
2016-04-20 06:03:55 -04:00
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
logger.info('deserializeUser: ' + user.id);
|
|
|
|
return done(null, user);
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return done(err, null);
|
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
|
2016-04-20 06:19:11 -04:00
|
|
|
// redirect url with trailing slashes
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
if ("GET" == req.method && req.path.substr(-1) == '/' && req.path.length > 1) {
|
|
|
|
var query = req.url.slice(req.path.length);
|
|
|
|
res.redirect(301, config.serverurl + req.path.slice(0, -1) + query);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-20 06:14:28 -04:00
|
|
|
// routes need sessions
|
2015-05-04 03:53:29 -04:00
|
|
|
//template files
|
2016-08-18 23:31:23 -04:00
|
|
|
app.set('views', __dirname + '/public/views');
|
2015-05-04 03:53:29 -04:00
|
|
|
//set render engine
|
2016-08-18 23:31:23 -04:00
|
|
|
app.engine('ejs', ejs.renderFile);
|
|
|
|
//set view engine
|
|
|
|
app.set('view engine', 'ejs');
|
2015-05-04 03:53:29 -04:00
|
|
|
//get index
|
2015-09-22 00:06:13 -04:00
|
|
|
app.get("/", response.showIndex);
|
2015-12-30 00:33:36 -05:00
|
|
|
//get 403 forbidden
|
2016-04-20 06:03:55 -04:00
|
|
|
app.get("/403", function (req, res) {
|
2015-12-30 00:33:36 -05:00
|
|
|
response.errorForbidden(res);
|
|
|
|
});
|
|
|
|
//get 404 not found
|
2016-04-20 06:03:55 -04:00
|
|
|
app.get("/404", function (req, res) {
|
2015-12-30 00:33:36 -05:00
|
|
|
response.errorNotFound(res);
|
|
|
|
});
|
2016-04-20 06:03:55 -04:00
|
|
|
//get 500 internal error
|
|
|
|
app.get("/500", function (req, res) {
|
|
|
|
response.errorInternalError(res);
|
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
//get status
|
|
|
|
app.get("/status", function (req, res, next) {
|
|
|
|
realtime.getStatus(function (data) {
|
2016-09-18 04:23:56 -04:00
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
});
|
2016-08-18 23:31:23 -04:00
|
|
|
res.send(data);
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
});
|
2015-05-15 00:58:13 -04:00
|
|
|
//get status
|
|
|
|
app.get("/temp", function (req, res) {
|
|
|
|
var host = req.get('host');
|
|
|
|
if (config.alloworigin.indexOf(host) == -1)
|
|
|
|
response.errorForbidden(res);
|
|
|
|
else {
|
|
|
|
var tempid = req.query.tempid;
|
|
|
|
if (!tempid)
|
|
|
|
response.errorForbidden(res);
|
|
|
|
else {
|
2016-04-20 06:03:55 -04:00
|
|
|
models.Temp.findOne({
|
|
|
|
where: {
|
|
|
|
id: tempid
|
|
|
|
}
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (!temp)
|
|
|
|
response.errorNotFound(res);
|
2015-05-15 00:58:13 -04:00
|
|
|
else {
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.send({
|
|
|
|
temp: temp.data
|
|
|
|
});
|
2016-04-20 06:03:55 -04:00
|
|
|
temp.destroy().catch(function (err) {
|
2015-05-15 00:58:13 -04:00
|
|
|
if (err)
|
2015-07-01 12:10:20 -04:00
|
|
|
logger.error('remove temp failed: ' + err);
|
2015-05-15 00:58:13 -04:00
|
|
|
});
|
|
|
|
}
|
2016-04-20 06:03:55 -04:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return response.errorInternalError(res);
|
2015-05-15 00:58:13 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//post status
|
|
|
|
app.post("/temp", urlencodedParser, function (req, res) {
|
|
|
|
var host = req.get('host');
|
|
|
|
if (config.alloworigin.indexOf(host) == -1)
|
|
|
|
response.errorForbidden(res);
|
|
|
|
else {
|
|
|
|
var data = req.body.data;
|
2016-04-20 06:03:55 -04:00
|
|
|
if (!data)
|
2015-05-15 00:58:13 -04:00
|
|
|
response.errorForbidden(res);
|
|
|
|
else {
|
|
|
|
if (config.debug)
|
2015-07-01 12:10:20 -04:00
|
|
|
logger.info('SERVER received temp from [' + host + ']: ' + req.body.data);
|
2016-04-20 06:03:55 -04:00
|
|
|
models.Temp.create({
|
|
|
|
data: data
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (temp) {
|
2015-05-15 00:58:13 -04:00
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: temp.id
|
|
|
|
});
|
|
|
|
} else
|
|
|
|
response.errorInternalError(res);
|
2016-04-20 06:03:55 -04:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return response.errorInternalError(res);
|
2015-05-15 00:58:13 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-07-31 12:06:07 -04:00
|
|
|
|
|
|
|
function setReturnToFromReferer(req) {
|
|
|
|
var referer = req.get('referer');
|
|
|
|
if (!req.session) req.session = {};
|
|
|
|
req.session.returnTo = referer;
|
|
|
|
}
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
//facebook auth
|
2016-04-20 06:03:55 -04:00
|
|
|
if (config.facebook) {
|
2016-07-31 12:06:07 -04:00
|
|
|
app.get('/auth/facebook', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req);
|
|
|
|
passport.authenticate('facebook')(req, res, next);
|
|
|
|
});
|
2016-04-20 06:03:55 -04:00
|
|
|
//facebook auth callback
|
|
|
|
app.get('/auth/facebook/callback',
|
|
|
|
passport.authenticate('facebook', {
|
2016-07-31 12:06:07 -04:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
2016-07-08 01:37:41 -04:00
|
|
|
failureRedirect: config.serverurl + '/'
|
2016-07-31 12:06:07 -04:00
|
|
|
}));
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
//twitter auth
|
2016-04-20 06:03:55 -04:00
|
|
|
if (config.twitter) {
|
2016-07-31 12:06:07 -04:00
|
|
|
app.get('/auth/twitter', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req);
|
|
|
|
passport.authenticate('twitter')(req, res, next);
|
|
|
|
});
|
2016-04-20 06:03:55 -04:00
|
|
|
//twitter auth callback
|
|
|
|
app.get('/auth/twitter/callback',
|
|
|
|
passport.authenticate('twitter', {
|
2016-07-31 12:06:07 -04:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
2016-07-08 01:37:41 -04:00
|
|
|
failureRedirect: config.serverurl + '/'
|
2016-07-31 12:06:07 -04:00
|
|
|
}));
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
//github auth
|
2016-04-20 06:03:55 -04:00
|
|
|
if (config.github) {
|
2016-07-31 12:06:07 -04:00
|
|
|
app.get('/auth/github', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req);
|
|
|
|
passport.authenticate('github')(req, res, next);
|
|
|
|
});
|
2016-04-20 06:03:55 -04:00
|
|
|
//github auth callback
|
|
|
|
app.get('/auth/github/callback',
|
|
|
|
passport.authenticate('github', {
|
2016-07-31 12:06:07 -04:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
2016-07-08 01:37:41 -04:00
|
|
|
failureRedirect: config.serverurl + '/'
|
2016-07-31 12:06:07 -04:00
|
|
|
}));
|
2016-04-20 06:03:55 -04:00
|
|
|
//github callback actions
|
|
|
|
app.get('/auth/github/callback/:noteId/:action', response.githubActions);
|
|
|
|
}
|
2016-05-09 16:27:35 -04:00
|
|
|
//gitlab auth
|
|
|
|
if (config.gitlab) {
|
2016-07-31 12:06:07 -04:00
|
|
|
app.get('/auth/gitlab', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req);
|
|
|
|
passport.authenticate('gitlab')(req, res, next);
|
|
|
|
});
|
2016-05-09 16:27:35 -04:00
|
|
|
//gitlab auth callback
|
|
|
|
app.get('/auth/gitlab/callback',
|
|
|
|
passport.authenticate('gitlab', {
|
2016-07-31 12:06:07 -04:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
2016-07-08 01:37:41 -04:00
|
|
|
failureRedirect: config.serverurl + '/'
|
2016-07-31 12:06:07 -04:00
|
|
|
}));
|
2016-05-09 16:27:35 -04:00
|
|
|
//gitlab callback actions
|
2016-05-16 06:16:45 -04:00
|
|
|
app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions);
|
2016-05-09 16:27:35 -04:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
//dropbox auth
|
2016-04-20 06:03:55 -04:00
|
|
|
if (config.dropbox) {
|
2016-07-31 12:06:07 -04:00
|
|
|
app.get('/auth/dropbox', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req);
|
|
|
|
passport.authenticate('dropbox-oauth2')(req, res, next);
|
|
|
|
});
|
2016-04-20 06:03:55 -04:00
|
|
|
//dropbox auth callback
|
|
|
|
app.get('/auth/dropbox/callback',
|
|
|
|
passport.authenticate('dropbox-oauth2', {
|
2016-07-31 12:06:07 -04:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
2016-07-08 01:37:41 -04:00
|
|
|
failureRedirect: config.serverurl + '/'
|
2016-07-31 12:06:07 -04:00
|
|
|
}));
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2016-05-21 10:48:00 -04:00
|
|
|
//google auth
|
|
|
|
if (config.google) {
|
2016-07-31 12:06:07 -04:00
|
|
|
app.get('/auth/google', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req);
|
|
|
|
passport.authenticate('google', { scope: ['profile'] })(req, res, next);
|
|
|
|
});
|
2016-05-21 10:48:00 -04:00
|
|
|
//google auth callback
|
|
|
|
app.get('/auth/google/callback',
|
|
|
|
passport.authenticate('google', {
|
2016-07-31 12:06:07 -04:00
|
|
|
successReturnToOrRedirect: config.serverurl + '/',
|
2016-07-08 01:37:41 -04:00
|
|
|
failureRedirect: config.serverurl + '/'
|
2016-07-31 12:06:07 -04:00
|
|
|
}));
|
2016-05-21 10:48:00 -04:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
//logout
|
|
|
|
app.get('/logout', function (req, res) {
|
2016-01-31 16:41:10 -05:00
|
|
|
if (config.debug && req.isAuthenticated())
|
2016-04-20 06:03:55 -04:00
|
|
|
logger.info('user logout: ' + req.user.id);
|
2015-05-04 03:53:29 -04:00
|
|
|
req.logout();
|
2016-07-08 01:37:41 -04:00
|
|
|
res.redirect(config.serverurl + '/');
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
//get history
|
2016-10-10 08:51:46 -04:00
|
|
|
app.get('/history', history.historyGet);
|
2015-05-04 03:53:29 -04:00
|
|
|
//post history
|
2016-10-10 08:51:46 -04:00
|
|
|
app.post('/history', urlencodedParser, history.historyPost);
|
2016-10-10 08:52:09 -04:00
|
|
|
//post history by note id
|
|
|
|
app.post('/history/:noteId', urlencodedParser, history.historyPost);
|
|
|
|
//delete history
|
|
|
|
app.delete('/history', history.historyDelete);
|
|
|
|
//delete history by note id
|
|
|
|
app.delete('/history/:noteId', history.historyDelete);
|
2015-05-04 03:53:29 -04:00
|
|
|
//get me info
|
|
|
|
app.get('/me', function (req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
2016-04-20 06:03:55 -04:00
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2016-04-20 06:03:55 -04:00
|
|
|
}).then(function (user) {
|
|
|
|
if (!user)
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
var profile = models.User.parseProfile(user.profile);
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: req.user.id,
|
|
|
|
name: profile.name,
|
|
|
|
photo: profile.photo
|
|
|
|
});
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('read me failed: ' + err);
|
|
|
|
return response.errorInternalError(res);
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.send({
|
|
|
|
status: 'forbidden'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-05-15 00:58:13 -04:00
|
|
|
//upload to imgur
|
|
|
|
app.post('/uploadimage', function (req, res) {
|
|
|
|
var form = new formidable.IncomingForm();
|
|
|
|
form.parse(req, function (err, fields, files) {
|
|
|
|
if (err || !files.image || !files.image.path) {
|
|
|
|
response.errorForbidden(res);
|
|
|
|
} else {
|
|
|
|
if (config.debug)
|
2015-07-01 12:10:20 -04:00
|
|
|
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
|
2015-05-15 00:58:13 -04:00
|
|
|
imgur.setClientId(config.imgur.clientID);
|
2015-07-01 12:10:20 -04:00
|
|
|
try {
|
|
|
|
imgur.uploadFile(files.image.path)
|
|
|
|
.then(function (json) {
|
|
|
|
if (config.debug)
|
|
|
|
logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
|
|
|
|
res.send({
|
2016-05-07 13:54:57 -04:00
|
|
|
link: json.data.link.replace(/^http:\/\//i, 'https://')
|
2015-07-01 12:10:20 -04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error(err);
|
2016-08-18 23:31:23 -04:00
|
|
|
return res.status(500).end('upload image error');
|
2015-06-01 06:04:25 -04:00
|
|
|
});
|
2015-07-01 12:10:20 -04:00
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
2016-08-18 23:31:23 -04:00
|
|
|
return res.status(500).end('upload image error');
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2015-05-15 00:58:13 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
//get new note
|
|
|
|
app.get("/new", response.newNote);
|
2015-07-06 01:51:55 -04:00
|
|
|
//get publish note
|
|
|
|
app.get("/s/:shortid", response.showPublishNote);
|
|
|
|
//publish note actions
|
|
|
|
app.get("/s/:shortid/:action", response.publishNoteActions);
|
2015-12-18 10:40:52 -05:00
|
|
|
//get publish slide
|
2015-11-23 07:38:26 -05:00
|
|
|
app.get("/p/:shortid", response.showPublishSlide);
|
2016-08-14 23:25:27 -04:00
|
|
|
//publish slide actions
|
|
|
|
app.get("/p/:shortid/:action", response.publishSlideActions);
|
2015-05-04 03:53:29 -04:00
|
|
|
//get note by id
|
|
|
|
app.get("/:noteId", response.showNote);
|
|
|
|
//note actions
|
|
|
|
app.get("/:noteId/:action", response.noteActions);
|
2016-06-17 04:11:14 -04:00
|
|
|
//note actions with action id
|
|
|
|
app.get("/:noteId/:action/:actionId", response.noteActions);
|
2016-04-20 06:19:29 -04:00
|
|
|
// response not found if no any route matches
|
|
|
|
app.get('*', function (req, res) {
|
|
|
|
response.errorNotFound(res);
|
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
|
|
|
|
//socket.io secure
|
|
|
|
io.use(realtime.secure);
|
2015-06-01 06:04:25 -04:00
|
|
|
//socket.io auth
|
|
|
|
io.use(passportSocketIo.authorize({
|
|
|
|
cookieParser: cookieParser,
|
|
|
|
key: config.sessionname,
|
|
|
|
secret: config.sessionsecret,
|
|
|
|
store: sessionStore,
|
|
|
|
success: realtime.onAuthorizeSuccess,
|
|
|
|
fail: realtime.onAuthorizeFail
|
|
|
|
}));
|
2015-05-04 03:53:29 -04:00
|
|
|
//socket.io heartbeat
|
|
|
|
io.set('heartbeat interval', config.heartbeatinterval);
|
|
|
|
io.set('heartbeat timeout', config.heartbeattimeout);
|
|
|
|
//socket.io connection
|
|
|
|
io.sockets.on('connection', realtime.connection);
|
|
|
|
|
|
|
|
//listen
|
2016-04-20 06:03:55 -04:00
|
|
|
function startListen() {
|
2016-09-06 07:37:05 -04:00
|
|
|
server.listen(config.port, function () {
|
|
|
|
var schema = config.usessl ? 'HTTPS' : 'HTTP';
|
|
|
|
logger.info('%s Server listening at port %d', schema, config.port);
|
|
|
|
config.maintenance = false;
|
|
|
|
});
|
2015-07-11 00:44:16 -04:00
|
|
|
}
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
// sync db then start listen
|
2016-06-17 04:09:33 -04:00
|
|
|
models.sequelize.sync().then(function () {
|
|
|
|
// check if realtime is ready
|
2016-10-10 08:51:46 -04:00
|
|
|
if (history.isReady() && realtime.isReady()) {
|
2016-06-17 04:09:33 -04:00
|
|
|
models.Revision.checkAllNotesRevision(function (err, notes) {
|
2016-10-10 08:56:41 -04:00
|
|
|
if (err) throw new Error(err);
|
2016-09-18 04:50:20 -04:00
|
|
|
if (!notes || notes.length <= 0) return startListen();
|
2016-06-17 04:09:33 -04:00
|
|
|
});
|
2016-11-07 08:31:11 -05:00
|
|
|
} else {
|
|
|
|
throw new Error('server still not ready after db synced');
|
2016-06-17 04:09:33 -04:00
|
|
|
}
|
|
|
|
});
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
// log uncaught exception
|
2015-07-11 00:44:16 -04:00
|
|
|
process.on('uncaughtException', function (err) {
|
2016-05-09 21:37:41 -04:00
|
|
|
logger.error('An uncaught exception has occured.');
|
2015-07-11 00:44:16 -04:00
|
|
|
logger.error(err);
|
2016-05-09 21:37:41 -04:00
|
|
|
logger.error('Process will exit now.');
|
|
|
|
process.exit(1);
|
2016-06-01 02:18:54 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// gracefully exit
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
config.maintenance = true;
|
|
|
|
// disconnect all socket.io clients
|
|
|
|
Object.keys(io.sockets.sockets).forEach(function (key) {
|
|
|
|
var socket = io.sockets.sockets[key];
|
|
|
|
// notify client server going into maintenance status
|
2016-06-17 04:09:33 -04:00
|
|
|
socket.emit('maintenance');
|
2016-10-21 01:35:29 -04:00
|
|
|
setTimeout(function () {
|
|
|
|
socket.disconnect(true);
|
|
|
|
}, 0);
|
2016-06-01 02:18:54 -04:00
|
|
|
});
|
|
|
|
var checkCleanTimer = setInterval(function () {
|
2016-10-10 08:51:46 -04:00
|
|
|
if (history.isReady() && realtime.isReady()) {
|
2016-06-17 04:09:33 -04:00
|
|
|
models.Revision.checkAllNotesRevision(function (err, notes) {
|
2016-10-10 08:56:41 -04:00
|
|
|
if (err) throw new Error(err);
|
2016-10-12 05:47:25 -04:00
|
|
|
if (!notes || notes.length <= 0) {
|
2016-06-17 04:09:33 -04:00
|
|
|
clearInterval(checkCleanTimer);
|
|
|
|
return process.exit(0);
|
|
|
|
}
|
|
|
|
});
|
2016-06-01 02:18:54 -04:00
|
|
|
}
|
|
|
|
}, 100);
|
2016-07-08 01:37:41 -04:00
|
|
|
});
|