1 |
|
2 |
|
3 | namespace HawtioBackend {
|
4 | export var log = logger.get('hawtio-backend');
|
5 | export var app = express();
|
6 | export var proxyRoutes = {};
|
7 |
|
8 | var startupTasks = [];
|
9 | var listening = false;
|
10 |
|
11 | export function getTargetURI(options) {
|
12 | var target = new URI({
|
13 | protocol: options.proto,
|
14 | username: options.username,
|
15 | password: options.password,
|
16 | hostname: options.hostname,
|
17 | port: options.port,
|
18 | path: options.path
|
19 | });
|
20 | target.query(options.query);
|
21 | var targetURI = target.toString();
|
22 | log.debug("Target URI: ", targetURI);
|
23 | return targetURI;
|
24 | }
|
25 |
|
26 | export function addStartupTask(cb:() => void) {
|
27 | log.debug("Adding startup task");
|
28 | startupTasks.push(cb);
|
29 | if (listening) {
|
30 | cb();
|
31 | }
|
32 | }
|
33 |
|
34 | export function setConfig(newConfig:any) {
|
35 | _.assign(config, newConfig);
|
36 | log.setLevel(config.logLevel);
|
37 | }
|
38 |
|
39 | var server = null;
|
40 | var lr = null;
|
41 | var lrServer = null;
|
42 |
|
43 | export function reload() {
|
44 | return eventStream.map((file, callback) => {
|
45 | if (lr) {
|
46 | lr.changed({
|
47 | body: {
|
48 | files: file.path
|
49 | }
|
50 | });
|
51 | }
|
52 | return callback(null, file);
|
53 | });
|
54 | }
|
55 |
|
56 | export function use(path: any, func: any) {
|
57 | app.use(path, func);
|
58 | }
|
59 |
|
60 | export function listen(cb:(server:any) => void) {
|
61 | var lrPort = config.liveReload.port || 35729;
|
62 | if (config.liveReload.enabled) {
|
63 | app.use(liveReload({ port: lrPort }));
|
64 | }
|
65 | listening = true;
|
66 | startupTasks.forEach((cb) => {
|
67 | log.debug("Executing startup task");
|
68 | cb();
|
69 | });
|
70 | if (config.fallback) {
|
71 | if (typeof config.fallback === 'string') {
|
72 | HawtioBackend.app.use(function (req, res, next) {
|
73 | fs.createReadStream(config.fallback).pipe(res);
|
74 | });
|
75 | } else if (typeof config.fallback === 'object') {
|
76 | HawtioBackend.app.use(function (req, res, next) {
|
77 | const match = _.find(config.fallback, (_, k) => req.originalUrl.match(new RegExp(k)));
|
78 | if (match) {
|
79 | fs.createReadStream(match).pipe(res);
|
80 | } else {
|
81 | res.statusCode = 404;
|
82 | res.end();
|
83 | }
|
84 | });
|
85 | } else {
|
86 | HawtioBackend.log.warn("Unsupported fallback option:", config.fallback);
|
87 | }
|
88 | }
|
89 | server = app.listen(config.port, () => {
|
90 | if (config.liveReload.enabled) {
|
91 | lr = tiny_lr();
|
92 | lrServer = lr.listen(lrPort, () => {
|
93 | log.info("Started livereload, port :", lrPort);
|
94 | });
|
95 | }
|
96 | cb(server);
|
97 | });
|
98 | server.on('upgrade', (req, socket, head) => {
|
99 |
|
100 | var targetUri = new URI(req.url);
|
101 | var targetPath = targetUri.path();
|
102 | _.forIn(proxyRoutes, (config:any, route) => {
|
103 | if (s.startsWith(targetPath, route)) {
|
104 |
|
105 | if (!config.httpProxy) {
|
106 | var proxyConfig = config.proxyConfig;
|
107 | var target = new URI().protocol(proxyConfig.proto).host(proxyConfig.hostname).port(proxyConfig.port).path(proxyConfig.targetPath).query({}).toString();
|
108 | console.log("Creating websocket proxy to target: ", target);
|
109 | config.proxy = httpProxy.createProxyServer({
|
110 | target: target,
|
111 | secure: false,
|
112 | ws: true
|
113 | });
|
114 | }
|
115 | targetPath = targetPath.substring(route.length);
|
116 | req.url = targetUri.path(targetPath).toString();
|
117 | config.proxy.ws(req, socket, head);
|
118 | }
|
119 | });
|
120 | });
|
121 | return server;
|
122 | }
|
123 |
|
124 | export function stop(cb) {
|
125 | if (lrServer) {
|
126 | lrServer.close(() => {
|
127 | log.info("Stopped livereload port");
|
128 | });
|
129 | lrServer = null;
|
130 | }
|
131 | if (server) {
|
132 | server.close(() => {
|
133 | listening = false;
|
134 | if (cb) {
|
135 | cb();
|
136 | }
|
137 | });
|
138 | server = null;
|
139 | }
|
140 | }
|
141 |
|
142 | export function getServer() {
|
143 | return server;
|
144 | }
|
145 |
|
146 | if (runningAsScript) {
|
147 | server = listen((server) => {
|
148 | var host = server.address().address;
|
149 | var port = server.address().port;
|
150 | log.info("started at ", host, ":", port);
|
151 | });
|
152 | }
|
153 |
|
154 | }
|
155 |
|
156 | (module).exports = HawtioBackend;
|
157 |
|