1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const _ = require('underscore');
|
7 | const wrap = _.wrap;
|
8 |
|
9 | const express = require('abacus-express');
|
10 | const cluster = require('abacus-cluster');
|
11 | const vcapenv = require('abacus-vcapenv');
|
12 | const perf = require('abacus-perf');
|
13 | const hystrix = require('abacus-hystrix');
|
14 | const eureka = require('abacus-eureka');
|
15 | const oauth = require('abacus-oauth');
|
16 | const cp = require('child_process');
|
17 | const commander = require('commander');
|
18 | const rc = require('abacus-rc');
|
19 |
|
20 |
|
21 | const debug = require('abacus-debug')('abacus-webapp');
|
22 | const edebug = require('abacus-debug')('e-abacus-webapp');
|
23 |
|
24 | const defaultSecurity = () => process.env.SECURED === 'true';
|
25 | const secured = () => process.env.HEALTHCHECK_SECURED === 'false' ? false : defaultSecurity();
|
26 |
|
27 | const healthCheckScopes = 'abacus.system.read';
|
28 |
|
29 | const defaultHealthCheck = (req, res) => {
|
30 | debug(`Entering ${secured ? 'secured' : ''} healthcheck`);
|
31 | const h = perf.healthy();
|
32 | debug('Returning app health %s', h);
|
33 | res.status(h ? 200 : 500).send({
|
34 | healthy: h
|
35 | });
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const webapp = () => {
|
44 |
|
45 | const appname = vcapenv.appname();
|
46 | debug('Creating app %s', appname);
|
47 |
|
48 |
|
49 | const app = cluster(express());
|
50 |
|
51 |
|
52 | app.use(vcapenv.headers());
|
53 |
|
54 | let healthCheckMiddlware = defaultHealthCheck;
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | app.useHealthCheck = (middlware) => {
|
63 | if (middlware) healthCheckMiddlware = middlware;
|
64 | else healthCheckMiddlware = defaultHealthCheck;
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 | app.listen = wrap(app.listen, (listen, opt, cb) => {
|
70 | if (secured())
|
71 | app.use(
|
72 | '/hystrix.stream',
|
73 | oauth.basicStrategy(process.env.API, hystrix.scopes, process.env.JWTKEY, process.env.JWTALGO)
|
74 | );
|
75 | app.use('/hystrix.stream', hystrix.stream());
|
76 |
|
77 | if (secured())
|
78 | app.use(
|
79 | '/healthcheck',
|
80 | oauth.basicStrategy(process.env.API, healthCheckScopes, process.env.JWTKEY, process.env.JWTALGO)
|
81 | );
|
82 | app.get('/healthcheck', healthCheckMiddlware);
|
83 |
|
84 |
|
85 | debug('Listening');
|
86 | const server = listen.call(app, opt, cb);
|
87 |
|
88 |
|
89 | if (cluster.isMaster() && eureka())
|
90 | eureka.register(eureka(), appname, vcapenv.iport() || server.address().port, vcapenv.iaddress(), (err, val) => {
|
91 | if (err) debug('Couldn\'t register app %s in Eureka registry, %o', appname, err);
|
92 | else debug('Registered app %s in Eureka registry', appname);
|
93 | });
|
94 |
|
95 | return server;
|
96 | });
|
97 |
|
98 | return app;
|
99 | };
|
100 |
|
101 |
|
102 | const basic = () => {
|
103 | return cluster(express.basic());
|
104 | };
|
105 |
|
106 |
|
107 | express.on('message', cluster.onMessage);
|
108 |
|
109 |
|
110 |
|
111 | if (cluster.size() > 1) {
|
112 | debug('Broadcasting perf stat messages across the cluster');
|
113 | cluster.on('message', perf.onMessage);
|
114 | perf.on('message', cluster.onMessage);
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 | const conf = () => {
|
120 | process.env.PORT = commander.port || process.env.PORT || 9080;
|
121 | if (commander.host) process.env.HOST = commander.host;
|
122 | };
|
123 |
|
124 |
|
125 | const runCLI = () => {
|
126 |
|
127 | commander
|
128 | .option('-p, --port <port>', 'port number [9080]')
|
129 | .option('-h, --host <hostname>', 'host name [*]')
|
130 | .option('start', 'start the server')
|
131 | .option('stop', 'stop the server')
|
132 | .parse(process.argv);
|
133 |
|
134 |
|
135 | rc();
|
136 |
|
137 |
|
138 | if (commander.start) {
|
139 | conf();
|
140 |
|
141 |
|
142 | const app = require(process.cwd());
|
143 | if (app && app.runCLI) app.runCLI();
|
144 | } else if (commander.stop)
|
145 |
|
146 | cp.exec('pkill -f "node ' + vcapenv.appname() + '.* master"', (err, stdout, stderr) => {
|
147 | if (err) edebug('Stop error %o', err);
|
148 | });
|
149 | };
|
150 |
|
151 |
|
152 | module.exports = webapp;
|
153 | module.exports.basic = basic;
|
154 | module.exports.runCLI = runCLI;
|