-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (56 loc) · 1.67 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
const express = require('express');
const dotenv = require('dotenv');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const db = require('./helpers/db');
const app = express();
const webhookMiddleware = require('x-hub-signature').middleware;
const schedule = require('node-schedule');
const Notification = require('./helpers/notification');
const SisFetcher = require('./fetchers/sis');
const ruleFetcher = new schedule.RecurrenceRule();
const notification = new Notification();
const fetcher = new SisFetcher();
ruleFetcher.minute = new schedule.Range(1, 59, 15);
// Load the variables in .env file to the process.env
dotenv.config();
// Connect to the db and listen if success
db
.connect()
.on('error', console.error)
.on('disconnected', db.connect)
.once('open', () => {
app.listen(process.env.APP_PORT);
console.log(`Listening on port: ${process.env.APP_PORT}`);
});
// Configure Middlewares
app.use(morgan(process.env.LOGGING_LEVEL || 'tiny'));
app.use(bodyParser.json());
// Configure Routes
app.use('/', require('./routes/index'));
app.use(
'/webhook',
require('./routes/webhook'),
webhookMiddleware({
algorithm: 'sha1',
secret: process.env.PAGE_TOKEN,
require: true,
bodyParser: bodyParser.json, // DO NOT INVOKE HERE! e.g. bodyParser.json()
bodyParserOptions: {},
}),
);
app.use((err, req, res, next) => {
if (err.status) {
res.status(err.status);
}
res.json({
code: err.alias,
message: err.message,
messages: err.messages,
});
next(err);
});
schedule.scheduleJob(ruleFetcher, () => {
fetcher.fetchAndSave({}, console.log);
setTimeout(() => notification.notificationToAll(),7000);
});