master.js 5.55 KB
Newer Older
1 2 3 4 5 6
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')
Nick's avatar
Nick committed
7
const session = require('express-session')
8
const KnexSessionStore = require('connect-session-knex')(session)
9 10
const favicon = require('serve-favicon')
const path = require('path')
Nick's avatar
Nick committed
11
const _ = require('lodash')
12

13
/* global WIKI */
NGPixel's avatar
NGPixel committed
14

NGPixel's avatar
NGPixel committed
15
module.exports = async () => {
16
  // ----------------------------------------
17
  // Load core modules
18 19
  // ----------------------------------------

20 21
  WIKI.auth = require('./core/auth').init()
  WIKI.lang = require('./core/localization').init()
22
  WIKI.mail = require('./core/mail').init()
23
  WIKI.system = require('./core/system').init()
24 25

  // ----------------------------------------
26
  // Load middlewares
27 28
  // ----------------------------------------

29 30
  var mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares'))
  var ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers'))
31 32 33 34 35 36

  // ----------------------------------------
  // Define Express App
  // ----------------------------------------

  const app = express()
37
  WIKI.app = app
38 39 40 41 42 43 44
  app.use(compression())

  // ----------------------------------------
  // Security
  // ----------------------------------------

  app.use(mw.security)
45 46
  app.use(cors(WIKI.config.cors))
  app.options('*', cors(WIKI.config.cors))
47
  if (WIKI.config.security.securityTrustProxy) {
48 49
    app.enable('trust proxy')
  }
50 51 52 53 54

  // ----------------------------------------
  // Public Assets
  // ----------------------------------------

55
  app.use(favicon(path.join(WIKI.ROOTPATH, 'assets', 'favicon.ico')))
56 57 58 59 60 61 62
  app.use('/_assets/svg/twemoji', async (req, res, next) => {
    try {
      WIKI.asar.serve('twemoji', req, res, next)
    } catch (err) {
      res.sendStatus(404)
    }
  })
63
  app.use('/_assets', express.static(path.join(WIKI.ROOTPATH, 'assets'), {
64 65 66 67
    index: false,
    maxAge: '7d'
  }))

NGPixel's avatar
NGPixel committed
68
  // ----------------------------------------
69
  // SSL Handlers
NGPixel's avatar
NGPixel committed
70 71
  // ----------------------------------------

72
  app.use('/', ctrl.ssl)
NGPixel's avatar
NGPixel committed
73

74 75 76 77 78
  // ----------------------------------------
  // Passport Authentication
  // ----------------------------------------

  app.use(cookieParser())
Nick's avatar
Nick committed
79 80 81
  app.use(session({
    secret: WIKI.config.sessionSecret,
    resave: false,
82 83 84 85
    saveUninitialized: false,
    store: new KnexSessionStore({
      knex: WIKI.models.knex
    })
Nick's avatar
Nick committed
86
  }))
87
  app.use(WIKI.auth.passport.initialize())
88
  app.use(WIKI.auth.authenticate)
89

NGPixel's avatar
NGPixel committed
90 91 92 93
  // ----------------------------------------
  // GraphQL Server
  // ----------------------------------------

94
  app.use(bodyParser.json({ limit: '1mb' }))
NGPixel's avatar
NGPixel committed
95 96
  await WIKI.servers.startGraphQL()

97 98 99 100 101 102 103 104 105
  // ----------------------------------------
  // SEO
  // ----------------------------------------

  app.use(mw.seo)

  // ----------------------------------------
  // View Engine Setup
  // ----------------------------------------
NGPixel's avatar
NGPixel committed
106

107
  app.set('views', path.join(WIKI.SERVERPATH, 'views'))
108
  app.set('view engine', 'pug')
NGPixel's avatar
NGPixel committed
109

110
  app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
NGPixel's avatar
NGPixel committed
111

112 113 114 115
  // ----------------------------------------
  // Localization
  // ----------------------------------------

116
  WIKI.lang.attachMiddleware(app)
117

118 119 120
  // ----------------------------------------
  // View accessible data
  // ----------------------------------------
NGPixel's avatar
NGPixel committed
121

NGPixel's avatar
NGPixel committed
122
  app.locals.siteConfig = {}
123
  app.locals.analyticsCode = {}
124 125
  app.locals.basedir = WIKI.ROOTPATH
  app.locals.config = WIKI.config
126 127 128 129 130 131
  app.locals.pageMeta = {
    title: '',
    description: WIKI.config.description,
    image: '',
    url: '/'
  }
132
  app.locals.devMode = WIKI.devMode
NGPixel's avatar
NGPixel committed
133

NGPixel's avatar
NGPixel committed
134 135 136 137 138
  // ----------------------------------------
  // HMR (Dev Mode Only)
  // ----------------------------------------

  if (global.DEV) {
139 140
    app.use(global.WP_DEV.devMiddleware)
    app.use(global.WP_DEV.hotMiddleware)
NGPixel's avatar
NGPixel committed
141 142
  }

143 144
  // ----------------------------------------
  // Routing
145
  // ----------------------------------------
NGPixel's avatar
NGPixel committed
146

147 148 149 150 151 152 153
  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,
154
      company: WIKI.config.company,
155
      contentLicense: WIKI.config.contentLicense,
156
      logoUrl: WIKI.config.logoUrl
157 158
    }
    res.locals.langs = await WIKI.models.locales.getNavLocales({ cache: true })
Nick's avatar
Nick committed
159
    res.locals.analyticsCode = await WIKI.models.analytics.getCode({ cache: true })
160 161 162
    next()
  })

163
  app.use('/', ctrl.auth)
164
  app.use('/', ctrl.upload)
165
  app.use('/', ctrl.common)
NGPixel's avatar
NGPixel committed
166

167 168 169
  // ----------------------------------------
  // Error handling
  // ----------------------------------------
NGPixel's avatar
NGPixel committed
170

171
  app.use((req, res, next) => {
172 173 174 175
    var err = new Error('Not Found')
    err.status = 404
    next(err)
  })
NGPixel's avatar
NGPixel committed
176

177
  app.use((err, req, res, next) => {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    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 : {}
      })
    }
NGPixel's avatar
NGPixel committed
194 195
  })

196
  // ----------------------------------------
NGPixel's avatar
NGPixel committed
197
  // Start HTTP Server(s)
198 199
  // ----------------------------------------

NGPixel's avatar
NGPixel committed
200
  await WIKI.servers.startHTTP()
NGPixel's avatar
NGPixel committed
201

NGPixel's avatar
NGPixel committed
202 203
  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()
NGPixel's avatar
NGPixel committed
204 205
  }

206
  return true
NGPixel's avatar
NGPixel committed
207
}