2017-07-29 04:11:22 +00:00
|
|
|
/* global wiki */
|
|
|
|
|
2017-10-08 02:44:35 +00:00
|
|
|
module.exports = () => {
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Load global modules
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
wiki.auth = require('./modules/auth').init()
|
2017-07-29 21:33:08 +00:00
|
|
|
wiki.disk = require('./modules/disk').init()
|
2017-08-03 03:47:11 +00:00
|
|
|
wiki.docs = require('./modules/documents').init()
|
2017-07-29 21:33:08 +00:00
|
|
|
wiki.git = require('./modules/git').init(false)
|
2017-09-25 03:22:33 +00:00
|
|
|
wiki.lang = require('./modules/localization').init()
|
2017-07-29 21:33:08 +00:00
|
|
|
wiki.mark = require('./modules/markdown')
|
|
|
|
wiki.search = require('./modules/search').init()
|
|
|
|
wiki.upl = require('./modules/uploads').init()
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Load modules
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
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 http = require('http')
|
|
|
|
const path = require('path')
|
|
|
|
const session = require('express-session')
|
|
|
|
const SessionRedisStore = require('connect-redis')(session)
|
|
|
|
const graceful = require('node-graceful')
|
|
|
|
const graphqlApollo = require('apollo-server-express')
|
|
|
|
const graphqlSchema = require('./modules/graphql')
|
|
|
|
|
|
|
|
var mw = autoload(path.join(wiki.SERVERPATH, '/middlewares'))
|
|
|
|
var ctrl = autoload(path.join(wiki.SERVERPATH, '/controllers'))
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Define Express App
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
wiki.app = app
|
|
|
|
app.use(compression())
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Security
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.security)
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// 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'
|
|
|
|
}))
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Passport Authentication
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
let sessionStore = new SessionRedisStore({
|
|
|
|
client: wiki.redis
|
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use(cookieParser())
|
|
|
|
app.use(session({
|
|
|
|
name: 'wikijs.sid',
|
|
|
|
store: sessionStore,
|
|
|
|
secret: wiki.config.site.sessionSecret,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false
|
|
|
|
}))
|
|
|
|
app.use(flash())
|
2017-09-11 03:00:03 +00:00
|
|
|
app.use(wiki.auth.passport.initialize())
|
|
|
|
app.use(wiki.auth.passport.session())
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// SEO
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.seo)
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View Engine Setup
|
|
|
|
// ----------------------------------------
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.set('views', path.join(wiki.SERVERPATH, 'views'))
|
|
|
|
app.set('view engine', 'pug')
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use(bodyParser.json({ limit: '1mb' }))
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// View accessible data
|
|
|
|
// ----------------------------------------
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
app.locals.basedir = wiki.ROOTPATH
|
2017-07-29 21:33:08 +00:00
|
|
|
app.locals._ = require('lodash')
|
2017-09-25 03:22:33 +00:00
|
|
|
app.locals.t = wiki.lang.engine.t.bind(wiki.lang)
|
2017-07-29 21:33:08 +00:00
|
|
|
app.locals.moment = require('moment')
|
|
|
|
app.locals.moment.locale(wiki.config.site.lang)
|
2017-09-10 05:41:22 +00:00
|
|
|
app.locals.config = wiki.config
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use(mw.flash)
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Controllers
|
|
|
|
// ----------------------------------------
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use('/', ctrl.auth)
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use('/graphql', graphqlApollo.graphqlExpress({ schema: graphqlSchema }))
|
|
|
|
app.use('/graphiql', graphqlApollo.graphiqlExpress({ endpointURL: '/graphql' }))
|
2017-07-30 04:04:57 +00:00
|
|
|
// app.use('/uploads', mw.auth, ctrl.uploads)
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use('/admin', mw.auth, ctrl.admin)
|
|
|
|
app.use('/', mw.auth, ctrl.pages)
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Error handling
|
|
|
|
// ----------------------------------------
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
var err = new Error('Not Found')
|
|
|
|
err.status = 404
|
|
|
|
next(err)
|
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
res.status(err.status || 500)
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: wiki.IS_DEBUG ? err : {}
|
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
})
|
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Start HTTP server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-09-25 03:22:33 +00:00
|
|
|
wiki.logger.info(`HTTP Server on port: ${wiki.config.port}`)
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
app.set('port', wiki.config.port)
|
2017-09-25 03:22:33 +00:00
|
|
|
let server = http.createServer(app)
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
server.listen(wiki.config.port)
|
|
|
|
server.on('error', (error) => {
|
|
|
|
if (error.syscall !== 'listen') {
|
2017-07-29 04:11:22 +00:00
|
|
|
throw error
|
2017-07-29 21:33: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!')
|
|
|
|
return process.exit(1)
|
|
|
|
case 'EADDRINUSE':
|
|
|
|
wiki.logger.error('Port ' + wiki.config.port + ' is already in use!')
|
|
|
|
return process.exit(1)
|
|
|
|
default:
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
server.on('listening', () => {
|
2017-09-25 03:22:33 +00:00
|
|
|
wiki.logger.info('HTTP Server: RUNNING')
|
2017-07-29 21:33:08 +00:00
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Graceful shutdown
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
graceful.on('exit', () => {
|
|
|
|
wiki.logger.info('- SHUTTING DOWN - Performing git sync...')
|
|
|
|
return global.git.resync().then(() => {
|
|
|
|
wiki.logger.info('- SHUTTING DOWN - Git sync successful. Now safe to exit.')
|
|
|
|
process.exit()
|
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
})
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
return true
|
2017-10-08 02:44:35 +00:00
|
|
|
}
|