-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
42 lines (33 loc) · 1.17 KB
/
index.ts
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
import express from 'express';
import * as actual from './actual';
import { authMiddleware } from './auth';
import { router } from './routes';
const app = express();
app.use(authMiddleware);
app.use('/', router);
const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
console.error(err);
res.sendStatus(500);
};
app.use(errorHandler);
// Server start
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(`Listening on port ${port}...`);
console.log('Initializing Actual DB...');
actual.init().then(() => console.log('Actual DB initialized'));
});
// Refresh Actual data at a given frequence
const intervalID = setInterval(() => {
actual.init().then(() => console.log('Actual DB refreshed'));
}, (Number(process.env.ACTUAL_REFRESH_INTERVAL_SECONDS) || 60) * 1000);
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
// Graceful shutdown
function shutdown() {
clearInterval(intervalID);
console.log('Shutting down Actual DB...');
actual.shutdown().then(() => console.log('Actual DB shut down'));
console.log('Closing HTTP server...');
server.close(() => console.log('HTTP server closed'));
}