forked from OSBI/saiku-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
83 lines (70 loc) · 2.59 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
75
76
77
78
79
80
81
82
83
/**
* Node.js proxy for Saiku
* Use this proxy to develop for the UI without having to install the server.
* Requests will be proxied to demo.analytical-labs.com,
* or a Saiku server installation of your choice.
*
* To play with the chaos monkey, set the CHAOS_MONKEY environment variable
* to anything (Preferably a nice name for your chaos monkey).
*
* To start the server, run `node server.js [port] [backend_host] [backend_port]`
*/
var http = require('http');
var express = require('express');
var app = express.createServer();
var port = process.env.C9_PORT || parseInt(process.ARGV[2], 10) || 8080;
var backend_host = process.ARGV[3] || 'dev.analytical-labs.com';
var backend_port = process.ARGV[4] || 80;
var proxy = http.createClient(backend_port, backend_host);
proxy.on('error', function() {
proxy = http.createClient(backend_port, backend_host);
});
// Load static server
var twoHours = 1000 * 60 * 60 * 2;
app.use(express['static'](__dirname));
// Proxy request
function get_from_proxy(request, response) {
var proxy_request = proxy.request(request.method, request.url, request.headers);
proxy_request.addListener('response', function (proxy_response) {
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {
if (process.env.CHAOS_MONKEY) {
setTimeout(function() {
response.end();
}, Math.floor(Math.random() * 3000));
} else {
response.end();
}
});
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
request.addListener('data', function(chunk) {
proxy_request.write(chunk, 'binary');
});
proxy_request.end();
}
// Unleash the chaos monkey!
function unleash_chaos_monkey(request, response) {
var monkey = "The chaos monkey strikes again!";
response.writeHead(500, {
"Content-Type": "text/plain",
"Content-Length": monkey.length
});
response.write(monkey);
response.end();
}
// Handle incoming requests
app.all("/saiku/*", function(request, response) {
console.log(request.method, request.url);
request.headers.host = backend_host;
if (process.env.CHAOS_MONKEY && Math.random() < 0.25) {
unleash_chaos_monkey(request, response);
} else {
get_from_proxy(request, response);
}
});
console.log("Connected to '", backend_host, ":", backend_port,"'");
console.log("Proxy listening on", port);
app.listen(port, '0.0.0.0');