-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (32 loc) · 1.41 KB
/
index.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
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const Cron = require('cron').CronJob
const env = require('./lib/env')
const middleware = require('./lib/middleware')
const handlers = require('./lib/handlers')
const gatekeeper = require('./lib/gatekeeper')
const scheduler = require('./lib/scheduler')
const storage = require('./lib/storage')
process.on('unhandledRejection', console.error)
async function init () {
await storage.init()
const app = express()
app.set('json spaces', 2)
app.use(bodyParser.json({ verify: middleware.rawBody }))
app.use(bodyParser.urlencoded({ extended: true, verify: middleware.rawBody }))
app.get('/', (req, res) => res.json({ status: 'ok' }))
app.get('/schedule', middleware.async(handlers.schedule))
app.post('/commands', gatekeeper.lock, middleware.async(handlers.commands))
app.post('/actions', gatekeeper.lock, middleware.async(handlers.actions))
Object.keys(env.locations.tzs).forEach(async (tz) => {
const cronPattern = `${env.matchTime.cron} * * ${env.matchDay.raw}`
const cron = new Cron(cronPattern, () => {}, null, true, tz)
await scheduler.add(tz, cron)
})
const skips = await storage.listSkips()
skips.forEach(x => scheduler.remove(x))
scheduler.ticker = new Cron('* * * * *', handlers.match, null, true, null, null, null, 0)
app.listen(env.port, () => console.log('server listening'))
}
init()