-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): move app creation to factory method
app was untestable, so app-factory.js was created to ensure it can be tested. closes #141
- Loading branch information
1 parent
22c7134
commit 9cefc2f
Showing
2 changed files
with
64 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters