forked from bewest/node-multienv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
378 lines (325 loc) · 11.6 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var restify = require('restify');
var fs = require('fs');
var path = require('path');
var tmp = require('tmp');
var mv = require('mv');
var bunyan = require('bunyan');
function createServer (opts) {
var cluster = opts.cluster;
var master = opts.create;
if (opts) {
opts.handleUpgrades = true;
}
var server = restify.createServer(opts);
server.on('after', restify.auditLogger({
log: bunyan.createLogger({
name: 'audit',
stream: process.stdout
})
}));
server.get('/cluster', function (req, res, next) {
var h = { };
var worker;
for (var id in cluster.workers) {
worker = cluster.workers[id];
var v = {
id: id
, custom_env: worker.custom_env
, state: worker.state
, isDead: worker.isDead && worker.isDead( )
, url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/'
, status_url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/api/v1/status.json'
};
// console.log(worker);
h[id] = v;
}
res.send(h);
next( );
});
server.get('/resolve/:id', function (req, res, next) {
var id = parseInt(req.params.id);
var worker = cluster.workers[id] || {custom_env: { }, state: 'missing'};
// console.log('worker', worker);
var v = {
id: id
, state: worker.state
, name: path.basename(worker.custom_env.envfile, '.env')
, url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/'
, port: worker.custom_env.PORT
, status_url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/api/v1/status.json'
};
console.log(req.url, req.headers);
res.header('X-Backend-State', v.state);
res.header('X-Backend-Name', v.name);
res.header('X-Backend', v.url);
res.end( );
next( );
});
server.get('/resolve/:id/test', function (req, res, next) {
var id = parseInt(req.params.id);
var worker = cluster.workers[id] || {custom_env: { }, state: 'missing'};
var port = worker.custom_env.PORT;
// console.log('worker', port, worker);
var v = {
id: id
, state: worker.state
, envfile: worker.custom_env.envfile
, name: path.basename(worker.custom_env.envfile, '.env')
, port: port
, url: "http://" + [ 'localhost', port ].join(':') + '/'
, status_url: "http://" + [ 'localhost', port ].join(':') + '/api/v1/status.json'
};
// console.log(req.url, req.headers);
res.header('X-Backend-State', v.state);
res.header('X-Backend-Name', v.name);
res.header('X-Backend', v.url);
// var internal = '/x-accel-redirect/' + v.port + '/api/v1/status.json';
var internal = '@proxy/' + v.port + '/' + v.id;
console.log('internal!', internal);
res.header('x-accel-redirect', internal);
res.end( );
next( );
});
server.get('/cluster/:id', function (req, res, next) {
var h = { };
var worker = cluster.workers[req.params.id];
var v = {
id: id
, custom_env: worker.custom_env
, state: worker.state
, isDead: worker.isDead && worker.isDead( )
, url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/'
, status_url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/api/v1/status.json'
};
res.send(h);
next( );
});
server.get('/history', function (req, res, next) {
var h = { };
var worker;
for (var file in master.handlers) {
worker = master.handlers[file].worker;
// console.log(worker);
var v = {
id: worker.id
, custom_env: worker.custom_env
, state: worker.state
, isDead: worker.isDead && worker.isDead( )
// , url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/'
// , status_url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/api/v1/status.json'
};
h[file] = v;
}
res.send(h);
next( );
});
server.get('/environs', function (req, res, next) {
master.scan(master.env, function iter (err, environs) {
var h = { };
var worker;
for (var i in environs) {
var environ = environs[i];
var file = environ.envfile;
var handler = master.handlers[file];
var worker = handler ? handler.worker : { };
// console.log(worker);
var v = {
id: worker.id || null
, custom_env: worker.custom_env || environ
, state: worker.state || 'missing'
, isDead: worker.isDead && worker.isDead( )
// , url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/'
// , status_url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/api/v1/status.json'
};
h[file] = v;
}
res.send(h);
next( );
});
});
server.get('/environs/:name', function (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
var handler = master.handlers[file];
var worker = handler ? handler.worker : { };
var v = {
id: worker.id || null
, custom_env: worker.custom_env
, state: worker.state || 'missing'
, isDead: worker.isDead && worker.isDead( )
// , url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/'
// , status_url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/api/v1/status.json'
};
res.header('content-type', 'application/json');
res.send(v);
next( );
});
// server.get(/^\/environs\/(.*)\/resolver\/(.*)?$/, function (req, res, next) { })
function resolverA (req, res, next) {
req.params.name = req.params[0];
req.params.target = req.params[1] || '';
console.log('found target', req.params.target);
next( );
}
function resolverB (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
var name = req.params.name;
var frontend = req.params.frontend;
var hostname = req.header('host');
var scheme = req.isSecure( ) ? 'https' : 'http';
var handler = master.handlers[file];
var worker = handler
? ( cluster.workers[handler.worker.id]
? cluster.workers[handler.worker.id]
: handler.worker
)
: { }
;
var port = worker.custom_env.PORT || 80;
var missing_url = scheme + '://' + hostname + '/';
// console.log('LIVE WORKER', worker);
console.log('PORT WORKER', port);
var v = {
id: worker.id || 'missing'
, custom_env: worker.custom_env
, port: port
, state: worker.state || 'missing'
, isDead: worker.isDead && worker.isDead( )
// , url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/'
// , status_url: "http://" + [ 'localhost', worker.custom_env.PORT ].join(':') + '/api/v1/status.json'
};
var internal = '@proxy/' + v.port + '/' + v.id + '/' + req.params.target;
// var internal = '@proxy/' + v.port + '/' + v.id;
console.log('internal!', internal, v);
res.header('x-accel-redirect', internal);
res.end( );
next( );
}
server.get(/^\/environs\/(.*)\/resolver\/(.*)?$/, resolverA, resolverB);
server.del(/^\/environs\/(.*)\/resolver\/(.*)?$/, resolverA, resolverB);
server.post(/^\/environs\/(.*)\/resolver\/(.*)?$/, resolverA, resolverB);
server.put(/^\/environs\/(.*)\/resolver\/(.*)?$/, resolverA, resolverB);
server.head(/^\/environs\/(.*)\/resolver\/(.*)?$/, resolverA, resolverB);
server.opts(/^\/environs\/(.*)\/resolver\/(.*)?$/, resolverA, resolverB);
server.patch(/^\/environs\/(.*)\/resolver\/(.*)?$/, resolverA, resolverB);
server.use(restify.queryParser( ));
server.use(restify.bodyParser( ));
server.del('/environs/:name', function (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
console.log("DELETING", req.params.name, file);
fs.unlink(file, function (ev) {
console.log('OK', arguments);
res.status(204);
res.send("");
// next( );
});
});
server.get('/environs/:name/env', function (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
var handler = master.handlers[file];
var worker = handler ? handler.worker : { };
res.send(worker.custom_env);
next( );
});
server.get('/environs/:name/env/:field', function (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
var worker = master.handlers[file].worker || { };
var field = worker.custom_env[req.params.field];
console.log(req.params.field, field);
if (typeof field !== 'undefined') {
res.send(field);
} else {
res.status(404);
res.send({msg: "field unknown", field: req.params.field});
}
next( );
});
server.post('/environs/:name/env/:field', function (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
var env = master.read(file);
if (fs.existsSync(file)) { fs.unlinkSync(file); }
var field = req.params.field;
env[req.params.field] = req.params[req.params.field] || req.body[field] || '';
var tmpname = tmp.tmpNameSync( );
var out = fs.createWriteStream(tmpname);
out.on('close', function (ev) {
mv(tmpname, file, function (err) {
res.status(201);
setTimeout(function ( ) {
res.send(env[req.params.field]);
next(err);
}, 3000);
});
});
var text = [ ];
for (x in env) {
text.push([x, env[x] ].join('='));
}
out.write(text.join("\n"));
out.end( );
res.status(201);
res.header('Location', '/environs/' + req.params.name);
next( );
});
server.del('/environs/:name/env/:field', function (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
var env = master.read(file);
delete env[req.params.field];
var tmpname = tmp.tmpNameSync( );
var out = fs.createWriteStream(tmpname);
out.on('close', function (ev) {
mv(tmpname, file, function (err) {
res.status(204);
res.send(env[req.params.field]);
next(err);
});
});
var text = [ ];
for (x in env) {
text.push([x, env[x] ].join('='));
}
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
out.write(text.join("\n"));
out.end( );
res.status(201);
res.header('Location', '/environs/' + req.params.name);
next( );
});
server.post('/environs/:name', function (req, res, next) {
var file = path.resolve(master.env.WORKER_ENV, path.basename(req.params.name + '.env'));
var tmpname = tmp.tmpNameSync( );
var text = [ ];
var item = { };
var out = fs.createWriteStream(tmpname);
if (fs.existsSync(file)) { fs.unlinkSync(file); }
out.on('close', function (ev) {
mv(tmpname, file, function (err) {
setTimeout(function ( ) {
res.send(item);
next(err);
}, 2500);
});
});
console.log('query', req.query);
console.log('input', req.body);
var x;
text.push(['WEB_NAME', req.params.name ].join('='));
item['WEB_NAME'] = req.params.name;
for (x in req.query) {
text.push([x, req.query[x] ].join('='));
item[x] = req.query[x];
}
for (x in req.body) {
text.push([x, req.body[x] ].join('='));
item[x] = req.body[x];
}
console.log('writing', file);
out.write(text.join("\n"));
res.status(201);
res.header('Location', '/environs/' + req.params.name);
out.end( );
});
return server;
}
exports = module.exports = createServer;