This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (39 loc) · 1.67 KB
/
app.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
const exec = require('child_process').exec;
const express = require('express');
const pug = require('pug');
const moment = require('moment');
const ansi_up = require('ansi_up');
let app = express();
app.use('/assets', express.static('assets'));
app.get('/logs/:container', (req, res) => {
new Promise((resolve, reject) => exec('docker logs ' + req.param('container') + ' --tail 2560', (error, stdout, stderr) => { //2560
if (error) {
reject(error);
} else if (stdout && stdout.trim() !== '') {
resolve(stdout);
} else {
reject(stderr);
}
}))
.then(logs => logs.split(' ').join(' '))
.then(logs => ansi_up.ansi_to_html(logs))
.then(logs => pug.renderFile('./logs.pug', {pretty: true, logs}))
.then(d => res.status(200).set('Content-type', 'text/html').send(d))
.catch(e => res.status(500).set('Content-type', 'text/plain').send(e.stack ? e.stack : e));
});
app.get('/*', (req, res) => {
new Promise((resolve, reject) => exec('docker inspect $(docker ps --all -q)', (error, stdout, stderr) => {
if (error) {
reject(error);
} else if (stdout && stdout.trim() !== '') {
resolve(stdout);
} else {
reject(stderr);
}
}))
.then(data => JSON.parse(data))
.then(containers => pug.renderFile('./index.pug', {pretty: true, containers, moment}))
.then(d => res.status(200).set('Content-type', 'text/html').send(d))
.catch(e => res.status(500).set('Content-type', 'text/plain').send(e.stack ? e.stack : e));
});
app.listen(process.env.PORT || 8080, _ => console.log('Server started.'));