wikijs-fork/server.js

257 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
// ===========================================
2017-02-09 01:52:37 +00:00
global.PROCNAME = 'SERVER'
global.ROOTPATH = __dirname
global.IS_DEBUG = process.env.NODE_ENV === 'development'
if (IS_DEBUG) {
global.CORE_PATH = ROOTPATH + '/../core/'
} else {
2017-02-09 01:52:37 +00:00
global.CORE_PATH = ROOTPATH + '/node_modules/requarks-core/'
}
2017-02-09 01:52:37 +00:00
process.env.VIPS_WARNING = false
2016-08-17 00:56:55 +00:00
// ----------------------------------------
// Load Winston
2016-08-17 00:56:55 +00:00
// ----------------------------------------
2017-02-09 01:52:37 +00:00
global.winston = require(CORE_PATH + 'core-libs/winston')(IS_DEBUG)
winston.info('[SERVER] Wiki.js is initializing...')
2016-08-17 00:56:55 +00:00
// ----------------------------------------
// Load global modules
// ----------------------------------------
2017-02-09 01:52:37 +00:00
let appconf = require(CORE_PATH + 'core-libs/config')()
global.appconfig = appconf.config
global.appdata = appconf.data
global.lcdata = require('./libs/local').init()
global.db = require(CORE_PATH + 'core-libs/mongodb').init()
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 path = require('path')
const session = require('express-session')
const SessionMongoStore = require('connect-mongo')(session)
const socketio = require('socket.io')
var mw = autoload(CORE_PATH + '/core-middlewares')
var ctrl = autoload(path.join(ROOTPATH, '/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-02-09 01:52:37 +00:00
require(CORE_PATH + 'core-libs/auth')(passport)
global.rights = require(CORE_PATH + 'core-libs/rights')
rights.init()
2016-11-21 01:09:50 +00:00
2017-02-09 01:52:37 +00:00
var 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: 'requarkswiki.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
// ----------------------------------------
// 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: {
loadPath: './locales/{{lng}}/{{ns}}.json'
}
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(ROOTPATH, 'views'))
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-02-09 01:52:37 +00:00
winston.info('[SERVER] 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-02-09 01:52:37 +00:00
console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!')
process.exit(1)
break
2016-08-17 03:56:08 +00:00
case 'EADDRINUSE':
2017-02-09 01:52:37 +00:00
console.error('Port ' + appconfig.port + ' is already in use!')
process.exit(1)
break
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-02-09 01:52:37 +00:00
winston.info('[SERVER] HTTP/WS server started successfully! [RUNNING]')
})
// ----------------------------------------
// WebSocket
// ----------------------------------------
io.use(passportSocketIo.authorize({
key: 'requarkswiki.sid',
store: sessionStore,
secret: appconfig.sessionSecret,
passport,
cookieParser,
success: (data, accept) => {
2017-02-09 01:52:37 +00:00
accept()
},
fail: (data, message, error, accept) => {
2017-02-09 01:52:37 +00:00
return accept(new Error(message))
}
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('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()
})