Skip to content

Commit

Permalink
refactor(app): move app creation to factory method
Browse files Browse the repository at this point in the history
app was untestable, so app-factory.js was created to ensure it can be tested.

closes #141
  • Loading branch information
christian-hawk committed Nov 16, 2020
1 parent 22c7134 commit 9cefc2f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 53 deletions.
58 changes: 58 additions & 0 deletions server/app-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const express = require('express')
const app = express()
const session = require('express-session')
const MemoryStore = require('memorystore')(session)
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const passport = require('passport')
const morgan = require('morgan')
const logger = require('./utils/logging')
const routes = require('./routes')
const metricsMiddleware = require('../server/utils/metrics')
const csurf = require('csurf')

class App {
createApp () {
app.use(metricsMiddleware)
app.use(morgan('short', { stream: logger.logger.stream }))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(csurf({ cookie: true }))

app.use(session({
cookie: {
maxAge: 86400000
},
store: new MemoryStore({
checkPeriod: 86400000 // prune expired entries every 24h
}),
secret: 'wtf',
resave: false,
saveUninitialized: false
}))

app.use(passport.initialize())
app.use(passport.session())
app.use('/passport', routes)

// Default error handler
// eslint-disable-next-line no-unused-vars
app.use((err, req, { redirect }, next) => {
logger.log2('error', `Unknown Error: ${err}`)
logger.log2('error', err.stack)
redirect(
`${global.basicConfig.failureRedirectUrl}?failure=An error occurred`
)
})
passport.serializeUser((user, done) => {
done(null, user)
})
passport.deserializeUser((user, done) => {
done(null, user)
})
return app
}
}

module.exports = App
59 changes: 6 additions & 53 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@

const config = require('config')
const express = require('express')
const session = require('express-session')
const MemoryStore = require('memorystore')(session)
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const passport = require('passport')
const morgan = require('morgan')
const logger = require('./utils/logging')
const misc = require('./utils/misc')
const confDiscovery = require('./utils/configDiscovery')
const routes = require('./routes')
const providers = require('./providers')
const passportFile = config.get('passportFile')
const metricsMiddleware = require('../server/utils/metrics')

const app = express()
const AppFactory = require('./app-factory')

var httpServer; var httpPort = -1
var httpServer
var httpPort = -1
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

app.use(metricsMiddleware)
app.use(morgan('short', { stream: logger.logger.stream }))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())

app.use(session({
cookie: {
maxAge: 86400000
},
store: new MemoryStore({
checkPeriod: 86400000 // prune expired entries every 24h
}),
secret: 'wtf',
resave: false,
saveUninitialized: false
}))

app.use(passport.initialize())
app.use(passport.session())
app.use('/passport', routes)

// Default error handler
// eslint-disable-next-line no-unused-vars
app.use((err, req, { redirect }, next) => {
logger.log2('error', `Unknown Error: ${err}`)
logger.log2('error', err.stack)
redirect(
`${global.basicConfig.failureRedirectUrl}?failure=An error occurred`
)
})

passport.serializeUser((user, done) => {
done(null, user)
})

passport.deserializeUser((user, done) => {
done(null, user)
})
const appFactory = new AppFactory()
const app = appFactory.createApp()

/**
* Creates express server for the first time and recreates if port changed
Expand All @@ -80,7 +33,6 @@ function recreateHttpServer (serverURI, port) {
'info', 'Server stopped accepting connections')
)
}

httpServer = app.listen(port, () => {
logger.log2('info', `Server listening on ${serverURI}:${port}`)
console.log(`Server listening on ${serverURI}:${port}`)
Expand Down Expand Up @@ -142,4 +94,5 @@ function init () {
}

module.exports = app

init()

0 comments on commit 9cefc2f

Please sign in to comment.