-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_api.js
68 lines (63 loc) · 2.28 KB
/
web_api.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
const http = require('http')
const lib = require(__dirname + '/lib')
const statusSystem = require(__dirname + '/modes/status')
const lokinet = require(__dirname + '/lokinet')
// if we keep this read-only, we can't go too wrong
// FIXME: caching layer...
// return server handle
function start(config) {
webApiServer = http.createServer(async function(request, response) {
//console.log('method', request.method)
switch(request.method) {
case 'GET':
//console.log('path', request.url)
switch(request.url) {
case '/v1/pids.json':
//
lib.getLauncherStatus(config, lokinet, 'waiting...', function(running, checklist) {
response.writeHead(200, {"Content-Type" : "application/json"})
// const combined = {...running, ...checklist}
response.end(JSON.stringify(running))
})
break;
case '/v1/health.json':
// , statusSystem.checkNetwork()
const statuses = await Promise.all([statusSystem.checkBlockchain(),
statusSystem.checkStorage()])
const status = statuses.reduce((result, current) => {
return Object.assign(result, current)
})
response.writeHead(200, {"Content-Type" : "application/json"})
response.end(JSON.stringify(status))
break;
case '/':
//response.writeHead(200, {"Content-Type" : "text/html"})
response.end(`
<a href="/v1/pids.json">pids.json</a>
<a href="/v1/health.json">health.json</a>
`)
break;
default:
response.writeHead(200, {"Content-Type" : "text/plain"});
response.end(`Unknown Path ${request.url}`);
break;
}
break;
default:
response.writeHead(200, {"Content-Type" : "text/plain"});
response.end(`Unknown Method ${request.method}`);
break;
}
});
webApiServer.listen(config.web_api.port, config.web_api.ip).on('error', function(e) {
if (e.code === 'EADDRINUSE') {
console.log('WEB_API: disabling because port', config.web_api.port, 'is inuse on', config.web_api.ip)
} else {
console.log('WEB_API:', e)
}
})
return webApiServer
}
module.exports = {
start: start,
}