-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
81 lines (71 loc) · 2.06 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
'use strict';
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const path = require('path');
const server = Hapi.Server({
host: 'localhost',
port: 3000
});
async function start () {
// static file serving
await server.register({ plugin: require('@hapi/inert') });
[ ['/', path.join(__dirname, 'index.html')],
['/app.js', path.join(__dirname, 'app.js')],
['/jquery.js',
path.join(__dirname, 'node_modules', 'jquery', 'dist', 'jquery.js')],
['/opossum.js', path.join(__dirname, 'node_modules', 'opossum', 'dist', 'opossum.js')]
].map(entry => {
server.route({
method: 'GET',
path: entry[0],
handler: (request, h) => h.file(entry[1])
});
});
const baseline = 20;
let delay = baseline;
server.route({
method: 'GET',
path: '/flakeyService',
handler: function flakeyService (request, h) {
console.log('Flakey service delay', delay);
// if we're really slowing down, just reply with an error
if (delay > 1000) {
console.log('Long delay encountered, returning Error 423 (Locked)');
return Boom.locked('Flakey service is flakey');
}
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Replying with flakey response after delay of', delay);
delay = delay * 2;
resolve({
body: 'Flakey service response',
delay });
}, delay);
});
}
});
// reset the delay every 10 seconds
setInterval(() => {
if (delay !== baseline) {
delay = baseline;
console.log('Resetting flakey service delay to', delay);
}
}, 20000);
await server.start(err => {
possibleError(err);
console.log(`Server: ${server.info.uri}`);
console.log('Endpoints:');
server.table().map(entry => {
entry.table.map(route => {
console.log(`${route.method} ${route.path}`);
});
});
});
}
process.on('uncaughtException', e => {
process._rawDebug(`Uncaught exception ${e}`);
});
function possibleError (err) {
if (err) throw err;
}
start();