-
Notifications
You must be signed in to change notification settings - Fork 3
/
sail.node.server.js
117 lines (96 loc) · 3.43 KB
/
sail.node.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
Simple stand-alone HTTP server with built-in reverse proxy for XMPP-BOSH.
This uses node.js with the http-proxy and node-static modules.
To use, create a `server.js` file at the root of your Sail app and add this code:
var sail = require('./js/sail.js/sail.node.server.js')
sail.server.listen(8000)
Looks for a `config.json` file in the current directory and configures itself accordingly.
*/
/*jshint devel: true */
/*global require, exports */
var http = require('http');
var httpProxy = require('http-proxy');
var httpStatic = require('node-static');
var url = require('url');
var util = require('util');
var fs = require('fs');
try {
var config = JSON.parse(fs.readFileSync('config.json'));
} catch (e) {
console.error("Error reading config.json. Does the file exists?\n("+e+")");
throw e;
}
var proxy = new httpProxy.RoutingProxy();
var file = new(httpStatic.Server)('.', {cache: false});
var server = http.createServer(function (req, res) {
var i;
for (i = 0; i <= server.proxyMap.length; i++) {
var map = server.proxyMap[i];
if (map.match(req)) {
map.proxy(req, res);
break;
}
}
});
server.config = config;
server.proxy = proxy;
server.start = function(port) {
this.listen(port, function() {
console.log("\nUsing settings from config.js:\n", config, "\n");
console.log("Sail server listening on http://localhost:" + port + "...");
});
};
server.proxyMap = [
{
name: 'BOSH',
match: function(req) { return url.parse(req.url).pathname.match(/^\/http-bind/); },
proxy: function(req, res) {
console.log("PROXY "+req.url+" ==> "+config.xmpp.url);
// FIXME: config.xmpp.url might be for websockets, in which case reverse proxying doesn't make sense
var boshUrl = url.parse(config.xmpp.url);
proxy.proxyRequest(req, res, {
host: boshUrl.hostname,
port: boshUrl.port
});
}
},
{
name: "Rollcall",
match: function(req) { return url.parse(req.url).pathname.match(/^\/rollcall/); },
proxy: function(req, res) {
req.url = req.url.replace(/\/rollcall/,'');
var rollcallUrl = url.parse(config.rollcall.url);
console.log("PROXY "+req.url+" ==> "+config.rollcall.url);
req.headers.host = rollcallUrl.hostname;
proxy.proxyRequest(req, res, {
host: rollcallUrl.hostname,
port: rollcallUrl.port || 80
});
}
},
{
name: "Mongo REST",
match: function(req) { return url.parse(req.url).pathname.match(/^\/mongo/); },
proxy: function(req, res) {
req.url = req.url.replace(/\/mongo/,'');
var drowsyUrl = url.parse(config.drowsy.url);
console.log("PROXY "+req.url+" ==> "+config.drowsy.url);
req.headers.host = drowsyUrl.hostname;
proxy.proxyRequest(req, res, {
host: drowsyUrl.hostname,
port: drowsyUrl.port
});
}
},
{
name: "STATIC",
match: function(req) { return true; },
proxy: function(req, res) {
req.addListener('end', function(){
console.log("STATIC "+req.url);
file.serve(req, res);
});
}
}
];
exports.server = server;