-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
58 lines (45 loc) · 1.29 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
var http = require('http');
var concat = require('concat-stream');
var Adapter = require('./adapter');
var debug = require('debug')
var log = debug('server')
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('');
log('ERROR: ' + e.toString())
return;
}
log(JSON.stringify(body, null, 4))
/*
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());
log('ERROR: ' + e.toString())
}
else{
res.statusCode = 200;
res.setHeader('Content-type', 'application/json');
log('AFTER ADAPTER')
log(JSON.stringify(body, null, 4))
body = JSON.stringify(body);
res.end(body);
}
})
}))
})
}