forked from FAForever/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fafApp.js
100 lines (86 loc) · 3.02 KB
/
fafApp.js
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
89
90
91
92
93
94
95
96
97
98
99
100
const appConfig = require("./config/app")
const express = require('express')
const bodyParser = require('body-parser')
const session = require('express-session')
const FileStore = require('session-file-store')(session)
const passport = require('passport')
const flash = require('connect-flash')
const middleware = require('./routes/middleware')
const defaultRouter = require("./routes/views/defaultRouter")
const authRouter = require("./routes/views/auth")
const staticMarkdownRouter = require("./routes/views/staticMarkdownRouter")
const newsRouter = require("./routes/views/news")
const leaderboardRouter = require("./routes/views/leaderboardRouter")
const clanRouter = require("./routes/views/clanRouter")
const accountRouter = require("./routes/views/accountRouter")
const dataRouter = require('./routes/views/dataRouter');
const setupCronJobs = require("./scripts/cron-jobs")
const copyFlashHandler = (req, res, next) => {
res.locals.message = req.flash();
next();
}
const notFoundHandler = (req, res) => {
res.status(404).render('errors/404');
}
const errorHandler = (err, req, res, next) => {
console.error('[error] Incoming request to"', req.originalUrl, '"failed with error "', err.toString(), '"')
if (res.headersSent) {
return next(err);
}
res.status(500).render('errors/500');
}
module.exports.setupCronJobs = () => {
setupCronJobs()
}
module.exports.startServer = (app) => {
app.listen(appConfig.expressPort, () => {
console.log(`Express listening on port ${appConfig.expressPort}`);
});
}
module.exports.loadRouters = (app) => {
app.use('/', defaultRouter)
app.use('/', authRouter)
app.use('/', staticMarkdownRouter)
app.use('/news', newsRouter)
app.use('/leaderboards', leaderboardRouter)
app.use('/clans', clanRouter)
app.use('/account', accountRouter)
app.use('/data', dataRouter)
app.use(notFoundHandler)
app.use(errorHandler)
}
module.exports.setup = (app) => {
app.locals.clanInvitations = {}
app.set('views', 'templates/views')
app.set('view engine', 'pug')
app.set('port', appConfig.expressPort)
app.use(middleware.injectServices)
app.use(middleware.initLocals)
app.use(express.static('public', {
immutable: true,
maxAge: 4 * 60 * 60 * 1000 // 4 hours
}))
app.use('/dist', express.static('dist', {
immutable: true,
maxAge: 4 * 60 * 60 * 1000 // 4 hours, could be longer since we got cache-busting
}))
app.use(express.json())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(session({
resave: false,
saveUninitialized: true,
secret: appConfig.session.key,
store: new FileStore({
retries: 0,
ttl: appConfig.session.tokenLifespan,
secret: appConfig.session.key
})
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
app.use(middleware.username)
app.use(middleware.webpackAsset)
app.use(copyFlashHandler)
}