-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (38 loc) · 1.42 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
require('dotenv').config()
const fastify = require('fastify')
const path = require('path')
const marko = require('marko')
const WSClientArray = require('./lib/WSClientArray')
const Datastore = require('nedb-promises')
const db = Datastore.create({
filename: '/data/pull_requests.db',
autoload: true
})
let websocketClients = new WSClientArray()
const generateJWT = require('./lib/generateJWT')
const getInstallationToken = require('./lib/getInstallationToken')
let jwt = generateJWT()
getInstallationToken(jwt).then(token => { global.token = token })
setInterval(() => { jwt = generateJWT() }, 5 * 60 * 1000)
setInterval(async () => { global.token = await getInstallationToken(jwt) }, 10 * 60 * 1000)
const app = fastify()
app.register(require('fastify-websocket'), {
handle: conn => websocketClients.push(conn)
})
app.register(require('point-of-view'), {
engine: { marko },
templates: 'templates'
})
app.register(require('fastify-static'), {
root: path.join(__dirname, 'static', 'js'),
prefix: '/js/'
})
app.get('/', async (req, res) => {
res.header('Content-Security-Policy', `default-src 'self' https: wss: 'unsafe-inline';`)
res.view('index.marko', {
pullRequests: await db.find({}).sort({ id: -1 }).limit(50)
})
})
app.post('/webhook', require('./lib/webhook')(db, websocketClients))
app.post('/refresh', require('./lib/refresh')(db, websocketClients))
app.listen(5500, '0.0.0.0', () => console.log('I\'m listening!'))