2018-02-04 05:53:13 +00:00
|
|
|
const autoload = require('auto-load')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const compression = require('compression')
|
|
|
|
const cookieParser = require('cookie-parser')
|
|
|
|
const cors = require('cors')
|
|
|
|
const express = require('express')
|
|
|
|
const favicon = require('serve-favicon')
|
|
|
|
const http = require('http')
|
2019-01-30 06:30:05 +00:00
|
|
|
const https = require('https')
|
2018-02-04 05:53:13 +00:00
|
|
|
const path = require('path')
|
2018-06-24 04:20:35 +00:00
|
|
|
const { ApolloServer } = require('apollo-server-express')
|
2018-03-05 22:27:30 +00:00
|
|
|
// const oauth2orize = require('oauth2orize')
|
2018-02-04 05:53:13 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-12-17 04:41:16 +00:00
|
|
|
module.exports = async () => {
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
2018-02-04 05:53:13 +00:00
|
|
|
// Load core modules
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.auth = require('./core/auth').init()
|
|
|
|
WIKI.lang = require('./core/localization').init()
|
2018-12-22 21:18:16 +00:00
|
|
|
WIKI.mail = require('./core/mail').init()
|
2019-01-13 20:37:45 +00:00
|
|
|
WIKI.system = require('./core/system').init()
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2018-02-04 05:53:13 +00:00
|
|
|
// Load middlewares
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
var mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares'))
|
|
|
|
var ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers'))
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Define Express App
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
const app = express()
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.app = app
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use(compression())
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Security
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.security)
|
2018-03-05 20:49:36 +00:00
|
|
|
app.use(cors(WIKI.config.cors))
|
|
|
|
app.options('*', cors(WIKI.config.cors))
|
2018-01-28 05:40:25 +00:00
|
|
|
app.enable('trust proxy')
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Public Assets
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.use(favicon(path.join(WIKI.ROOTPATH, 'assets', 'favicon.ico')))
|
|
|
|
app.use(express.static(path.join(WIKI.ROOTPATH, 'assets'), {
|
2017-07-29 21:33:08 +00:00
|
|
|
index: false,
|
|
|
|
maxAge: '7d'
|
|
|
|
}))
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// OAuth2 Server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 22:27:30 +00:00
|
|
|
// const OAuth2Server = oauth2orize.createServer()
|
2018-03-05 20:49:36 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Passport Authentication
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(cookieParser())
|
2018-03-05 20:49:36 +00:00
|
|
|
app.use(WIKI.auth.passport.initialize())
|
2019-01-07 03:03:34 +00:00
|
|
|
app.use(WIKI.auth.authenticate)
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// SEO
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.seo)
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View Engine Setup
|
|
|
|
// ----------------------------------------
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.set('views', path.join(WIKI.SERVERPATH, 'views'))
|
2017-07-29 21:33:08 +00:00
|
|
|
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
|
|
|
|
2018-02-04 05:53:13 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Localization
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.lang.attachMiddleware(app)
|
2018-02-04 05:53:13 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// View accessible data
|
|
|
|
// ----------------------------------------
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.locals.basedir = WIKI.ROOTPATH
|
2017-07-29 21:33:08 +00:00
|
|
|
app.locals._ = require('lodash')
|
|
|
|
app.locals.moment = require('moment')
|
2018-06-11 03:23:09 +00:00
|
|
|
app.locals.moment.locale(WIKI.config.lang.code)
|
2018-03-05 20:49:36 +00:00
|
|
|
app.locals.config = WIKI.config
|
2018-12-24 22:38:34 +00:00
|
|
|
app.locals.pageMeta = {
|
|
|
|
title: '',
|
|
|
|
description: WIKI.config.description,
|
|
|
|
image: '',
|
|
|
|
url: '/'
|
|
|
|
}
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2018-01-27 22:39:55 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// HMR (Dev Mode Only)
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
if (global.DEV) {
|
2018-02-03 21:48:25 +00:00
|
|
|
app.use(global.WP_DEV.devMiddleware)
|
|
|
|
app.use(global.WP_DEV.hotMiddleware)
|
2018-01-27 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
2018-06-24 04:20:35 +00:00
|
|
|
// Apollo Server (GraphQL)
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
const graphqlSchema = require('./graph')
|
|
|
|
const apolloServer = new ApolloServer({
|
|
|
|
...graphqlSchema,
|
2018-09-30 18:20:26 +00:00
|
|
|
context: ({ req, res }) => ({ req, res }),
|
|
|
|
subscriptions: {
|
|
|
|
onConnect: (connectionParams, webSocket) => {
|
|
|
|
|
|
|
|
},
|
|
|
|
path: '/graphql-subscriptions'
|
|
|
|
}
|
2018-06-24 04:20:35 +00:00
|
|
|
})
|
|
|
|
apolloServer.applyMiddleware({ app })
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Routing
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
app.use('/', ctrl.auth)
|
2019-01-07 03:03:34 +00:00
|
|
|
app.use('/', ctrl.common)
|
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
|
|
|
|
2018-01-15 03:05:08 +00:00
|
|
|
app.use((req, res, next) => {
|
2017-07-29 21:33:08 +00:00
|
|
|
var err = new Error('Not Found')
|
|
|
|
err.status = 404
|
|
|
|
next(err)
|
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2018-01-15 03:05:08 +00:00
|
|
|
app.use((err, req, res, next) => {
|
2017-07-29 21:33:08 +00:00
|
|
|
res.status(err.status || 500)
|
2019-01-25 22:30:31 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Error')
|
2017-07-29 21:33:08 +00:00
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
2018-03-05 20:49:36 +00:00
|
|
|
error: WIKI.IS_DEBUG ? err : {}
|
2017-07-29 21:33:08 +00:00
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
})
|
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
2018-02-04 05:53:13 +00:00
|
|
|
// HTTP server
|
2017-07-29 21:33:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-01-27 22:39:55 +00:00
|
|
|
let srvConnections = {}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.set('port', WIKI.config.port)
|
2019-01-30 06:30:05 +00:00
|
|
|
if (WIKI.config.ssl.enabled) {
|
|
|
|
WIKI.logger.info(`HTTPS Server on port: [ ${WIKI.config.port} ]`)
|
|
|
|
const tlsOpts = {}
|
|
|
|
try {
|
|
|
|
if (WIKI.config.ssl.format === 'pem') {
|
|
|
|
tlsOpts.key = fs.readFileSync(WIKI.config.ssl.key)
|
|
|
|
tlsOpts.cert = fs.readFileSync(WIKI.config.ssl.cert)
|
|
|
|
} else {
|
|
|
|
tlsOpts.pfx = fs.readFileSync(WIKI.config.ssl.pfx)
|
|
|
|
}
|
|
|
|
if (!_.isEmpty(WIKI.config.ssl.passphrase)) {
|
|
|
|
tlsOpts.passphrase = WIKI.config.ssl.passphrase
|
|
|
|
}
|
|
|
|
if (!_.isEmpty(WIKI.config.ssl.dhparam)) {
|
|
|
|
tlsOpts.dhparam = WIKI.config.ssl.dhparam
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error('Failed to setup HTTPS server parameters:')
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
return process.exit(1)
|
|
|
|
}
|
|
|
|
WIKI.server = https.createServer(tlsOpts, app)
|
|
|
|
} else {
|
|
|
|
WIKI.logger.info(`HTTP Server on port: [ ${WIKI.config.port} ]`)
|
|
|
|
WIKI.server = http.createServer(app)
|
|
|
|
}
|
2018-09-30 18:20:26 +00:00
|
|
|
apolloServer.installSubscriptionHandlers(WIKI.server)
|
2017-07-29 21:33:08 +00:00
|
|
|
|
2018-09-01 03:42:14 +00:00
|
|
|
WIKI.server.listen(WIKI.config.port, WIKI.config.bindIP)
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.on('error', (error) => {
|
2017-07-29 21:33:08 +00:00
|
|
|
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':
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error('Listening on port ' + WIKI.config.port + ' requires elevated privileges!')
|
2017-07-29 21:33:08 +00:00
|
|
|
return process.exit(1)
|
|
|
|
case 'EADDRINUSE':
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error('Port ' + WIKI.config.port + ' is already in use!')
|
2017-07-29 21:33:08 +00:00
|
|
|
return process.exit(1)
|
|
|
|
default:
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.on('connection', conn => {
|
2018-01-27 22:39:55 +00:00
|
|
|
let key = `${conn.remoteAddress}:${conn.remotePort}`
|
|
|
|
srvConnections[key] = conn
|
|
|
|
conn.on('close', function() {
|
|
|
|
delete srvConnections[key]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.on('listening', () => {
|
2019-01-30 06:30:05 +00:00
|
|
|
if (WIKI.config.ssl.enabled) {
|
|
|
|
WIKI.logger.info('HTTPS Server: [ RUNNING ]')
|
|
|
|
} else {
|
|
|
|
WIKI.logger.info('HTTP Server: [ RUNNING ]')
|
|
|
|
}
|
2017-07-29 21:33:08 +00:00
|
|
|
})
|
2017-07-29 04:11:22 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.destroy = (cb) => {
|
|
|
|
WIKI.server.close(cb)
|
2018-01-27 22:39:55 +00:00
|
|
|
for (let key in srvConnections) {
|
|
|
|
srvConnections[key].destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
return true
|
2017-10-08 02:44:35 +00:00
|
|
|
}
|