-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (32 loc) · 909 Bytes
/
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
const express = require('express')
const { fork } = require('child_process')
const binHandler = require('./bin')
const resultsView = require('./views/results.js')
let BIN_ID = process.env.ID
const port = process.env.PORT || 4711
const app = express()
app.get('/run', async (req, res) => {
const runner = fork('runner.js')
runner.send('run')
runner.on('message', result => {
if (result) {
binHandler.update(BIN_ID, result)
}
runner.kill()
res.end('done')
})
})
app.get('/results', async (req, res) => {
const results = await binHandler.get(BIN_ID)
res.status(200).send(resultsView(results))
})
app.listen(port, async () => {
console.log(`Monitor running on port ${port}`)
if (!BIN_ID) {
const bin = await binHandler.create()
BIN_ID = bin.id
console.log(
`Your app has generated a new ID, please set ID=${BIN_ID} before next launch.`
)
}
})