wikijs-fork/server/index.js

259 lines
6.4 KiB
JavaScript
Raw Normal View History

2017-02-09 01:52:37 +00:00
'use strict'
2016-08-17 00:56:55 +00:00
// ===========================================
// Wiki.js
2016-08-17 00:56:55 +00:00
// 1.0.0
// Licensed under AGPLv3
// ===========================================
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'
2017-02-09 01:52:37 +00:00
process.env.VIPS_WARNING = false
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
// ----------------------------------------
// 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')
winston.info('Wiki.js is initializing...')
2016-08-17 00:56:55 +00:00
// ----------------------------------------
// Load global modules
// ----------------------------------------
2017-02-09 01:52:37 +00:00
global.lcdata = require('./libs/local').init()
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')
global.search = require('./libs/search').init()
2017-02-09 01:52:37 +00:00
global.upl = require('./libs/uploads').init()
// ----------------------------------------
// 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')
const i18nextBackend = require('i18next-node-fs-backend')
const i18nextMw = require('i18next-express-middleware')
const passport = require('passport')
const passportSocketIo = require('passport.socketio')
const session = require('express-session')
const SessionMongoStore = require('connect-mongo')(session)
const socketio = require('socket.io')
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-02-09 01:52:37 +00:00
global.app = express()
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
// ----------------------------------------
// Public Assets
// ----------------------------------------
2017-02-09 01:52:37 +00:00
app.use(favicon(path.join(ROOTPATH, 'assets', 'favicon.ico')))
app.use(express.static(path.join(ROOTPATH, 'assets')))
// ----------------------------------------
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-02-09 01:52:37 +00:00
rights.init()
2016-11-21 01:09:50 +00:00
let sessionStore = new SessionMongoStore({
mongooseConnection: db.connection,
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({
name: 'wikijs.sid',
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
// ----------------------------------------
// SEO
// ----------------------------------------
app.use(mw.seo)
2016-08-17 03:56:08 +00:00
// ----------------------------------------
// Localization Engine
// ----------------------------------------
lang
2017-02-09 01:52:37 +00:00
.use(i18nextBackend)
.use(i18nextMw.LanguageDetector)
2016-08-17 03:56:08 +00:00
.init({
load: 'languageOnly',
2016-11-02 03:02:11 +00:00
ns: ['common', 'auth'],
2016-08-17 03:56:08 +00:00
defaultNS: 'common',
saveMissing: false,
supportedLngs: ['en', 'fr'],
preload: ['en', 'fr'],
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-02-09 01:52:37 +00:00
app.use(i18nextMw.handle(lang))
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-02-09 01:52:37 +00:00
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
2016-08-17 03:56:08 +00:00
// ----------------------------------------
// View accessible data
// ----------------------------------------
2017-02-09 01:52:37 +00:00
app.locals._ = require('lodash')
app.locals.moment = require('moment')
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-04-28 22:46:27 +00:00
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)
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-04-28 22:46:27 +00:00
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-04-28 22:46:27 +00:00
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-04-28 22:46:27 +00:00
winston.info('HTTP/WS server started successfully! [RUNNING]')
2017-02-09 01:52:37 +00:00
})
// ----------------------------------------
// WebSocket
// ----------------------------------------
io.use(passportSocketIo.authorize({
key: 'wikijs.sid',
store: sessionStore,
secret: appconfig.sessionSecret,
cookieParser,
success: (data, accept) => {
2017-02-09 01:52:37 +00:00
accept()
},
fail: (data, message, error, accept) => {
accept()
}
2017-02-09 01:52:37 +00:00
}))
2017-02-09 01:52:37 +00:00
io.on('connection', ctrl.ws)
// ----------------------------------------
// Start child processes
// ----------------------------------------
let bgAgent = fork(path.join(SERVERPATH, 'agent.js'))
bgAgent.on('message', m => {
if (!m.action) {
return
}
switch (m.action) {
case 'searchAdd':
search.add(m.content)
break
}
})
process.on('exit', (code) => {
2017-02-09 01:52:37 +00:00
bgAgent.disconnect()
})