All files / lib/crowi express-init.ts

97.83% Statements 45/46
50% Branches 3/6
100% Functions 2/2
97.83% Lines 45/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                        16x 16x   16x 1x 1x 1x 1x 1x 1x   1x   1x 1x   1x 1x                   1x   1x 1x 1x 1x 1x 1x 1x           1x   1x     16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x     16x   16x 16x   16x   16x 16x   16x   16x 16x 16x    
import express, { Express } from 'express'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
import methodOverride from 'method-override'
import passport from 'passport'
import session from 'express-session'
import flash from 'connect-flash'
import cons from 'consolidate'
import Crowi from 'server/crowi'
 
export default (crowi: Crowi, app: Express) => {
  // const debug = Debug('crowi:crowi:express-init')
  const env = crowi.node_env
  const middlewares = crowi.middlewares
 
  app.use(function(req: any, res, next) {
    const now = new Date()
    const config = crowi.getConfig()
    const tzoffset = -(config.crowi['app:timezone'] || 9) * 60 // for date
    const Page = crowi.model('Page')
    const User = crowi.model('User')
    const Config = crowi.model('Config')
 
    app.set('tzoffset', tzoffset)
 
    req.config = config
    req.csrfToken = null
 
    let baseUrl = crowi.baseUrl
    Iif (!baseUrl) {
      baseUrl = (req.headers['x-forwarded-proto'] == 'https' ? 'https' : req.protocol) + '://' + req.get('host')
    }
 
    // FIXME:
    // This ... is accidentally working.
    // This `config` is now service/config object. (Originally, that was an object of configs)
    // And service/config has crowi object on its own property.
    // So, this config.crowi is Crowi Object and config.crowi['app:url'] means, accessing Crowi object's public property (create and assign the value)
    // It has to be fixed to set values into service/config.
    config.crowi['app:url'] = baseUrl
 
    res.locals.req = req
    res.locals.baseUrl = baseUrl
    res.locals.config = config
    res.locals.env = env
    res.locals.now = now
    res.locals.tzoffset = tzoffset
    res.locals.consts = {
      pageGrants: Page.getGrantLabels(),
      userStatus: User.getUserStatusLabels(),
      language: User.getLanguageLabels(),
      registrationMode: Config.getRegistrationModeLabels(),
    }
    res.locals.local_config = Config.getLocalconfig(config) // config for browser context
 
    next()
  })
 
  app.set('port', crowi.port)
  app.use(express.static(crowi.publicDir))
  app.engine('html', cons.swig)
  app.set('view cache', false)
  app.set('view engine', 'html')
  app.set('views', crowi.viewsDir)
  app.use(methodOverride())
  app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }))
  app.use(bodyParser.json({ limit: '50mb' }))
  app.use(cookieParser())
  app.use(session(crowi.sessionConfig))
 
  // Set basic auth middleware
  app.use(middlewares.BasicAuth)
 
  app.use(passport.initialize())
  app.use(passport.session())
 
  app.use(flash())
 
  app.use(middlewares.SwigFilters)
  app.use(middlewares.SwigFunctions)
 
  app.use(middlewares.LoginChecker)
 
  app.use(middlewares.I18next)
  app.use(middlewares.ClientContext)
  app.use(middlewares.SsrContext)
}