-
Notifications
You must be signed in to change notification settings - Fork 1
/
telemetry-server.js
44 lines (37 loc) · 1.31 KB
/
telemetry-server.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
const fs = require('fs')
const zlib = require('zlib')
const _ = require('lodash')
const redis = require('redis')
const { promisify } = require('util')
var client = redis.createClient(6399)
const microtime = require('microtime')
const express = require('express')
const compression = require('compression')
const morgan = require('morgan')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const errorhandler = require('errorhandler')
const crypto = require('crypto')
const app = express()
app.use(compression())
app.use(morgan('dev'))
app.use(express.json())
app.use(express.urlencoded())
app.use(cookieParser())
app.use(session({ secret: fs.readFileSync('session-secret', 'ascii') }))
app.use(express.static(__dirname + '/public'))
fs.mkdirSync('telemetry', {recursive: true})
app.post('/telemetry', (req, res) => {
console.log('Receive')
console.log(req.body.worker)
console.log(req.ip)
req.body.receivedAt = microtime.nowDouble()
req.body.ip = req.ip
const movieName = _.split(req.body.segment, ':')[0]
fs.appendFile('telemetry/' + req.body.worker + '.gz', zlib.gzipSync(JSON.stringify(req.body) + '\n'), () => 0)
res.type('text')
res.header("Access-Control-Allow-Origin", "*")
res.end()
})
app.listen(process.env.PORT || 3001)
app.use(errorhandler())