wikijs-fork/server/index.js

279 lines
7.3 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
// 1.0.1
2016-08-17 00:56:55 +00:00
// Licensed under AGPLv3
// ===========================================
const path = require('path')
let wiki = {
IS_DEBUG: process.env.NODE_ENV === 'development',
ROOTPATH: process.cwd(),
SERVERPATH: path.join(process.cwd(), 'server')
}
global.wiki = wiki
2017-02-09 01:52:37 +00:00
process.env.VIPS_WARNING = false
// if (wiki.IS_DEBUG) {
2017-05-19 00:23:24 +00:00
// require('@glimpse/glimpse').init()
// }
let appconf = require('./modules/config')()
wiki.config = appconf.config
wiki.data = appconf.data
2017-03-29 00:19:01 +00:00
2016-08-17 00:56:55 +00:00
// ----------------------------------------
// Load Winston
2016-08-17 00:56:55 +00:00
// ----------------------------------------
wiki.logger = require('./modules/logger')(wiki.IS_DEBUG, 'SERVER')
wiki.logger.info('Wiki.js is initializing...')
2016-08-17 00:56:55 +00:00
// ----------------------------------------
// Load global modules
// ----------------------------------------
wiki.disk = require('./modules/disk').init()
wiki.db = require('./modules/db').init()
wiki.entries = require('./modules/entries').init()
wiki.git = require('./modules/git').init(false)
wiki.lang = require('i18next')
wiki.mark = require('./modules/markdown')
wiki.redis = require('./modules/redis').init()
wiki.search = require('./modules/search').init()
wiki.upl = require('./modules/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 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 SessionRedisStore = require('connect-redis')(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-07-25 02:37:13 +00:00
const graphqlApollo = require('apollo-server-express')
const graphqlSchema = require('./modules/graphql')
2017-02-09 01:52:37 +00:00
var mw = autoload(path.join(wiki.SERVERPATH, '/middlewares'))
var ctrl = autoload(path.join(wiki.SERVERPATH, '/controllers'))
2016-08-17 03:56:08 +00:00
// ----------------------------------------
// Define Express App
// ----------------------------------------
const app = express()
wiki.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
// ----------------------------------------
// Public Assets
// ----------------------------------------
app.use(favicon(path.join(wiki.ROOTPATH, 'assets', 'favicon.ico')))
app.use(express.static(path.join(wiki.ROOTPATH, 'assets'), {
index: false,
maxAge: '7d'
}))
// ----------------------------------------
2016-11-21 01:09:50 +00:00
// Passport Authentication
2016-08-17 03:56:08 +00:00
// ----------------------------------------
require('./modules/auth')(passport)
wiki.rights = require('./modules/rights')
wiki.rights.init()
2016-11-21 01:09:50 +00:00
let sessionStore = new SessionRedisStore({
client: wiki.redis
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,
secret: wiki.config.sessionSecret,
2016-08-17 03:56:08 +00:00
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
// ----------------------------------------
wiki.lang.use(i18nBackend).init({
load: 'languageOnly',
ns: ['common', 'admin', 'auth', 'errors', 'git'],
defaultNS: 'common',
saveMissing: false,
preload: [wiki.config.lang],
lng: wiki.config.lang,
fallbackLng: 'en',
backend: {
loadPath: path.join(wiki.SERVERPATH, 'locales/{{lng}}/{{ns}}.json')
}
})
2016-08-17 03:56:08 +00:00
// ----------------------------------------
// View Engine Setup
// ----------------------------------------
app.set('views', path.join(wiki.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')
app.locals.t = wiki.lang.t.bind(wiki.lang)
2017-02-09 01:52:37 +00:00
app.locals.moment = require('moment')
app.locals.moment.locale(wiki.config.lang)
app.locals.appconfig = wiki.config
2017-02-09 01:52:37 +00:00
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-07-25 02:37:13 +00:00
app.use('/graphql', graphqlApollo.graphqlExpress({ schema: graphqlSchema }))
app.use('/graphiql', graphqlApollo.graphiqlExpress({ endpointURL: '/graphql' }))
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,
error: wiki.IS_DEBUG ? err : {}
2017-02-09 01:52:37 +00:00
})
})
2016-08-17 03:56:08 +00:00
// ----------------------------------------
// Start HTTP server
// ----------------------------------------
wiki.logger.info('Starting HTTP/WS server on port ' + wiki.config.port + '...')
2016-08-17 03:56:08 +00:00
app.set('port', wiki.config.port)
2017-02-09 01:52:37 +00:00
var server = http.createServer(app)
var io = socketio(server)
server.listen(wiki.config.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':
wiki.logger.error('Listening on port ' + wiki.config.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':
wiki.logger.error('Port ' + wiki.config.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', () => {
wiki.logger.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: wiki.config.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(wiki.SERVERPATH, 'agent.js'))
bgAgent.on('message', m => {
if (!m.action) {
return
}
switch (m.action) {
case 'searchAdd':
wiki.search.add(m.content)
break
}
})
2017-05-19 00:23:24 +00:00
// ----------------------------------------
// Graceful shutdown
// ----------------------------------------
graceful.on('exit', () => {
wiki.logger.info('- SHUTTING DOWN - Terminating Background Agent...')
bgAgent.kill()
wiki.logger.info('- SHUTTING DOWN - Performing git sync...')
2017-05-19 00:23:24 +00:00
return global.git.resync().then(() => {
wiki.logger.info('- SHUTTING DOWN - Git sync successful. Now safe to exit.')
process.exit()
2017-05-19 00:23:24 +00:00
})
2017-02-09 01:52:37 +00:00
})