2017-04-12 13:16:33 -04:00
|
|
|
'use strict'
|
2017-03-08 05:45:51 -05:00
|
|
|
// app
|
|
|
|
// external modules
|
|
|
|
var express = require('express')
|
2017-04-11 18:05:43 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
var ejs = require('ejs')
|
|
|
|
var passport = require('passport')
|
|
|
|
var methodOverride = require('method-override')
|
|
|
|
var cookieParser = require('cookie-parser')
|
2015-05-04 03:53:29 -04:00
|
|
|
var compression = require('compression')
|
2017-03-08 05:45:51 -05:00
|
|
|
var session = require('express-session')
|
|
|
|
var SequelizeStore = require('connect-session-sequelize')(session.Store)
|
|
|
|
var fs = require('fs')
|
|
|
|
var path = require('path')
|
2017-04-11 18:01:45 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
var morgan = require('morgan')
|
|
|
|
var passportSocketIo = require('passport.socketio')
|
|
|
|
var helmet = require('helmet')
|
|
|
|
var i18n = require('i18n')
|
|
|
|
var flash = require('connect-flash')
|
|
|
|
|
|
|
|
// core
|
2017-04-12 12:20:28 -04:00
|
|
|
var config = require('./lib/config')
|
|
|
|
var logger = require('./lib/logger')
|
|
|
|
var response = require('./lib/response')
|
2017-03-08 05:45:51 -05:00
|
|
|
var models = require('./lib/models')
|
|
|
|
|
2017-03-19 13:39:09 -04:00
|
|
|
// generate front-end constants by template
|
|
|
|
var constpath = path.join(__dirname, './public/js/lib/common/constant.ejs')
|
|
|
|
var data = {
|
|
|
|
domain: config.domain,
|
|
|
|
urlpath: config.urlpath,
|
|
|
|
debug: config.debug,
|
|
|
|
version: config.version,
|
2017-04-12 13:57:55 -04:00
|
|
|
GOOGLE_API_KEY: config.google.clientSecret,
|
|
|
|
GOOGLE_CLIENT_ID: config.google.clientID,
|
|
|
|
DROPBOX_APP_KEY: config.dropbox.clientSecret
|
2017-03-19 13:39:09 -04:00
|
|
|
}
|
2017-04-12 13:57:55 -04:00
|
|
|
|
2017-03-19 13:39:09 -04:00
|
|
|
ejs.renderFile(constpath, data, {}, function (err, str) {
|
|
|
|
if (err) throw new Error(err)
|
|
|
|
fs.writeFileSync(path.join(__dirname, './public/build/constant.js'), str)
|
|
|
|
})
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// server setup
|
|
|
|
var app = express()
|
|
|
|
var server = null
|
2015-05-15 00:58:13 -04:00
|
|
|
if (config.usessl) {
|
2017-03-08 05:45:51 -05:00
|
|
|
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,
|
|
|
|
dhparam: fs.readFileSync(config.dhparampath, 'utf8'),
|
|
|
|
requestCert: false,
|
|
|
|
rejectUnauthorized: false
|
|
|
|
}
|
|
|
|
server = require('https').createServer(options, app)
|
2015-05-15 00:58:13 -04:00
|
|
|
} else {
|
2017-03-08 05:45:51 -05:00
|
|
|
server = require('http').createServer(app)
|
2015-05-15 00:58:13 -04:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// logger
|
2015-06-01 06:04:25 -04:00
|
|
|
app.use(morgan('combined', {
|
2017-03-18 14:44:06 -04:00
|
|
|
'stream': logger
|
2017-03-08 05:45:51 -05:00
|
|
|
}))
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// socket io
|
|
|
|
var io = require('socket.io')(server)
|
2016-11-17 23:18:29 -05:00
|
|
|
io.engine.ws = new (require('uws').Server)({
|
2017-03-08 05:45:51 -05:00
|
|
|
noServer: true,
|
|
|
|
perMessageDeflate: false
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// others
|
|
|
|
var realtime = require('./lib/realtime.js')
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// assign socket io to realtime
|
|
|
|
realtime.io = io
|
2015-09-23 23:36:41 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// methodOverride
|
|
|
|
app.use(methodOverride('_method'))
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// session store
|
2016-04-20 06:03:55 -04:00
|
|
|
var sessionStore = new SequelizeStore({
|
2017-03-08 05:45:51 -05:00
|
|
|
db: models.sequelize
|
|
|
|
})
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// compression
|
|
|
|
app.use(compression())
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2016-03-14 22:41:49 -04:00
|
|
|
// use hsts to tell https users stick to this
|
|
|
|
app.use(helmet.hsts({
|
2017-03-08 05:45:51 -05:00
|
|
|
maxAge: 31536000 * 1000, // 365 days
|
|
|
|
includeSubdomains: true,
|
|
|
|
preload: true
|
|
|
|
}))
|
2016-03-14 22:41:49 -04:00
|
|
|
|
2016-08-18 23:49:24 -04:00
|
|
|
i18n.configure({
|
2017-03-20 02:52:25 -04:00
|
|
|
locales: ['en', 'zh', 'fr', 'de', 'ja', 'es', 'ca', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk', 'hi', 'sv', 'eo'],
|
2017-03-08 05:45:51 -05:00
|
|
|
cookie: 'locale',
|
|
|
|
directory: path.join(__dirname, '/locales')
|
|
|
|
})
|
2016-08-18 23:49:24 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
app.use(cookieParser())
|
2016-08-18 23:49:24 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
app.use(i18n.init)
|
2016-08-18 23:49:24 -04:00
|
|
|
|
2016-04-20 06:14:28 -04:00
|
|
|
// routes without sessions
|
|
|
|
// static files
|
2017-03-08 05:45:51 -05:00
|
|
|
app.use('/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticcachetime }))
|
2016-04-20 06:14:28 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// session
|
2015-05-04 03:53:29 -04:00
|
|
|
app.use(session({
|
2017-03-08 05:45:51 -05:00
|
|
|
name: config.sessionname,
|
|
|
|
secret: config.sessionsecret,
|
|
|
|
resave: false, // don't save session if unmodified
|
|
|
|
saveUninitialized: true, // always create session to ensure the origin
|
|
|
|
rolling: true, // reset maxAge on every response
|
|
|
|
cookie: {
|
|
|
|
maxAge: config.sessionlife
|
|
|
|
},
|
|
|
|
store: sessionStore
|
|
|
|
}))
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2016-03-14 22:42:07 -04:00
|
|
|
// session resumption
|
2017-03-08 05:45:51 -05:00
|
|
|
var tlsSessionStore = {}
|
2016-03-14 22:42:07 -04:00
|
|
|
server.on('newSession', function (id, data, cb) {
|
2017-03-08 05:45:51 -05:00
|
|
|
tlsSessionStore[id.toString('hex')] = data
|
|
|
|
cb()
|
|
|
|
})
|
2016-03-14 22:42:07 -04:00
|
|
|
server.on('resumeSession', function (id, cb) {
|
2017-03-08 05:45:51 -05:00
|
|
|
cb(null, tlsSessionStore[id.toString('hex')] || null)
|
|
|
|
})
|
2016-03-14 22:42:07 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// middleware which blocks requests when we're too busy
|
2017-04-11 18:05:43 -04:00
|
|
|
app.use(require('./lib/web/middleware/tooBusy'))
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
app.use(flash())
|
2016-12-01 12:58:14 -05:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// passport
|
|
|
|
app.use(passport.initialize())
|
|
|
|
app.use(passport.session())
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2016-12-03 01:37:24 -05:00
|
|
|
// check uri is valid before going further
|
2017-04-13 09:28:00 -04:00
|
|
|
app.use(require('./lib/web/middleware/checkURIValid'))
|
2016-12-11 21:50:43 -05:00
|
|
|
// redirect url without trailing slashes
|
2017-05-07 12:52:13 -04:00
|
|
|
app.use(require('./lib/web/middleware/redirectWithoutTrailingSlashes'))
|
2016-04-20 06:19:11 -04:00
|
|
|
|
2016-04-20 06:14:28 -04:00
|
|
|
// routes need sessions
|
2017-03-08 05:45:51 -05:00
|
|
|
// template files
|
|
|
|
app.set('views', path.join(__dirname, '/public/views'))
|
|
|
|
// set render engine
|
|
|
|
app.engine('ejs', ejs.renderFile)
|
|
|
|
// set view engine
|
|
|
|
app.set('view engine', 'ejs')
|
2016-07-31 12:06:07 -04:00
|
|
|
|
2017-04-11 17:38:54 -04:00
|
|
|
app.use(require('./lib/web/baseRouter'))
|
2017-04-11 17:39:41 -04:00
|
|
|
app.use(require('./lib/web/statusRouter'))
|
2017-04-11 17:41:14 -04:00
|
|
|
app.use(require('./lib/web/auth'))
|
2017-04-11 17:48:55 -04:00
|
|
|
app.use(require('./lib/web/historyRouter'))
|
2017-04-11 17:55:36 -04:00
|
|
|
app.use(require('./lib/web/userRouter'))
|
2017-04-11 18:01:45 -04:00
|
|
|
app.use(require('./lib/web/imageRouter'))
|
2017-04-11 18:01:22 -04:00
|
|
|
app.use(require('./lib/web/noteRouter'))
|
2017-04-11 17:56:20 -04:00
|
|
|
|
|
|
|
// response not found if no any route matxches
|
2016-04-20 06:19:29 -04:00
|
|
|
app.get('*', function (req, res) {
|
2017-03-08 05:45:51 -05:00
|
|
|
response.errorNotFound(res)
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// socket.io secure
|
|
|
|
io.use(realtime.secure)
|
|
|
|
// socket.io auth
|
2015-06-01 06:04:25 -04:00
|
|
|
io.use(passportSocketIo.authorize({
|
2017-03-08 05:45:51 -05:00
|
|
|
cookieParser: cookieParser,
|
|
|
|
key: config.sessionname,
|
|
|
|
secret: config.sessionsecret,
|
|
|
|
store: sessionStore,
|
|
|
|
success: realtime.onAuthorizeSuccess,
|
|
|
|
fail: realtime.onAuthorizeFail
|
|
|
|
}))
|
|
|
|
// 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
|
|
|
|
function startListen () {
|
|
|
|
server.listen(config.port, function () {
|
|
|
|
var schema = config.usessl ? 'HTTPS' : 'HTTP'
|
|
|
|
logger.info('%s Server listening at port %d', schema, config.port)
|
2017-04-12 13:57:55 -04:00
|
|
|
realtime.maintenance = false
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
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 () {
|
2017-03-08 05:45:51 -05:00
|
|
|
// check if realtime is ready
|
|
|
|
if (realtime.isReady()) {
|
|
|
|
models.Revision.checkAllNotesRevision(function (err, notes) {
|
|
|
|
if (err) throw new Error(err)
|
|
|
|
if (!notes || notes.length <= 0) return startListen()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw new Error('server still not ready after db synced')
|
|
|
|
}
|
|
|
|
})
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
// log uncaught exception
|
2015-07-11 00:44:16 -04:00
|
|
|
process.on('uncaughtException', function (err) {
|
2017-03-08 05:45:51 -05:00
|
|
|
logger.error('An uncaught exception has occured.')
|
|
|
|
logger.error(err)
|
|
|
|
logger.error('Process will exit now.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2016-06-01 02:18:54 -04:00
|
|
|
|
2017-01-19 20:13:09 -05:00
|
|
|
// install exit handler
|
2017-03-08 05:45:51 -05:00
|
|
|
function handleTermSignals () {
|
2017-03-22 03:31:39 -04:00
|
|
|
logger.info('hackmd has been killed by signal, try to exit gracefully...')
|
2017-04-12 14:00:27 -04:00
|
|
|
realtime.maintenance = true
|
2017-03-08 05:45:51 -05:00
|
|
|
// 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
|
|
|
|
socket.emit('maintenance')
|
|
|
|
setTimeout(function () {
|
|
|
|
socket.disconnect(true)
|
|
|
|
}, 0)
|
|
|
|
})
|
|
|
|
var checkCleanTimer = setInterval(function () {
|
|
|
|
if (realtime.isReady()) {
|
|
|
|
models.Revision.checkAllNotesRevision(function (err, notes) {
|
|
|
|
if (err) return logger.error(err)
|
|
|
|
if (!notes || notes.length <= 0) {
|
|
|
|
clearInterval(checkCleanTimer)
|
|
|
|
return process.exit(0)
|
2016-06-01 02:18:54 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, 100)
|
2017-01-19 20:13:09 -05:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
process.on('SIGINT', handleTermSignals)
|
|
|
|
process.on('SIGTERM', handleTermSignals)
|
2017-03-22 03:26:35 -04:00
|
|
|
process.on('SIGQUIT', handleTermSignals)
|