codimd/app.js

408 lines
12 KiB
JavaScript
Raw Normal View History

2015-05-04 07:53:29 +00: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 10:04:25 +00:00
var cookieParser = require('cookie-parser');
2015-05-04 07:53:29 +00:00
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var compression = require('compression')
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
2015-05-15 04:58:13 +00:00
var fs = require('fs');
var shortid = require('shortid');
var imgur = require('imgur');
var formidable = require('formidable');
2015-06-01 10:04:25 +00:00
var morgan = require('morgan');
var passportSocketIo = require("passport.socketio");
2015-05-04 07:53:29 +00:00
//core
var config = require("./config.js");
2015-06-01 10:04:25 +00:00
var logger = require("./lib/logger.js");
2015-05-04 07:53:29 +00:00
var User = require("./lib/user.js");
2015-05-15 04:58:13 +00:00
var Temp = require("./lib/temp.js");
2015-05-04 07:53:29 +00:00
var auth = require("./lib/auth.js");
var response = require("./lib/response.js");
//server setup
2015-05-15 04:58:13 +00: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,
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 16:10:20 +00:00
2015-06-01 10:04:25 +00:00
//logger
app.use(morgan('combined', {
"stream": logger.stream
}));
2015-05-04 07:53:29 +00:00
2015-07-01 16:10:20 +00:00
//socket io
var io = require('socket.io')(server);
2015-05-04 07:53:29 +00:00
// connect to the mongodb
2015-05-15 04:58:13 +00:00
mongoose.connect(process.env.MONGOLAB_URI || config.mongodbstring);
2015-05-04 07:53:29 +00:00
//others
var db = require("./lib/db.js");
var realtime = require("./lib/realtime.js");
//assign socket io to realtime
realtime.io = io;
2015-05-04 07:53:29 +00:00
//methodOverride
app.use(methodOverride('_method'));
// create application/json parser
var jsonParser = bodyParser.json();
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({
extended: false
});
2015-06-01 10:04:25 +00:00
//session store
var sessionStore = new MongoStore({
mongooseConnection: mongoose.connection,
touchAfter: config.sessiontouch
},
function (err) {
2015-07-01 16:10:20 +00:00
logger.info(err);
2015-06-01 10:04:25 +00:00
});
2015-05-04 07:53:29 +00:00
//compression
app.use(compression());
//session
app.use(session({
name: config.sessionname,
secret: config.sessionsecret,
resave: false, //don't save session if unmodified
saveUninitialized: true, //always create session to ensure the origin
2015-05-04 07:53:29 +00:00
cookie: {
maxAge: new Date(Date.now() + config.sessionlife),
expires: new Date(Date.now() + config.sessionlife),
},
maxAge: new Date(Date.now() + config.sessionlife),
2015-06-01 10:04:25 +00:00
store: sessionStore
2015-05-04 07:53:29 +00: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) {
2015-07-01 16:10:20 +00:00
//logger.info('serializeUser: ' + user._id);
2015-05-04 07:53:29 +00:00
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
User.model.findById(id, function (err, user) {
2015-07-01 16:10:20 +00:00
//logger.info(user)
2015-05-04 07:53:29 +00:00
if (!err) done(null, user);
else done(err, null);
})
});
//routes
//static files
app.use('/', express.static(__dirname + '/public'));
2015-11-29 07:00:41 +00:00
app.use('/vendor/', express.static(__dirname + '/bower_components'));
2015-05-04 07:53:29 +00:00
//template files
app.set('views', __dirname + '/public');
//set render engine
app.engine('html', ejs.renderFile);
//get index
app.get("/", response.showIndex);
2015-12-30 05:33:36 +00:00
//get 403 forbidden
app.get("/403", function(req, res) {
response.errorForbidden(res);
});
//get 404 not found
app.get("/404", function(req, res) {
response.errorNotFound(res);
});
2015-05-04 07:53:29 +00:00
//get status
app.get("/status", function (req, res, next) {
realtime.getStatus(function (data) {
res.end(JSON.stringify(data));
});
});
2015-05-15 04:58:13 +00: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 {
Temp.findTemp(tempid, function (err, temp) {
if (err || !temp)
response.errorForbidden(res);
else {
res.header("Access-Control-Allow-Origin", "*");
res.send({
temp: temp.data
});
temp.remove(function (err) {
if (err)
2015-07-01 16:10:20 +00:00
logger.error('remove temp failed: ' + err);
2015-05-15 04:58:13 +00: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 id = shortid.generate();
var data = req.body.data;
if (!id || !data)
response.errorForbidden(res);
else {
if (config.debug)
2015-07-01 16:10:20 +00:00
logger.info('SERVER received temp from [' + host + ']: ' + req.body.data);
2015-05-15 04:58:13 +00:00
Temp.newTemp(id, data, function (err, temp) {
if (!err && temp) {
res.header("Access-Control-Allow-Origin", "*");
res.send({
status: 'ok',
id: temp.id
});
} else
response.errorInternalError(res);
});
}
}
});
2015-05-04 07:53:29 +00:00
//facebook auth
app.get('/auth/facebook',
passport.authenticate('facebook'),
function (req, res) {});
//facebook auth callback
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
failureRedirect: '/'
}),
function (req, res) {
res.redirect('/');
});
//twitter auth
app.get('/auth/twitter',
passport.authenticate('twitter'),
function (req, res) {});
//twitter auth callback
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
failureRedirect: '/'
}),
function (req, res) {
res.redirect('/');
});
//github auth
app.get('/auth/github',
passport.authenticate('github'),
function (req, res) {});
//github auth callback
app.get('/auth/github/callback',
passport.authenticate('github', {
failureRedirect: '/'
}),
function (req, res) {
res.redirect('/');
});
2016-01-31 21:42:26 +00:00
//github callback actions
app.get('/auth/github/callback/:noteId/:action', response.githubActions);
2015-05-04 07:53:29 +00:00
//dropbox auth
app.get('/auth/dropbox',
passport.authenticate('dropbox-oauth2'),
function (req, res) {});
//dropbox auth callback
app.get('/auth/dropbox/callback',
passport.authenticate('dropbox-oauth2', {
failureRedirect: '/'
}),
function (req, res) {
res.redirect('/');
});
//logout
app.get('/logout', function (req, res) {
if (config.debug && req.isAuthenticated())
logger.info('user logout: ' + req.user._id);
2015-05-04 07:53:29 +00:00
req.logout();
res.redirect('/');
});
//get history
app.get('/history', function (req, res) {
if (req.isAuthenticated()) {
User.model.findById(req.user._id, function (err, user) {
2015-05-04 07:53:29 +00:00
if (err) {
2015-07-01 16:10:20 +00:00
logger.error('read history failed: ' + err);
2015-05-04 07:53:29 +00:00
} else {
var history = [];
if (user.history)
history = JSON.parse(user.history);
res.send({
history: history
});
}
});
} else {
response.errorForbidden(res);
}
});
//post history
app.post('/history', urlencodedParser, function (req, res) {
if (req.isAuthenticated()) {
if (config.debug)
logger.info('SERVER received history from [' + req.user._id + ']: ' + req.body.history);
User.model.findById(req.user._id, function (err, user) {
2015-05-04 07:53:29 +00:00
if (err) {
2015-07-01 16:10:20 +00:00
logger.error('write history failed: ' + err);
2015-05-04 07:53:29 +00:00
} else {
user.history = req.body.history;
user.save(function (err) {
if (err) {
2015-07-01 16:10:20 +00:00
logger.error('write user history failed: ' + err);
2015-05-04 07:53:29 +00:00
} else {
if (config.debug)
2015-07-01 16:10:20 +00:00
logger.info("write user history success: " + user._id);
2015-05-04 07:53:29 +00:00
};
});
}
});
res.end();
} else {
response.errorForbidden(res);
}
});
//get me info
app.get('/me', function (req, res) {
if (req.isAuthenticated()) {
User.model.findById(req.user._id, function (err, user) {
2015-05-04 07:53:29 +00:00
if (err) {
2015-07-01 16:10:20 +00:00
logger.error('read me failed: ' + err);
2015-05-04 07:53:29 +00:00
} else {
var profile = JSON.parse(user.profile);
res.send({
status: 'ok',
id: req.user._id,
2015-05-04 07:53:29 +00:00
name: profile.displayName || profile.username
});
}
});
} else {
res.send({
status: 'forbidden'
});
}
});
2015-05-15 04:58:13 +00: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 16:10:20 +00:00
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
2015-05-15 04:58:13 +00:00
imgur.setClientId(config.imgur.clientID);
2015-07-01 16:10:20 +00:00
try {
imgur.uploadFile(files.image.path)
.then(function (json) {
if (config.debug)
logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
res.send({
link: json.data.link
});
})
.catch(function (err) {
logger.error(err);
res.send('upload image error');
2015-06-01 10:04:25 +00:00
});
2015-07-01 16:10:20 +00:00
} catch (err) {
logger.error(err);
res.send('upload image error');
}
2015-05-15 04:58:13 +00:00
}
});
});
2015-05-04 07:53:29 +00:00
//get new note
app.get("/new", response.newNote);
//get features
app.get("/features", response.showFeatures);
//get publish note
app.get("/s/:shortid", response.showPublishNote);
//publish note actions
app.get("/s/:shortid/:action", response.publishNoteActions);
2015-12-18 15:40:52 +00:00
//get publish slide
app.get("/p/:shortid", response.showPublishSlide);
2015-05-04 07:53:29 +00:00
//get note by id
app.get("/:noteId", response.showNote);
//note actions
app.get("/:noteId/:action", response.noteActions);
//socket.io secure
io.use(realtime.secure);
2015-06-01 10:04:25 +00: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 07:53:29 +00: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
2015-05-15 04:58:13 +00:00
if (config.usessl) {
server.listen(config.sslport, function () {
2015-07-01 16:10:20 +00:00
logger.info('HTTPS Server listening at sslport %d', config.sslport);
2015-05-15 04:58:13 +00:00
});
} else {
server.listen(config.port, function () {
2015-07-01 16:10:20 +00:00
logger.info('HTTP Server listening at port %d', config.port);
2015-05-15 04:58:13 +00:00
});
}
process.on('uncaughtException', function (err) {
logger.error(err);
});