2018-02-04 00:53:13 -05: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')
|
2019-04-21 02:04:00 -04:00
|
|
|
const session = require('express-session')
|
2019-05-11 01:23:32 -04:00
|
|
|
const KnexSessionStore = require('connect-session-knex')(session)
|
2018-02-04 00:53:13 -05:00
|
|
|
const favicon = require('serve-favicon')
|
|
|
|
const path = require('path')
|
2019-02-10 14:02:26 -05:00
|
|
|
const _ = require('lodash')
|
2018-02-04 00:53:13 -05:00
|
|
|
|
2018-03-05 15:49:36 -05:00
|
|
|
/* global WIKI */
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2017-12-16 23:41:16 -05:00
|
|
|
module.exports = async () => {
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
2018-02-04 00:53:13 -05:00
|
|
|
// Load core modules
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 15:49:36 -05:00
|
|
|
WIKI.auth = require('./core/auth').init()
|
|
|
|
WIKI.lang = require('./core/localization').init()
|
2018-12-22 16:18:16 -05:00
|
|
|
WIKI.mail = require('./core/mail').init()
|
2019-01-13 15:37:45 -05:00
|
|
|
WIKI.system = require('./core/system').init()
|
2017-07-29 17:33:08 -04:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2018-02-04 00:53:13 -05:00
|
|
|
// Load middlewares
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2020-10-14 18:46:27 +03:30
|
|
|
const mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares'))
|
|
|
|
const ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers'))
|
2017-07-29 17:33:08 -04:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Define Express App
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
const app = express()
|
2018-03-05 15:49:36 -05:00
|
|
|
WIKI.app = app
|
2017-07-29 17:33:08 -04:00
|
|
|
app.use(compression())
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Security
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.security)
|
2021-03-25 21:19:32 -04:00
|
|
|
app.use(cors({ origin: false }))
|
|
|
|
app.options('*', cors({ origin: false }))
|
2019-10-05 16:58:34 -04:00
|
|
|
if (WIKI.config.security.securityTrustProxy) {
|
2019-02-13 17:20:46 -05:00
|
|
|
app.enable('trust proxy')
|
|
|
|
}
|
2017-07-29 17:33:08 -04:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Public Assets
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 15:49:36 -05:00
|
|
|
app.use(favicon(path.join(WIKI.ROOTPATH, 'assets', 'favicon.ico')))
|
2020-05-24 18:02:05 -04:00
|
|
|
app.use('/_assets/svg/twemoji', async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
WIKI.asar.serve('twemoji', req, res, next)
|
|
|
|
} catch (err) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
}
|
|
|
|
})
|
2020-05-15 17:05:11 -04:00
|
|
|
app.use('/_assets', express.static(path.join(WIKI.ROOTPATH, 'assets'), {
|
2017-07-29 17:33:08 -04:00
|
|
|
index: false,
|
|
|
|
maxAge: '7d'
|
|
|
|
}))
|
|
|
|
|
2020-01-11 22:33:19 -05:00
|
|
|
// ----------------------------------------
|
2020-01-19 21:30:25 -05:00
|
|
|
// SSL Handlers
|
2020-01-11 22:33:19 -05:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2020-01-19 21:30:25 -05:00
|
|
|
app.use('/', ctrl.ssl)
|
2020-01-11 22:33:19 -05:00
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
// Passport Authentication
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(cookieParser())
|
2019-04-21 02:04:00 -04:00
|
|
|
app.use(session({
|
|
|
|
secret: WIKI.config.sessionSecret,
|
|
|
|
resave: false,
|
2019-05-11 01:23:32 -04:00
|
|
|
saveUninitialized: false,
|
|
|
|
store: new KnexSessionStore({
|
|
|
|
knex: WIKI.models.knex
|
|
|
|
})
|
2019-04-21 02:04:00 -04:00
|
|
|
}))
|
2018-03-05 15:49:36 -05:00
|
|
|
app.use(WIKI.auth.passport.initialize())
|
2019-01-06 22:03:34 -05:00
|
|
|
app.use(WIKI.auth.authenticate)
|
2017-07-29 17:33:08 -04:00
|
|
|
|
2020-01-14 22:21:24 -05:00
|
|
|
// ----------------------------------------
|
|
|
|
// GraphQL Server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2020-02-15 19:35:53 -08:00
|
|
|
app.use(bodyParser.json({ limit: '1mb' }))
|
2020-01-14 22:21:24 -05:00
|
|
|
await WIKI.servers.startGraphQL()
|
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
// SEO
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.seo)
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View Engine Setup
|
|
|
|
// ----------------------------------------
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2018-03-05 15:49:36 -05:00
|
|
|
app.set('views', path.join(WIKI.SERVERPATH, 'views'))
|
2017-07-29 17:33:08 -04:00
|
|
|
app.set('view engine', 'pug')
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2018-02-04 00:53:13 -05:00
|
|
|
// ----------------------------------------
|
|
|
|
// Localization
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 15:49:36 -05:00
|
|
|
WIKI.lang.attachMiddleware(app)
|
2018-02-04 00:53:13 -05:00
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
// View accessible data
|
|
|
|
// ----------------------------------------
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2020-01-11 22:33:19 -05:00
|
|
|
app.locals.siteConfig = {}
|
2019-07-02 01:48:19 -04:00
|
|
|
app.locals.analyticsCode = {}
|
2018-03-05 15:49:36 -05:00
|
|
|
app.locals.basedir = WIKI.ROOTPATH
|
|
|
|
app.locals.config = WIKI.config
|
2018-12-24 17:38:34 -05:00
|
|
|
app.locals.pageMeta = {
|
|
|
|
title: '',
|
|
|
|
description: WIKI.config.description,
|
|
|
|
image: '',
|
|
|
|
url: '/'
|
|
|
|
}
|
2019-12-18 23:45:19 -05:00
|
|
|
app.locals.devMode = WIKI.devMode
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2018-01-27 17:39:55 -05:00
|
|
|
// ----------------------------------------
|
|
|
|
// HMR (Dev Mode Only)
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
if (global.DEV) {
|
2018-02-03 16:48:25 -05:00
|
|
|
app.use(global.WP_DEV.devMiddleware)
|
|
|
|
app.use(global.WP_DEV.hotMiddleware)
|
2018-01-27 17:39:55 -05:00
|
|
|
}
|
|
|
|
|
2018-06-24 00:20:35 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
// Routing
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2019-06-21 20:54:09 -04:00
|
|
|
app.use(async (req, res, next) => {
|
|
|
|
res.locals.siteConfig = {
|
|
|
|
title: WIKI.config.title,
|
|
|
|
theme: WIKI.config.theming.theme,
|
|
|
|
darkMode: WIKI.config.theming.darkMode,
|
|
|
|
lang: WIKI.config.lang.code,
|
|
|
|
rtl: WIKI.config.lang.rtl,
|
2019-12-18 23:45:19 -05:00
|
|
|
company: WIKI.config.company,
|
2020-02-23 15:20:55 -05:00
|
|
|
contentLicense: WIKI.config.contentLicense,
|
2019-12-18 23:45:19 -05:00
|
|
|
logoUrl: WIKI.config.logoUrl
|
2019-06-21 20:54:09 -04:00
|
|
|
}
|
|
|
|
res.locals.langs = await WIKI.models.locales.getNavLocales({ cache: true })
|
2019-06-23 22:57:04 -04:00
|
|
|
res.locals.analyticsCode = await WIKI.models.analytics.getCode({ cache: true })
|
2019-06-21 20:54:09 -04:00
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
app.use('/', ctrl.auth)
|
2019-05-13 01:15:27 -04:00
|
|
|
app.use('/', ctrl.upload)
|
2019-01-06 22:03:34 -05:00
|
|
|
app.use('/', ctrl.common)
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
// Error handling
|
|
|
|
// ----------------------------------------
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2018-01-14 22:05:08 -05:00
|
|
|
app.use((req, res, next) => {
|
2020-10-14 18:46:27 +03:30
|
|
|
const err = new Error('Not Found')
|
2017-07-29 17:33:08 -04:00
|
|
|
err.status = 404
|
|
|
|
next(err)
|
|
|
|
})
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2018-01-14 22:05:08 -05:00
|
|
|
app.use((err, req, res, next) => {
|
2020-02-22 17:38:06 -05:00
|
|
|
if (req.path === '/graphql') {
|
|
|
|
res.status(err.status || 500).json({
|
|
|
|
data: {},
|
|
|
|
errors: [{
|
|
|
|
message: err.message,
|
|
|
|
path: []
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.status(err.status || 500)
|
|
|
|
_.set(res.locals, 'pageMeta.title', 'Error')
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: WIKI.IS_DEBUG ? err : {}
|
|
|
|
})
|
|
|
|
}
|
2017-07-29 00:11:22 -04:00
|
|
|
})
|
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
2020-01-11 22:33:19 -05:00
|
|
|
// Start HTTP Server(s)
|
2017-07-29 17:33:08 -04:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2020-01-11 22:33:19 -05:00
|
|
|
await WIKI.servers.startHTTP()
|
2017-07-29 00:11:22 -04:00
|
|
|
|
2020-01-11 22:33:19 -05:00
|
|
|
if (WIKI.config.ssl.enabled === true || WIKI.config.ssl.enabled === 'true' || WIKI.config.ssl.enabled === 1 || WIKI.config.ssl.enabled === '1') {
|
|
|
|
await WIKI.servers.startHTTPS()
|
2018-01-27 17:39:55 -05:00
|
|
|
}
|
|
|
|
|
2017-07-29 17:33:08 -04:00
|
|
|
return true
|
2017-10-07 22:44:35 -04:00
|
|
|
}
|