-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.js
162 lines (131 loc) · 4.46 KB
/
httpd.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var http = require('http'),
conf = require('./conf/httpd'),
dyno = require('./dyno'),
app = require('./app'),
pending = {},
next_seq = 0,
ports = {},
apps = {},
thisPort = exports.port = process.env.PORT || conf.basePort;
exports.reservePort = function (dyno) {
if (!apps[dyno.app.name])
apps[dyno.app.name] = dyno.app;
thisPort++;
dyno.port = thisPort;
ports[thisPort] = dyno;
return thisPort;
};
exports.hookRpc = function (amqp) {
exports.amqp = amqp;
exports.exchange = amqp.exchange('rpc');
exports.exchange.on('open', function () {
exports.queue = amqp.queue('app-manager-http-proxy', function () {
exports.queue.bind('rpc', 'rpc.responses');
});
exports.queue.subscribeJSON(function(message) {
message = JSON.parse(message.data);
var data = pending[message.seq];
if (!data) return;
pending[message.seq] = undefined;
var target = data[0];
var req = data[1];
var res = data[2];
var result = message.result || {};
req.headers['x-user'] = JSON.stringify(result.user || {});
req.headers['x-stranger'] = JSON.stringify(result.stranger || {});
req.headers['x-context'] = JSON.stringify(result.context || {});
var dyno = target.randomDyno('web');
var options = {
hostname: 'localhost',
port: dyno.port,
method: req.method,
path: req.url,
headers: req.headers};
var requ = http.request(options, function (resp) {
res.writeHead(resp.statusCode, resp.headers);
resp.pipe(res);
});
req.resume();
requ.write(req.buffer);
req.removeAllListeners('data');
if (req.readable)
req.pipe(requ);
else
requ.end();
});
});
};
exports.handle = function (req, res, app) {
var dynos = app.listDynos('web');
if (dynos.length == 0) {
console.log("Starting a dyno of", app.name, "on-demand");
dyno.start(app, "web", null, function () {
console.log("Started");
exports.handle(req, res, apps[app.name]);
});
return;
};
var cookies = {};
req.headers.cookie && req.headers.cookie.split(';').forEach(function (cookie) {
var parts = cookie.split('=');
cookies[parts[0].trim()] = (parts[1] || '').trim();
});
var session = cookies[conf.cookie];
next_seq += 1;
pending[next_seq] = [app, req, res];
exports.exchange.publish('rpc.requests', {
object: 'auth',
method: 'check_session',
params: {cookie: session},
seq: next_seq
});
req.pause();
req.buffer = new Buffer(0);
req.on('data', function (buff) {
var nbuff = new Buffer(req.buffer.length + buff.length);
req.buffer.copy(nbuff);
buff.copy(nbuff, req.buffer.length);
req.buffer = nbuff;
});
return;
};
exports.server = http.createServer(function (req, res) {
var host = req.headers['x-app'] || req.headers.host || '';
console.log(req.method, host, req.url);
var name = host.split('.')[0];
if (apps[name]) { // use existing dyno
return exports.handle(req, res, apps[name]);
};
app.fromName(name, function (app) {
if (app) { // lazy dyno booting
console.log("Starting a dyno of", app.name, "on-demand");
dyno.start(app, "web", null, function () {
console.log("Started");
exports.handle(req, res, apps[app.name]);
});
} else if (req.url == '/') {
res.writeHead(200, {'content-type': 'text/html'});
res.write('<!doctype html><html>');
res.write('<head><title>App Manager</title></head>');
res.write('<body><h1>Running Apps</h1><ul>');
var root = req.headers['x-actual-host'] || req.headers['x-forwarded-host'] || req.headers['host'];
Object.keys(apps).forEach(function (name) {
var info = apps[name];
var path;
if (req.headers['x-environment'] == 'development')
path = root + name + '/';
else
path = name + '.' + root;
res.write('<li><a href="http://' + path + '">' + name + '</a> (' + info.dynos.length + ')</li>');
});
res.write('</ul></body></html>');
res.end();
} else {
res.writeHead(404, {'content-type': 'text/plain'});
res.end("ain't nuttin' here");
};
});
});
exports.server.listen(thisPort, function () {
console.log('Listening for HTTP traffic on port ' + thisPort + ', http://apps.' + conf.baseName + ':' + thisPort + '/');
});