-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensemble.httpserver.js
More file actions
73 lines (64 loc) · 2.47 KB
/
ensemble.httpserver.js
File metadata and controls
73 lines (64 loc) · 2.47 KB
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
// simple http server run from Ensemble.
// will hand off requests to the adapter to send to servier
var ensemble = require('./ensemble.js');
if ( process.argv.length < 6 ) {
console.dir("INVALID args " + JSON.stringify(process.argv));
return;
}
var port = process.argv[2];
var connection_string = process.argv[3]; // required!
var namespace = process.argv[4]; //default
var targetConfigNames = process.argv[5];
var url = require('url');
var http = require('http');
var querystring = require('querystring');
function postRequest(request, response, callback) {
var queryData = "";
if(typeof callback !== 'function') return null;
if(request.method == 'POST') {
request.on('data', function(data) {
queryData += data;
console.log('data='+data);
if(queryData.length > 1e6) {
queryData = "";
response.writeHead(413, {'Content-Type': 'text/plain'});
request.connection.destroy();
}
});
request.on('end', function() {
response.post = queryData; //querystring.parse(queryData);
callback(queryData);
});
} else {
response.writeHead(405, {'Content-Type': 'text/plain'});
response.end();
}
}
// connect up to Ensemble and fetch handle back to
// the namespace & production we're servicing.
//
var director = new ensemble.Director( connection_string, namespace);
http.createServer(function (req, res) {
if ( req.url == '/__shutdown__' ) {
console.dir("Got shutdown");
process.exit(0);
}
postRequest(req, res, function (data) {
console.log(res.post);
res.writeHead(200, {'Content-Type': 'text/plain'});
console.dir(req.url);
// send the request back to Ensemble to handle, send response back to client
//
debugger;
// should have a way to route requests based upon url to the correct
// production!
var ensResponse = director.onRequest({"url":req.url, "data":data}, targetConfigNames);
console.log('ensResponse='+ensResponse);
//console.dir( JSON.parse(ensResponse) );
res.write( ensResponse );
res.end('Hello World\n');
});
}).listen(port, '127.0.0.1');
console.log('Ensemble httpserver start.');
console.log('namespace='+namespace+' targetConfigNames='+targetConfigNames);
console.log('Server running at http://127.0.0.1:'+port+'/');