mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
266 lines
7.2 KiB
266 lines
7.2 KiB
2 years ago
|
import bodyParser from 'body-parser'
|
||
|
import compression from 'compression'
|
||
|
import cookieParser from 'cookie-parser'
|
||
|
import cors from 'cors'
|
||
|
import express from 'express'
|
||
|
import session from 'express-session'
|
||
|
import KnexSessionStore from 'connect-session-knex'
|
||
|
import favicon from 'serve-favicon'
|
||
|
import path from 'node:path'
|
||
|
import { set } from 'lodash-es'
|
||
|
|
||
|
import auth from './core/auth.mjs'
|
||
|
import mail from './core/mail.mjs'
|
||
|
import system from './core/system.mjs'
|
||
|
|
||
|
import ctrlAuth from './controllers/auth.mjs'
|
||
|
import ctrlCommon from './controllers/common.mjs'
|
||
|
import ctrlSsl from './controllers/ssl.mjs'
|
||
|
import ctrlWs from './controllers/ws.mjs'
|
||
|
|
||
|
export async function init () {
|
||
7 years ago
|
// ----------------------------------------
|
||
7 years ago
|
// Load core modules
|
||
7 years ago
|
// ----------------------------------------
|
||
|
|
||
2 years ago
|
WIKI.auth = auth.init()
|
||
|
WIKI.mail = mail.init()
|
||
|
WIKI.system = system.init()
|
||
7 years ago
|
|
||
|
// ----------------------------------------
|
||
|
// Define Express App
|
||
|
// ----------------------------------------
|
||
|
|
||
|
const app = express()
|
||
7 years ago
|
WIKI.app = app
|
||
7 years ago
|
app.use(compression())
|
||
|
|
||
2 years ago
|
// ----------------------------------------
|
||
|
// Initialize HTTP/HTTPS Server
|
||
|
// ----------------------------------------
|
||
|
|
||
|
const useHTTPS = WIKI.config.ssl.enabled === true || WIKI.config.ssl.enabled === 'true' || WIKI.config.ssl.enabled === 1 || WIKI.config.ssl.enabled === '1'
|
||
|
|
||
|
await WIKI.servers.initHTTP()
|
||
|
if (useHTTPS) {
|
||
|
await WIKI.servers.initHTTPS()
|
||
|
}
|
||
|
await WIKI.servers.initWebSocket()
|
||
|
|
||
|
// ----------------------------------------
|
||
|
// Attach WebSocket Server
|
||
|
// ----------------------------------------
|
||
|
|
||
2 years ago
|
ctrlWs()
|
||
2 years ago
|
|
||
7 years ago
|
// ----------------------------------------
|
||
|
// Security
|
||
|
// ----------------------------------------
|
||
|
|
||
2 years ago
|
app.use((req, res, next) => {
|
||
|
// -> Disable X-Powered-By
|
||
|
req.app.disable('x-powered-by')
|
||
|
|
||
|
// -> Disable Frame Embedding
|
||
|
if (WIKI.config.security.securityIframe) {
|
||
|
res.set('X-Frame-Options', 'deny')
|
||
|
}
|
||
|
|
||
|
// -> Re-enable XSS Fitler if disabled
|
||
|
res.set('X-XSS-Protection', '1; mode=block')
|
||
|
|
||
|
// -> Disable MIME-sniffing
|
||
|
res.set('X-Content-Type-Options', 'nosniff')
|
||
|
|
||
|
// -> Disable IE Compatibility Mode
|
||
|
res.set('X-UA-Compatible', 'IE=edge')
|
||
|
|
||
|
// -> Disables referrer header when navigating to a different origin
|
||
|
if (WIKI.config.security.securityReferrerPolicy) {
|
||
|
res.set('Referrer-Policy', 'same-origin')
|
||
|
}
|
||
|
|
||
|
// -> Enforce HSTS
|
||
|
if (WIKI.config.security.securityHSTS) {
|
||
|
res.set('Strict-Transport-Security', `max-age=${WIKI.config.security.securityHSTSDuration}; includeSubDomains`)
|
||
|
}
|
||
|
|
||
|
// -> Prevent Open Redirect from user provided URL
|
||
|
if (WIKI.config.security.securityOpenRedirect) {
|
||
|
// Strips out all repeating / character in the provided URL
|
||
|
req.url = req.url.replace(/(\/)(?=\/*\1)/g, '')
|
||
|
}
|
||
|
|
||
|
next()
|
||
|
})
|
||
4 years ago
|
app.use(cors({ origin: false }))
|
||
|
app.options('*', cors({ origin: false }))
|
||
5 years ago
|
if (WIKI.config.security.securityTrustProxy) {
|
||
6 years ago
|
app.enable('trust proxy')
|
||
|
}
|
||
7 years ago
|
|
||
|
// ----------------------------------------
|
||
|
// Public Assets
|
||
|
// ----------------------------------------
|
||
|
|
||
7 years ago
|
app.use(favicon(path.join(WIKI.ROOTPATH, 'assets', 'favicon.ico')))
|
||
2 years ago
|
app.use('/_assets', express.static(path.join(WIKI.ROOTPATH, 'assets/_assets'), {
|
||
|
index: false,
|
||
|
maxAge: '7d'
|
||
|
}))
|
||
2 years ago
|
app.use('/_assets/svg/twemoji', async (req, res, next) => {
|
||
5 years ago
|
try {
|
||
|
WIKI.asar.serve('twemoji', req, res, next)
|
||
|
} catch (err) {
|
||
|
res.sendStatus(404)
|
||
|
}
|
||
|
})
|
||
7 years ago
|
|
||
5 years ago
|
// ----------------------------------------
|
||
5 years ago
|
// SSL Handlers
|
||
5 years ago
|
// ----------------------------------------
|
||
|
|
||
2 years ago
|
app.use('/', ctrlSsl())
|
||
5 years ago
|
|
||
7 years ago
|
// ----------------------------------------
|
||
|
// Passport Authentication
|
||
|
// ----------------------------------------
|
||
|
|
||
|
app.use(cookieParser())
|
||
6 years ago
|
app.use(session({
|
||
3 years ago
|
secret: WIKI.config.auth.secret,
|
||
6 years ago
|
resave: false,
|
||
6 years ago
|
saveUninitialized: false,
|
||
2 years ago
|
store: new KnexSessionStore(session)({
|
||
2 years ago
|
knex: WIKI.db.knex
|
||
6 years ago
|
})
|
||
6 years ago
|
}))
|
||
7 years ago
|
app.use(WIKI.auth.passport.initialize())
|
||
6 years ago
|
app.use(WIKI.auth.authenticate)
|
||
7 years ago
|
|
||
5 years ago
|
// ----------------------------------------
|
||
|
// GraphQL Server
|
||
|
// ----------------------------------------
|
||
|
|
||
3 years ago
|
app.use(bodyParser.json({ limit: WIKI.config.bodyParserLimit || '1mb' }))
|
||
5 years ago
|
await WIKI.servers.startGraphQL()
|
||
|
|
||
7 years ago
|
// ----------------------------------------
|
||
|
// SEO
|
||
|
// ----------------------------------------
|
||
|
|
||
2 years ago
|
app.use((req, res, next) => {
|
||
|
if (req.path.length > 1 && req.path.endsWith('/')) {
|
||
|
let query = req.url.slice(req.path.length) || ''
|
||
|
res.redirect(301, req.path.slice(0, -1) + query)
|
||
|
} else {
|
||
|
set(res.locals, 'pageMeta.url', `${WIKI.config.host}${req.path}`)
|
||
|
next()
|
||
|
}
|
||
|
})
|
||
7 years ago
|
|
||
|
// ----------------------------------------
|
||
|
// View Engine Setup
|
||
|
// ----------------------------------------
|
||
7 years ago
|
|
||
7 years ago
|
app.set('views', path.join(WIKI.SERVERPATH, 'views'))
|
||
7 years ago
|
app.set('view engine', 'pug')
|
||
7 years ago
|
|
||
7 years ago
|
app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
|
||
7 years ago
|
|
||
7 years ago
|
// ----------------------------------------
|
||
|
// View accessible data
|
||
|
// ----------------------------------------
|
||
7 years ago
|
|
||
5 years ago
|
app.locals.analyticsCode = {}
|
||
7 years ago
|
app.locals.basedir = WIKI.ROOTPATH
|
||
|
app.locals.config = WIKI.config
|
||
6 years ago
|
app.locals.pageMeta = {
|
||
|
title: '',
|
||
|
description: WIKI.config.description,
|
||
|
image: '',
|
||
|
url: '/'
|
||
|
}
|
||
5 years ago
|
app.locals.devMode = WIKI.devMode
|
||
7 years ago
|
|
||
7 years ago
|
// ----------------------------------------
|
||
|
// HMR (Dev Mode Only)
|
||
|
// ----------------------------------------
|
||
|
|
||
|
if (global.DEV) {
|
||
7 years ago
|
app.use(global.WP_DEV.devMiddleware)
|
||
|
app.use(global.WP_DEV.hotMiddleware)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// ----------------------------------------
|
||
|
// Routing
|
||
7 years ago
|
// ----------------------------------------
|
||
7 years ago
|
|
||
6 years ago
|
app.use(async (req, res, next) => {
|
||
2 years ago
|
const currentSite = await WIKI.db.sites.getSiteByHostname({ hostname: req.hostname })
|
||
2 years ago
|
if (!currentSite) {
|
||
|
return res.status(404).send('Site Not Found')
|
||
|
}
|
||
|
|
||
6 years ago
|
res.locals.siteConfig = {
|
||
2 years ago
|
id: currentSite.id,
|
||
|
title: currentSite.config.title,
|
||
|
darkMode: currentSite.config.theme.dark,
|
||
|
lang: currentSite.config.locale,
|
||
|
rtl: false, // TODO: handle RTL
|
||
|
company: currentSite.config.company,
|
||
2 years ago
|
contentLicense: currentSite.config.contentLicense
|
||
2 years ago
|
}
|
||
|
res.locals.theming = {
|
||
|
|
||
6 years ago
|
}
|
||
2 years ago
|
res.locals.langs = await WIKI.db.locales.getNavLocales({ cache: true })
|
||
|
res.locals.analyticsCode = await WIKI.db.analytics.getCode({ cache: true })
|
||
6 years ago
|
next()
|
||
|
})
|
||
|
|
||
2 years ago
|
app.use('/', ctrlAuth())
|
||
|
app.use('/', ctrlCommon())
|
||
7 years ago
|
|
||
7 years ago
|
// ----------------------------------------
|
||
|
// Error handling
|
||
|
// ----------------------------------------
|
||
7 years ago
|
|
||
7 years ago
|
app.use((req, res, next) => {
|
||
4 years ago
|
const err = new Error('Not Found')
|
||
7 years ago
|
err.status = 404
|
||
|
next(err)
|
||
|
})
|
||
7 years ago
|
|
||
7 years ago
|
app.use((err, req, res, next) => {
|
||
2 years ago
|
if (req.path === '/_graphql') {
|
||
5 years ago
|
res.status(err.status || 500).json({
|
||
|
data: {},
|
||
|
errors: [{
|
||
|
message: err.message,
|
||
|
path: []
|
||
|
}]
|
||
|
})
|
||
|
} else {
|
||
|
res.status(err.status || 500)
|
||
2 years ago
|
set(res.locals, 'pageMeta.title', 'Error')
|
||
5 years ago
|
res.render('error', {
|
||
|
message: err.message,
|
||
|
error: WIKI.IS_DEBUG ? err : {}
|
||
|
})
|
||
|
}
|
||
7 years ago
|
})
|
||
|
|
||
7 years ago
|
// ----------------------------------------
|
||
5 years ago
|
// Start HTTP Server(s)
|
||
7 years ago
|
// ----------------------------------------
|
||
|
|
||
5 years ago
|
await WIKI.servers.startHTTP()
|
||
7 years ago
|
|
||
2 years ago
|
if (useHTTPS) {
|
||
5 years ago
|
await WIKI.servers.startHTTPS()
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
return true
|
||
7 years ago
|
}
|