-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (76 loc) · 2.27 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const crypto = require('crypto')
const express = require('express')
const EventEmitter = require('events')
const app = express()
const http = require('http');
const url = require('url');
const WebSocket = require('ws');
let listeners = {}
let apis = {}
const stateEmitter = new EventEmitter()
app.enable('etag')
app.get('/', (req, res) => {
res.sendFile('client/index.html', { root: __dirname })
})
var config = require('./api.json')
config.forEach(api => {
apis[api.name] = {
current: api.responses[0],
responses: api.responses,
index: 0,
lastUpdated: Date.now()
}
startResponseLifecycle(api)
app.get(api.endpoint, (req, res) => {
const currentResponse = apis[api.name].current
res.header('Access-Control-Allow-Origin', '*')
if (currentResponse.headers) {
Object.keys(currentResponse.headers).forEach((header) => {
res.setHeader(header, currentResponse.headers[header])
})
}
if (currentResponse.response) {
res.status(currentResponse.status)
res.sendFile(currentResponse.response, { root: __dirname })
} else {
res.status(500);
res.send('Error with api configuration')
}
})
})
app.get('/config', (req, res) => {
res.header('Access-Control-Allow-Origin', '*')
res.send(config)
})
app.use(express.static('client'))
let port = process.env.PORT || 9000
const server = http.createServer(app)
const wss = new WebSocket.Server({ server })
wss.on('connection', (ws, req) => {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send(JSON.stringify(apis))
stateEmitter.on('update', update)
ws.on('close', () => {
stateEmitter.removeListener('update', update)
})
function update() {
ws.send(JSON.stringify(apis))
}
})
server.listen(port)
function startResponseLifecycle(api) {
const apiState = apis[api.name]
const current = apiState.current
setTimeout(() => {
const nextIndex = apiState.index + 1 >= apiState.responses.length ?
0 : apiState.index + 1;
apiState.current = apiState.responses[nextIndex]
apiState.index = nextIndex
apiState.lastUpdated = Date.now()
stateEmitter.emit('update')
startResponseLifecycle(api)
}, current.timeToLive * 1000);
}
console.log(`Started on port ${port}`)