-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadapter.js
126 lines (100 loc) · 3.5 KB
/
adapter.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
var create = require('./actions/create');
var start = require('./actions/start');
var responder = require('./responder');
var dockerclient = require('./dockerclient')
var utils = require('./utils')
var debug = require('debug')
var log = debug('router')
/*
this is the single endpoint that will route based on the
Request parameter of the JSON packet
it can be configured with opts
*/
module.exports = function(opts){
opts = opts || {};
// the two handlers for create & start actions
// overriden for the unit tests
var createHandler = opts.create || create;
var startHandler = opts.start || start;
// a do nothing handler for all other requests
var noopHandler = function(req, callback){
callback(null, req)
}
// the function used to fetch image data - overridden for the unit tests
var getImageData = opts.getImageData || dockerclient.image;
// the function used to fetch container data - overridden for the unit tests
var getContainerData = opts.getContainerData || dockerclient.container;
// the function used to ask weave to attach a container to a CIDR - overriden for the unit tests
var runWeaveAttach = opts.runWeaveAttach || utils.runWeaveAttach;
function startHandlerWrapper(req, callback){
startHandler(req.ClientRequest, {
getContainerData:getContainerData,
runWeaveAttach:runWeaveAttach
}, function(err){
if(err) return callback(err)
callback(null, req)
})
}
// the router array - enables us to match handlers to incoming requests
var routes = [{
method:'POST',
url:/^(\/[\w\.]+)?\/containers\/create/,
type:'pre-hook',
/*
CREATE action
pass the ClientRequest so the action ignores the powerstrip layer of the JSON packet
the create action changes the ClientRequest and so we re-assign the modified ClientRequest
*/
handler:function(req, callback){
createHandler(req.ClientRequest, {
getImageData:getImageData
}, function(err, ModifiedRequest){
if(err) return callback(err)
req.ClientRequest = ModifiedRequest;
callback(null, req);
})
}
},{
method:'POST',
url:/^(\/[\w\.]+)?\/containers\/\w+\/start/,
type:'post-hook',
/*
START action
pass the ClientRequest so the action ignores the powerstrip layer of the JSON packet
the start action does not change anything it just reads the container id and ENV
*/
handler:startHandlerWrapper
},{
method:'POST',
url:/^(\/[\w\.]+)?\/containers\/\w+\/restart/,
type:'post-hook',
/*
RESTART action
same as the start handler but once a container is restarted
*/
handler:startHandlerWrapper
}];
return function(req, callback){
var url = req.ClientRequest.Request || '';
log('ROUTING')
log(req.ClientRequest.Method + ' ' + url)
// loop over the registered routes to find a handler for this request
var handler;
routes.forEach(function(route){
if(handler) return;
if(route.method==req.ClientRequest.Method && route.type==req.Type && url.match(route.url)){
handler = route.handler;
}
})
if(!handler){
log('NO ROUTE FOUND')
}
// if no handler is found then we return the request unmolested
handler = handler || noopHandler;
handler(req, function(err, req){
if(err) return callback(err);
// pass off to the responder which knows how to format the powerstrip response body
responder(req, callback)
})
}
}