forked from binocarlos/powerstrip-weave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (38 loc) · 1.04 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
var http = require('http');
var concat = require('concat-stream');
var Adapter = require('./adapter');
module.exports = function(opts){
var adapter = Adapter(opts);
return http.createServer(function(req, res){
/*
slurp the request body into a JSON object
then pass it off to the plugin to handle
*/
req.pipe(concat(function(body){
body = body.toString();
try {
body = JSON.parse(body);
} catch (e) {
res.statusCode = 200;
res.end('');
return;
}
/*
pass off the JSON body to the adapter which will route
the /containers/create and /containers/:id/start handlers
*/
adapter(body, function(err, body){
if(err){
res.statusCode = 500;
res.end(err.toString());
}
else{
res.statusCode = 200;
res.setHeader('Content-type', 'application/json');
body = JSON.stringify(body);
res.end(body);
}
})
}))
})
}