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.

207 lines
5.5 KiB

  1. const autoload = require('auto-load')
  2. const bodyParser = require('body-parser')
  3. const compression = require('compression')
  4. const cookieParser = require('cookie-parser')
  5. const cors = require('cors')
  6. const express = require('express')
  7. const session = require('express-session')
  8. const KnexSessionStore = require('connect-session-knex')(session)
  9. const favicon = require('serve-favicon')
  10. const path = require('path')
  11. const _ = require('lodash')
  12. /* global WIKI */
  13. module.exports = async () => {
  14. // ----------------------------------------
  15. // Load core modules
  16. // ----------------------------------------
  17. WIKI.auth = require('./core/auth').init()
  18. WIKI.lang = require('./core/localization').init()
  19. WIKI.mail = require('./core/mail').init()
  20. WIKI.system = require('./core/system').init()
  21. // ----------------------------------------
  22. // Load middlewares
  23. // ----------------------------------------
  24. var mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares'))
  25. var ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers'))
  26. // ----------------------------------------
  27. // Define Express App
  28. // ----------------------------------------
  29. const app = express()
  30. WIKI.app = app
  31. app.use(compression())
  32. // ----------------------------------------
  33. // Security
  34. // ----------------------------------------
  35. app.use(mw.security)
  36. app.use(cors(WIKI.config.cors))
  37. app.options('*', cors(WIKI.config.cors))
  38. if (WIKI.config.security.securityTrustProxy) {
  39. app.enable('trust proxy')
  40. }
  41. // ----------------------------------------
  42. // Public Assets
  43. // ----------------------------------------
  44. app.use(favicon(path.join(WIKI.ROOTPATH, 'assets', 'favicon.ico')))
  45. app.use('/_assets/svg/twemoji', async (req, res, next) => {
  46. try {
  47. WIKI.asar.serve('twemoji', req, res, next)
  48. } catch (err) {
  49. res.sendStatus(404)
  50. }
  51. })
  52. app.use('/_assets', express.static(path.join(WIKI.ROOTPATH, 'assets'), {
  53. index: false,
  54. maxAge: '7d'
  55. }))
  56. // ----------------------------------------
  57. // SSL Handlers
  58. // ----------------------------------------
  59. app.use('/', ctrl.ssl)
  60. // ----------------------------------------
  61. // Passport Authentication
  62. // ----------------------------------------
  63. app.use(cookieParser())
  64. app.use(session({
  65. secret: WIKI.config.sessionSecret,
  66. resave: false,
  67. saveUninitialized: false,
  68. store: new KnexSessionStore({
  69. knex: WIKI.models.knex
  70. })
  71. }))
  72. app.use(WIKI.auth.passport.initialize())
  73. app.use(WIKI.auth.authenticate)
  74. // ----------------------------------------
  75. // GraphQL Server
  76. // ----------------------------------------
  77. app.use(bodyParser.json({ limit: '1mb' }))
  78. await WIKI.servers.startGraphQL()
  79. // ----------------------------------------
  80. // SEO
  81. // ----------------------------------------
  82. app.use(mw.seo)
  83. // ----------------------------------------
  84. // View Engine Setup
  85. // ----------------------------------------
  86. app.set('views', path.join(WIKI.SERVERPATH, 'views'))
  87. app.set('view engine', 'pug')
  88. app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
  89. // ----------------------------------------
  90. // Localization
  91. // ----------------------------------------
  92. WIKI.lang.attachMiddleware(app)
  93. // ----------------------------------------
  94. // View accessible data
  95. // ----------------------------------------
  96. app.locals.siteConfig = {}
  97. app.locals.analyticsCode = {}
  98. app.locals.basedir = WIKI.ROOTPATH
  99. app.locals.config = WIKI.config
  100. app.locals.pageMeta = {
  101. title: '',
  102. description: WIKI.config.description,
  103. image: '',
  104. url: '/'
  105. }
  106. app.locals.devMode = WIKI.devMode
  107. // ----------------------------------------
  108. // HMR (Dev Mode Only)
  109. // ----------------------------------------
  110. if (global.DEV) {
  111. app.use(global.WP_DEV.devMiddleware)
  112. app.use(global.WP_DEV.hotMiddleware)
  113. }
  114. // ----------------------------------------
  115. // Routing
  116. // ----------------------------------------
  117. app.use(async (req, res, next) => {
  118. res.locals.siteConfig = {
  119. title: WIKI.config.title,
  120. theme: WIKI.config.theming.theme,
  121. darkMode: WIKI.config.theming.darkMode,
  122. lang: WIKI.config.lang.code,
  123. rtl: WIKI.config.lang.rtl,
  124. company: WIKI.config.company,
  125. contentLicense: WIKI.config.contentLicense,
  126. logoUrl: WIKI.config.logoUrl
  127. }
  128. res.locals.langs = await WIKI.models.locales.getNavLocales({ cache: true })
  129. res.locals.analyticsCode = await WIKI.models.analytics.getCode({ cache: true })
  130. next()
  131. })
  132. app.use('/', ctrl.auth)
  133. app.use('/', ctrl.upload)
  134. app.use('/', ctrl.common)
  135. // ----------------------------------------
  136. // Error handling
  137. // ----------------------------------------
  138. app.use((req, res, next) => {
  139. var err = new Error('Not Found')
  140. err.status = 404
  141. next(err)
  142. })
  143. app.use((err, req, res, next) => {
  144. if (req.path === '/graphql') {
  145. res.status(err.status || 500).json({
  146. data: {},
  147. errors: [{
  148. message: err.message,
  149. path: []
  150. }]
  151. })
  152. } else {
  153. res.status(err.status || 500)
  154. _.set(res.locals, 'pageMeta.title', 'Error')
  155. res.render('error', {
  156. message: err.message,
  157. error: WIKI.IS_DEBUG ? err : {}
  158. })
  159. }
  160. })
  161. // ----------------------------------------
  162. // Start HTTP Server(s)
  163. // ----------------------------------------
  164. await WIKI.servers.startHTTP()
  165. if (WIKI.config.ssl.enabled === true || WIKI.config.ssl.enabled === 'true' || WIKI.config.ssl.enabled === 1 || WIKI.config.ssl.enabled === '1') {
  166. await WIKI.servers.startHTTPS()
  167. }
  168. return true
  169. }