2017-02-09 01:52:37 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-08-17 00:56:55 +00:00
|
|
|
// ===========================================
|
2016-12-09 03:44:31 +00:00
|
|
|
// Wiki.js
|
2016-08-17 00:56:55 +00:00
|
|
|
// 1.0.0
|
|
|
|
// Licensed under AGPLv3
|
|
|
|
// ===========================================
|
|
|
|
|
2017-04-28 21:58:38 +00:00
|
|
|
const path = require('path')
|
|
|
|
const ROOTPATH = process.cwd()
|
|
|
|
const SERVERPATH = path.join(ROOTPATH, 'server')
|
|
|
|
|
|
|
|
global.ROOTPATH = ROOTPATH
|
|
|
|
global.SERVERPATH = SERVERPATH
|
2017-04-28 22:46:27 +00:00
|
|
|
const IS_DEBUG = process.env.NODE_ENV === 'development'
|
2016-08-20 21:20:53 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
process.env.VIPS_WARNING = false
|
|
|
|
|
2017-05-19 00:23:24 +00:00
|
|
|
// if (IS_DEBUG) {
|
|
|
|
// require('@glimpse/glimpse').init()
|
|
|
|
// }
|
2017-05-14 17:56:33 +00:00
|
|
|
|
2017-04-02 23:56:47 +00:00
|
|
|
let appconf = require('./libs/config')()
|
2017-03-29 00:19:01 +00:00
|
|
|
global.appconfig = appconf.config
|
|
|
|
global.appdata = appconf.data
|
|
|
|
|
2016-08-17 00:56:55 +00:00
|
|
|
// ----------------------------------------
|
2016-09-25 04:08:49 +00:00
|
|
|
// Load Winston
|
2016-08-17 00:56:55 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-04-28 22:46:27 +00:00
|
|
|
global.winston = require('./libs/logger')(IS_DEBUG, 'SERVER')
|
2017-05-13 19:29:00 +00:00
|
|
|
global.winston.info('Wiki.js is initializing...')
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2016-09-25 04:08:49 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Load global modules
|
|
|
|
// ----------------------------------------
|
2016-08-22 03:18:31 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
global.lcdata = require('./libs/local').init()
|
2017-03-31 01:53:21 +00:00
|
|
|
global.db = require('./libs/db').init()
|
2017-02-09 01:52:37 +00:00
|
|
|
global.entries = require('./libs/entries').init()
|
|
|
|
global.git = require('./libs/git').init(false)
|
|
|
|
global.lang = require('i18next')
|
|
|
|
global.mark = require('./libs/markdown')
|
2017-02-12 20:40:43 +00:00
|
|
|
global.search = require('./libs/search').init()
|
2017-02-09 01:52:37 +00:00
|
|
|
global.upl = require('./libs/uploads').init()
|
2016-08-20 21:20:53 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Load modules
|
|
|
|
// ----------------------------------------
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
const autoload = require('auto-load')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const compression = require('compression')
|
|
|
|
const cookieParser = require('cookie-parser')
|
|
|
|
const express = require('express')
|
|
|
|
const favicon = require('serve-favicon')
|
|
|
|
const flash = require('connect-flash')
|
|
|
|
const fork = require('child_process').fork
|
|
|
|
const http = require('http')
|
2017-05-13 19:29:00 +00:00
|
|
|
const i18nBackend = require('i18next-node-fs-backend')
|
2017-02-09 01:52:37 +00:00
|
|
|
const passport = require('passport')
|
|
|
|
const passportSocketIo = require('passport.socketio')
|
|
|
|
const session = require('express-session')
|
|
|
|
const SessionMongoStore = require('connect-mongo')(session)
|
2017-05-19 00:23:24 +00:00
|
|
|
const graceful = require('node-graceful')
|
2017-02-09 01:52:37 +00:00
|
|
|
const socketio = require('socket.io')
|
|
|
|
|
2017-04-28 21:58:38 +00:00
|
|
|
var mw = autoload(path.join(SERVERPATH, '/middlewares'))
|
|
|
|
var ctrl = autoload(path.join(SERVERPATH, '/controllers'))
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Define Express App
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-05-13 19:29:00 +00:00
|
|
|
const app = express()
|
|
|
|
global.app = app
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use(compression())
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Security
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use(mw.security)
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-16 05:34:34 +00:00
|
|
|
// Public Assets
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use(favicon(path.join(ROOTPATH, 'assets', 'favicon.ico')))
|
2017-05-28 18:34:50 +00:00
|
|
|
app.use(express.static(path.join(ROOTPATH, 'assets'), {
|
|
|
|
index: false,
|
|
|
|
maxAge: '7d'
|
|
|
|
}))
|
2016-10-16 05:34:34 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-11-21 01:09:50 +00:00
|
|
|
// Passport Authentication
|
2016-08-17 03:56:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-04-02 23:56:47 +00:00
|
|
|
require('./libs/auth')(passport)
|
|
|
|
global.rights = require('./libs/rights')
|
2017-05-13 19:29:00 +00:00
|
|
|
global.rights.init()
|
2016-11-21 01:09:50 +00:00
|
|
|
|
2017-04-29 21:42:33 +00:00
|
|
|
let sessionStore = new SessionMongoStore({
|
2017-05-13 19:29:00 +00:00
|
|
|
mongooseConnection: global.db.connection,
|
2016-10-30 22:41:10 +00:00
|
|
|
touchAfter: 15
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use(cookieParser())
|
2016-08-17 03:56:08 +00:00
|
|
|
app.use(session({
|
2017-04-29 21:42:33 +00:00
|
|
|
name: 'wikijs.sid',
|
2016-10-30 22:41:10 +00:00
|
|
|
store: sessionStore,
|
2016-08-17 03:56:08 +00:00
|
|
|
secret: appconfig.sessionSecret,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false
|
2017-02-09 01:52:37 +00:00
|
|
|
}))
|
|
|
|
app.use(flash())
|
|
|
|
app.use(passport.initialize())
|
|
|
|
app.use(passport.session())
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-04-23 14:56:14 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// SEO
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.seo)
|
|
|
|
|
2016-08-17 03:56:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Localization Engine
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-05-13 19:29:00 +00:00
|
|
|
global.lang
|
|
|
|
.use(i18nBackend)
|
2016-08-17 03:56:08 +00:00
|
|
|
.init({
|
|
|
|
load: 'languageOnly',
|
2017-05-04 02:10:12 +00:00
|
|
|
ns: ['common', 'admin', 'auth', 'errors', 'git'],
|
2016-08-17 03:56:08 +00:00
|
|
|
defaultNS: 'common',
|
|
|
|
saveMissing: false,
|
2017-05-13 19:29:00 +00:00
|
|
|
preload: [appconfig.lang],
|
|
|
|
lng: appconfig.lang,
|
2017-02-09 01:52:37 +00:00
|
|
|
fallbackLng: 'en',
|
2016-08-17 03:56:08 +00:00
|
|
|
backend: {
|
2017-04-29 20:33:48 +00:00
|
|
|
loadPath: path.join(SERVERPATH, 'locales/{{lng}}/{{ns}}.json')
|
2016-08-17 03:56:08 +00:00
|
|
|
}
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View Engine Setup
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-04-28 21:58:38 +00:00
|
|
|
app.set('views', path.join(SERVERPATH, 'views'))
|
2017-02-09 01:52:37 +00:00
|
|
|
app.set('view engine', 'pug')
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-05-04 23:06:29 +00:00
|
|
|
app.use(bodyParser.json({ limit: '1mb' }))
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View accessible data
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.locals._ = require('lodash')
|
2017-05-13 19:29:00 +00:00
|
|
|
app.locals.t = global.lang.t.bind(global.lang)
|
2017-02-09 01:52:37 +00:00
|
|
|
app.locals.moment = require('moment')
|
2017-05-13 19:29:00 +00:00
|
|
|
app.locals.moment.locale(appconfig.lang)
|
2017-02-09 01:52:37 +00:00
|
|
|
app.locals.appconfig = appconfig
|
|
|
|
app.use(mw.flash)
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Controllers
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use('/', ctrl.auth)
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use('/uploads', mw.auth, ctrl.uploads)
|
|
|
|
app.use('/admin', mw.auth, ctrl.admin)
|
|
|
|
app.use('/', mw.auth, ctrl.pages)
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Error handling
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
var err = new Error('Not Found')
|
|
|
|
err.status = 404
|
|
|
|
next(err)
|
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
res.status(err.status || 500)
|
2016-08-17 03:56:08 +00:00
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
2016-11-21 01:09:50 +00:00
|
|
|
error: IS_DEBUG ? err : {}
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Start HTTP server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-05-13 19:29:00 +00:00
|
|
|
global.winston.info('Starting HTTP/WS server on port ' + appconfig.port + '...')
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
app.set('port', appconfig.port)
|
|
|
|
var server = http.createServer(app)
|
|
|
|
var io = socketio(server)
|
2016-10-16 05:34:34 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
server.listen(appconfig.port)
|
2016-08-17 03:56:08 +00:00
|
|
|
server.on('error', (error) => {
|
|
|
|
if (error.syscall !== 'listen') {
|
2017-02-09 01:52:37 +00:00
|
|
|
throw error
|
2016-08-17 03:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle specific listen errors with friendly messages
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
2017-05-13 19:29:00 +00:00
|
|
|
global.winston.error('Listening on port ' + appconfig.port + ' requires elevated privileges!')
|
2017-04-05 02:04:17 +00:00
|
|
|
return process.exit(1)
|
2016-08-17 03:56:08 +00:00
|
|
|
case 'EADDRINUSE':
|
2017-05-13 19:29:00 +00:00
|
|
|
global.winston.error('Port ' + appconfig.port + ' is already in use!')
|
2017-04-05 02:04:17 +00:00
|
|
|
return process.exit(1)
|
2016-08-17 03:56:08 +00:00
|
|
|
default:
|
2017-02-09 01:52:37 +00:00
|
|
|
throw error
|
2016-08-17 03:56:08 +00:00
|
|
|
}
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
server.on('listening', () => {
|
2017-05-13 19:29:00 +00:00
|
|
|
global.winston.info('HTTP/WS server started successfully! [RUNNING]')
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
2016-09-01 02:45:28 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-30 22:41:10 +00:00
|
|
|
// WebSocket
|
2016-09-01 02:45:28 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-10-30 22:41:10 +00:00
|
|
|
io.use(passportSocketIo.authorize({
|
2017-04-29 21:42:33 +00:00
|
|
|
key: 'wikijs.sid',
|
2016-10-30 22:41:10 +00:00
|
|
|
store: sessionStore,
|
|
|
|
secret: appconfig.sessionSecret,
|
|
|
|
cookieParser,
|
|
|
|
success: (data, accept) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
accept()
|
2016-10-30 22:41:10 +00:00
|
|
|
},
|
|
|
|
fail: (data, message, error, accept) => {
|
2017-04-29 21:42:33 +00:00
|
|
|
accept()
|
2016-10-30 22:41:10 +00:00
|
|
|
}
|
2017-02-09 01:52:37 +00:00
|
|
|
}))
|
2016-10-30 22:41:10 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
io.on('connection', ctrl.ws)
|
2016-09-05 23:23:49 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-16 05:34:34 +00:00
|
|
|
// Start child processes
|
2016-09-05 23:23:49 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-04-28 21:58:38 +00:00
|
|
|
let bgAgent = fork(path.join(SERVERPATH, 'agent.js'))
|
2017-02-12 20:40:43 +00:00
|
|
|
|
|
|
|
bgAgent.on('message', m => {
|
|
|
|
if (!m.action) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (m.action) {
|
|
|
|
case 'searchAdd':
|
2017-05-13 19:29:00 +00:00
|
|
|
global.search.add(m.content)
|
2017-02-12 20:40:43 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-09-05 23:23:49 +00:00
|
|
|
|
2017-05-19 00:23:24 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Graceful shutdown
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
graceful.on('exit', () => {
|
2017-07-20 17:13:06 +00:00
|
|
|
global.winston.info('- SHUTTING DOWN - Terminating Background Agent...')
|
|
|
|
bgAgent.kill()
|
2017-05-19 00:23:24 +00:00
|
|
|
global.winston.info('- SHUTTING DOWN - Performing git sync...')
|
|
|
|
return global.git.resync().then(() => {
|
|
|
|
global.winston.info('- SHUTTING DOWN - Git sync successful. Now safe to exit.')
|
2017-07-20 17:13:06 +00:00
|
|
|
process.exit()
|
2017-05-19 00:23:24 +00:00
|
|
|
})
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|