-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·74 lines (61 loc) · 1.96 KB
/
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
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
#!/usr/bin/env node
const Koa = require('koa');
const app = new Koa();
const port = process.env['PORT'] || 3000;
const bodyParser = require('koa-body');
const send = require('koa-send');
const serve = require('koa-static');
const main = require('./modules/invoice-service').default;
// logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});
// response time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
// static response
app.use(serve('public', { extensions: true }));
// parser
app.use(bodyParser());
// reactive response
app.use(async (ctx) => {
console.log('Request: ', ctx.request);
switch (ctx.path) {
case ('/log.json'):
await send(ctx, '/static/log.json');
break;
case ('/position_events.json'):
await send(ctx, '/static/position_events.json');
break;
case ('/create-invoice'):
await main([ 'create-invoice', ctx.request.body ])
.then(r => {
console.debug(JSON.stringify(r)); // debug
ctx.header['Content-Type'] = 'application/json';
ctx.body = r;
}, err => {
console.error(err);
});
break;
case ('/test-invoice'):
await main([ 'test-invoice', '192.168.1.3', 6379, 0 ])
.then(r => {
console.debug(JSON.stringify(r)); // debug
ctx.header['Content-Type'] = 'application/json';
ctx.body = r;
}, err => {
console.error(err);
});
break;
default:
return ctx.body = 'Not Found';
}
});
app.listen(port);
console.log('Listening @ http://127.0.0.1:' + port);