-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
105 lines (79 loc) · 2.68 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
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var Docker = require('dockerode');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('src'))
var port = process.env.PORT || 9009; // set our port
var router = express.Router(); // get an instance of the express Router
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
router.get('/containers', function(req, res) {
var docker = new Docker();
docker.listContainers({all: false}, function(err, containers) {
res.json(containers);
});
});
router.get('/images', function(req, res) {
var docker = new Docker();
docker.listImages({all: false}, function(err, images) {
res.json(images);
});
});
router.get('/networks', function(req, res) {
var docker = new Docker();
docker.listNetworks({all: false}, function(err, networks) {
res.json(networks);
});
});
router.get('/swarm', function(req, res) {
var docker = new Docker();
docker.swarmInspect(function(err, swarnInfo) {
res.json(swarnInfo);
});
});
router.get('/nodes', function(req, res) {
var docker = new Docker();
docker.listNodes(function(err, nodes) {
res.json(nodes);
});
});
router.delete('/nodes/:nodeId', function(req, res) {
if(req.params.nodeId) {
console.log('Request DELETE NODE ID=' + req.params.nodeId)
var docker = new Docker();
docker.listNodes({all: true}, function(err, nodes) {
nodes.forEach((node) => {
if(node.ID === req.params.nodeId) {
node.remove({force: true}, function(nres) {
res.json(nres);
});
}
});
});
} else {
res.json({ message: 'Invalid operation.' });
}
});
router.get('/services', function(req, res) {
var docker = new Docker();
docker.listServices(function(err, nodes) {
res.json(nodes);
});
});
router.get('/tasks', function(req, res) {
var docker = new Docker();
docker.listTasks(function(err, nodes) {
res.json(nodes);
});
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);