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.

200 lines
5.4 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(express.static(path.join(WIKI.ROOTPATH, 'assets'), {
  46. index: false,
  47. maxAge: '7d'
  48. }))
  49. // ----------------------------------------
  50. // SSL Handlers
  51. // ----------------------------------------
  52. app.use('/', ctrl.ssl)
  53. // ----------------------------------------
  54. // Passport Authentication
  55. // ----------------------------------------
  56. app.use(cookieParser())
  57. app.use(session({
  58. secret: WIKI.config.sessionSecret,
  59. resave: false,
  60. saveUninitialized: false,
  61. store: new KnexSessionStore({
  62. knex: WIKI.models.knex
  63. })
  64. }))
  65. app.use(WIKI.auth.passport.initialize())
  66. app.use(WIKI.auth.authenticate)
  67. // ----------------------------------------
  68. // GraphQL Server
  69. // ----------------------------------------
  70. app.use(bodyParser.json({ limit: '1mb' }))
  71. await WIKI.servers.startGraphQL()
  72. // ----------------------------------------
  73. // SEO
  74. // ----------------------------------------
  75. app.use(mw.seo)
  76. // ----------------------------------------
  77. // View Engine Setup
  78. // ----------------------------------------
  79. app.set('views', path.join(WIKI.SERVERPATH, 'views'))
  80. app.set('view engine', 'pug')
  81. app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
  82. // ----------------------------------------
  83. // Localization
  84. // ----------------------------------------
  85. WIKI.lang.attachMiddleware(app)
  86. // ----------------------------------------
  87. // View accessible data
  88. // ----------------------------------------
  89. app.locals.siteConfig = {}
  90. app.locals.analyticsCode = {}
  91. app.locals.basedir = WIKI.ROOTPATH
  92. app.locals.config = WIKI.config
  93. app.locals.pageMeta = {
  94. title: '',
  95. description: WIKI.config.description,
  96. image: '',
  97. url: '/'
  98. }
  99. app.locals.devMode = WIKI.devMode
  100. // ----------------------------------------
  101. // HMR (Dev Mode Only)
  102. // ----------------------------------------
  103. if (global.DEV) {
  104. app.use(global.WP_DEV.devMiddleware)
  105. app.use(global.WP_DEV.hotMiddleware)
  106. }
  107. // ----------------------------------------
  108. // Routing
  109. // ----------------------------------------
  110. app.use(async (req, res, next) => {
  111. res.locals.siteConfig = {
  112. title: WIKI.config.title,
  113. theme: WIKI.config.theming.theme,
  114. darkMode: WIKI.config.theming.darkMode,
  115. lang: WIKI.config.lang.code,
  116. rtl: WIKI.config.lang.rtl,
  117. company: WIKI.config.company,
  118. contentLicense: WIKI.config.contentLicense,
  119. logoUrl: WIKI.config.logoUrl
  120. }
  121. res.locals.langs = await WIKI.models.locales.getNavLocales({ cache: true })
  122. res.locals.analyticsCode = await WIKI.models.analytics.getCode({ cache: true })
  123. next()
  124. })
  125. app.use('/', ctrl.auth)
  126. app.use('/', ctrl.upload)
  127. app.use('/', ctrl.common)
  128. // ----------------------------------------
  129. // Error handling
  130. // ----------------------------------------
  131. app.use((req, res, next) => {
  132. var err = new Error('Not Found')
  133. err.status = 404
  134. next(err)
  135. })
  136. app.use((err, req, res, next) => {
  137. if (req.path === '/graphql') {
  138. res.status(err.status || 500).json({
  139. data: {},
  140. errors: [{
  141. message: err.message,
  142. path: []
  143. }]
  144. })
  145. } else {
  146. res.status(err.status || 500)
  147. _.set(res.locals, 'pageMeta.title', 'Error')
  148. res.render('error', {
  149. message: err.message,
  150. error: WIKI.IS_DEBUG ? err : {}
  151. })
  152. }
  153. })
  154. // ----------------------------------------
  155. // Start HTTP Server(s)
  156. // ----------------------------------------
  157. await WIKI.servers.startHTTP()
  158. if (WIKI.config.ssl.enabled === true || WIKI.config.ssl.enabled === 'true' || WIKI.config.ssl.enabled === 1 || WIKI.config.ssl.enabled === '1') {
  159. await WIKI.servers.startHTTPS()
  160. }
  161. return true
  162. }