-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (60 loc) · 1.88 KB
/
index.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
var express = require('express');
var app = express();
var fs = require('fs');
var glob = require('glob');
var path = require("path");
var yaml = require("js-yaml");
var child_process = require("child_process");
var http = require('http').Server(app);
var io = require('socket.io')(http);
var bodyParser = require('body-parser');
app.use(bodyParser.text());
app.use(express.static('public'));
app.set('view engine', 'pug');
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.post('/log', function(req, res){
var host = req.get("host").toLowerCase();
var ui = req.get("ui").toLowerCase();
console.log(req.body);
io.emit(host + "," + ui, req.body);
io.emit(host + "," + "all", req.body);
res.end();
});
app.get('/charts', function(req, res){
var files = glob.sync(__dirname + "/chart/*.yml");
var ymlFiles = files.map(function(file){
return path.basename(file);
});
var files = glob.sync(__dirname + "/public/generated/**/*.png");
var pngFiles = files.map(function(file){
var dirs = path.dirname(file).split("/");
var dir = dirs[dirs.length - 1];
return "/generated/" + dir + "/" + path.basename(file);
});
res.render("chart.pug", {
ymlFiles: ymlFiles,
pngFiles: pngFiles
});
});
app.get('/charts/:filename', function(req, res){
var json = yaml.safeLoad(fs.readFileSync(__dirname + '/chart/' + req.params.filename, 'utf-8'));
res.json(json);
});
app.post('/charts/generate', function(req, res){
var args = Object.keys(req.query).map(function(q){
return req.query[q];
});
var cmd = __dirname + "/chart/chart.sh " + __dirname + "/chart/" + args.join(" ");
console.log("cmd: " + cmd);
child_process.exec(cmd, {
cwd: __dirname + "/chart"
}, function(err, stdout, stderr){
console.log(stdout);
return res.end();
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});