forked from dockersamples/docker-swarm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (69 loc) · 1.81 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
var url = require('url')
var fs = require('fs');
var express = require('express');
var _ = require('lodash');
var superagent = require('superagent');
var net = require('net');
var http = require('http');
var WS = require('ws');
var WebSocketServer = WS.Server;
var host = process.env.HOST ||"localhost";
process.env.HOST=host
var port =process.env.PORT || 8080;
process.env.PORT=port;
var indexData;
var app = express();
var ms = process.env.MS || 1000;
process.env.MS=ms
app.use(express.static('dist'));
var server = app.listen(process.env.PORT || 3000, function () {
port = server.address().port;
process.env.HOST = host;
process.env.PORT = port;
indexData = _.template(fs.readFileSync('index.tpl'))(process.env);
});
app.get('/', function(req, res) {
res.send(indexData);
});
console.log(process.env.DOCKER_HOST)
if(process.env.DOCKER_HOST) {
try {
dh = process.env.DOCKER_HOST.split(":");
var docker_host = dh[0];
var docker_port = dh[1];
}
catch (err) {
console.log(err.stack)
}
}
var wss = new WebSocketServer({server: server});
app.get('/apis/*', function(req, response) {
var path = req.params[0];
var jsonData={};
var options = {
path: ('/' + path),
method: 'GET'
}
if(docker_host) {
options.host = docker_host;
options.port = docker_port;
}
else {
options.socketPath = '/var/run/docker.sock';
}
var req = http.request(options, (res) => {
var data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
jsonData['objects'] = JSON.parse(data.toString());
response.json(jsonData);
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
console.log(e.stack);
});
req.end();
});