web.js 5.9 KB

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