-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (70 loc) · 2.09 KB
/
app.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
const express = require('express');
const ehb = require('express-handlebars');
const session = require('express-session');
const path = require('path');
const mongoose = require('mongoose');
const frontendRouter = require('./routes/frontendRouter');
const apiRouter = require('./routes/apiRouter');
const cfg = require('./services/config');
require('./services/data');
const authService = require('./services/auth')(cfg);
const app = express();
app.disable('x-powered-by');
app.set('views', path.resolve(__dirname, 'views'));
app.set('view engine', 'hbs');
app.engine('hbs', ehb({
layoutsDir: './views/layouts',
defaultLayout: 'main',
extname: '.hbs',
}));
app.use(express.static(path.join(__dirname, 'public')));
// globally enable authentication
app.use(session({
saveUninitialized: true,
resave: true,
maxAge: 3600,
secure: true,
httpOnly: true,
proxy: true,
name: '200ok_session',
secret: cfg.cookieSecret,
}));
app.use(authService.initialize());
app.use(authService.session());
app.get('/login', authService.authenticate('github', {
scope: ['read:user'],
successRedirect: '/dashboard',
failureRedirect: '/login',
}));
app.get('/login/callback', authService.authenticate('github', {
successRedirect: '/dashboard',
failureRedirect: '/login',
}));
app.use('/api', express.json());
app.use('/api', apiRouter);
app.use('/', frontendRouter);
// catch-all route to forward 404 errors to error handler
app.use((req, res, next) => {
const error = new Error(`Page not found: ${req.url}`);
error.status = 404;
next(error);
});
// error handler to send proper 404 and 500 pages
app.use((err, req, res, next) => {
if (app.get('env') === 'development') console.error(err);
res.status(err.status || 500);
if (err.status === 404) {
res.render('static/404');
} else {
res.render('static/500');
}
});
const gracefulShutdown = async (e) => {
console.error(e);
await mongoose.disconnect();
process.exit();
};
process.on('SIGINT', gracefulShutdown);
process.on('uncaughtException', gracefulShutdown);
process.on('unhandledRejection', gracefulShutdown);
module.exports = app;