-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
90 lines (68 loc) · 2.35 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
88
89
90
process.env.DEBUG_FD = 1; // debug() echoes to stdout instead of stderr
const express = require('express');
const bodyParser = require('body-parser');
const compression = require('compression');
const cors = require('cors');
const jwt = require('express-jwt');
const config = require('./config');
const database = require('./dao/database');
const notificationsSender = require('./business/notifications');
database.init(config.profile);
notificationsSender.start();
const app = express();
const API_ROOT = config.apiRoot;
app.use(compression());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.options('*', cors()); // include before other routes
// first check if the requested resource is the login.
app.post(`${API_ROOT}/login`, require('./routes/login'));
// redirect if we're not logged in
app.use(
jwt({ secret: config.authenticationSecret })
// Note: add this back if you want to skip authentication for /users again
// .unless({ path: [ { url: `${API_ROOT}/users`, methods: 'POST' } ] })
);
// rewrite "myself" with the logged in user id
app.use(require('./routes/myself'));
app.use(`${API_ROOT}/reminders`, require('./routes/reminders'));
app.use(`${API_ROOT}/subscriptions`, require('./routes/subscriptions'));
app.use(`${API_ROOT}/users`, require('./routes/users'));
app.use(`${API_ROOT}/groups`, require('./routes/groups'));
app.use((err, req, res, next) => {
if (res.headersSent) {
next(err);
return;
}
switch(err.name) {
case 'NotFoundError': break;
default: console.error(err.stack);
}
const errorMessage = {
error: err.name,
code: err.code,
message: err.message,
};
if (err.data) {
errorMessage.data = err.data;
}
res.status(err.status || 500).send(errorMessage);
});
const server = app.listen(config.httpPort, () => {
console.log(`HTTP server listening on port ${config.httpPort}.`);
});
function gracefulExit() {
console.log('Received exit request. Closing app...');
new Promise((resolve, reject) => {
server.close((err) => (err ? reject(err) : resolve()));
}).then(() => notificationsSender.stop())
.then(() => database.close())
.then(() => process.exit())
.catch((err) => {
console.error('Error while closing app: ', err);
process.exit(1);
});
}
process.on('SIGINT', gracefulExit);
process.on('SIGTERM', gracefulExit);