-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhermes.js
49 lines (40 loc) · 1.07 KB
/
hermes.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
'use strict';
const EventEmitter = require('events');
class Hermes {
constructor(routeSupplier, port) {
Object.assign(this, EventEmitter.prototype);
this.port = (typeof port === 'undefined' ? 3000 : port);
this.app = require('express')();
routeSupplier.getRoutes().then(routes => this.start(routes));
}
start(routes) {
this._initStatusRoute();
this._initProxyRoutes(routes);
this._init404Route();
this.server = this.app.listen(this.port, () => {
this.emit('listening', this.port);
});
}
stop() {
this.server.close();
}
_initStatusRoute() {
this.app.use('/status', Hermes.statusController);
}
_initProxyRoutes(routes) {
for(let route of routes) {
this.app.use(route.pattern, route.middleware);
route.on('proxy', this.emit.bind(this, 'proxy'));
}
}
_init404Route() {
this.app.use((req, res) => { res.status(404).send('Not found'); });
}
static statusController(req, res) {
res.send({
status: 'up',
time: new Date().toISOString()
});
}
}
module.exports = Hermes;