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.

189 lines
5.1 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. await WIKI.servers.startGraphQL()
  71. // ----------------------------------------
  72. // SEO
  73. // ----------------------------------------
  74. app.use(mw.seo)
  75. // ----------------------------------------
  76. // View Engine Setup
  77. // ----------------------------------------
  78. app.set('views', path.join(WIKI.SERVERPATH, 'views'))
  79. app.set('view engine', 'pug')
  80. app.use(bodyParser.json({ limit: '1mb' }))
  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. logoUrl: WIKI.config.logoUrl
  119. }
  120. res.locals.langs = await WIKI.models.locales.getNavLocales({ cache: true })
  121. res.locals.analyticsCode = await WIKI.models.analytics.getCode({ cache: true })
  122. next()
  123. })
  124. app.use('/', ctrl.auth)
  125. app.use('/', ctrl.upload)
  126. app.use('/', ctrl.common)
  127. // ----------------------------------------
  128. // Error handling
  129. // ----------------------------------------
  130. app.use((req, res, next) => {
  131. var err = new Error('Not Found')
  132. err.status = 404
  133. next(err)
  134. })
  135. app.use((err, req, res, next) => {
  136. res.status(err.status || 500)
  137. _.set(res.locals, 'pageMeta.title', 'Error')
  138. res.render('error', {
  139. message: err.message,
  140. error: WIKI.IS_DEBUG ? err : {}
  141. })
  142. })
  143. // ----------------------------------------
  144. // Start HTTP Server(s)
  145. // ----------------------------------------
  146. await WIKI.servers.startHTTP()
  147. if (WIKI.config.ssl.enabled === true || WIKI.config.ssl.enabled === 'true' || WIKI.config.ssl.enabled === 1 || WIKI.config.ssl.enabled === '1') {
  148. await WIKI.servers.startHTTPS()
  149. }
  150. return true
  151. }