wikijs-fork/server/master.js

190 lines
5.1 KiB
JavaScript
Raw Normal View History

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 06:04:00 +00:00
const session = require('express-session')
const KnexSessionStore = require('connect-session-knex')(session)
const favicon = require('serve-favicon')
const path = require('path')
2019-02-10 19:02:26 +00:00
const _ = require('lodash')
/* 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
// ----------------------------------------
// Load core modules
2017-07-29 21:33:08 +00:00
// ----------------------------------------
WIKI.auth = require('./core/auth').init()
WIKI.lang = require('./core/localization').init()
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
// ----------------------------------------
// Load middlewares
2017-07-29 21:33:08 +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()
WIKI.app = app
2017-07-29 21:33:08 +00:00
app.use(compression())
// ----------------------------------------
// Security
// ----------------------------------------
app.use(mw.security)
app.use(cors(WIKI.config.cors))
app.options('*', cors(WIKI.config.cors))
if (WIKI.config.security.securityTrustProxy) {
app.enable('trust proxy')
}
2017-07-29 21:33:08 +00:00
// ----------------------------------------
// Public Assets
// ----------------------------------------
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'
}))
2020-01-12 03:33:19 +00:00
// ----------------------------------------
// SSL Handlers
2020-01-12 03:33:19 +00:00
// ----------------------------------------
app.use('/', ctrl.ssl)
2020-01-12 03:33:19 +00:00
2017-07-29 21:33:08 +00:00
// ----------------------------------------
// Passport Authentication
// ----------------------------------------
app.use(cookieParser())
2019-04-21 06:04:00 +00:00
app.use(session({
secret: WIKI.config.sessionSecret,
resave: false,
saveUninitialized: false,
store: new KnexSessionStore({
knex: WIKI.models.knex
})
2019-04-21 06:04:00 +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
2020-01-15 03:21:24 +00:00
// ----------------------------------------
// GraphQL Server
// ----------------------------------------
await WIKI.servers.startGraphQL()
2017-07-29 21:33:08 +00:00
// ----------------------------------------
// SEO
// ----------------------------------------
app.use(mw.seo)
// ----------------------------------------
// View Engine Setup
// ----------------------------------------
2017-07-29 04:11:22 +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
// ----------------------------------------
// Localization
// ----------------------------------------
WIKI.lang.attachMiddleware(app)
2017-07-29 21:33:08 +00:00
// ----------------------------------------
// View accessible data
// ----------------------------------------
2017-07-29 04:11:22 +00:00
2020-01-12 03:33:19 +00:00
app.locals.siteConfig = {}
2019-07-02 05:48:19 +00:00
app.locals.analyticsCode = {}
app.locals.basedir = WIKI.ROOTPATH
app.locals.config = WIKI.config
2018-12-24 22:38:34 +00:00
app.locals.pageMeta = {
title: '',
description: WIKI.config.description,
image: '',
url: '/'
}
app.locals.devMode = WIKI.devMode
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
}
// ----------------------------------------
// Routing
2017-07-29 21:33:08 +00:00
// ----------------------------------------
2017-07-29 04:11:22 +00:00
2019-06-22 00:54:09 +00: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,
company: WIKI.config.company,
logoUrl: WIKI.config.logoUrl
2019-06-22 00:54:09 +00:00
}
res.locals.langs = await WIKI.models.locales.getNavLocales({ cache: true })
2019-06-24 02:57:04 +00:00
res.locals.analyticsCode = await WIKI.models.analytics.getCode({ cache: true })
2019-06-22 00:54:09 +00:00
next()
})
2017-07-29 21:33:08 +00:00
app.use('/', ctrl.auth)
app.use('/', ctrl.upload)
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
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
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,
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
// ----------------------------------------
2020-01-12 03:33:19 +00:00
// Start HTTP Server(s)
2017-07-29 21:33:08 +00:00
// ----------------------------------------
2020-01-12 03:33:19 +00:00
await WIKI.servers.startHTTP()
2017-07-29 04:11:22 +00:00
2020-01-12 03:33:19 +00: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 22:39:55 +00:00
}
2017-07-29 21:33:08 +00:00
return true
2017-10-08 02:44:35 +00:00
}