forked from bofh69/FlyLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webServer.js
106 lines (93 loc) · 2.72 KB
/
webServer.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
//
// # Monitoring/Configuring WebbServer for FlyLight
//
var http = require('http');
var path = require('path');
var express = require('express');
var fs = require('fs');
// Creates a new instance of SimpleServer with the following options:
// * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
//
exports.init = function(cfg, aggHistory) {
var app = express();
var server = http.createServer(app);
app.use(express.static(path.resolve(__dirname, 'html')));
app.set('view engine', 'jade');
app.set('views', './views');
app.get('/colour', function (req, res) {
var response = "<html><body>";
if(aggHistory.isOk()) {
response += "FLY";
} else {
response += "DONT FLY";
}
response += "<br>";
response += history[0].history.join(" (1) <br>");
response += " (1) <br>";
response += history[1].history.join(" (2) <br>");
response += " (2) <br>";
response += "</body></html>";
res.send(response);
});
app.get('/svc/config', function (req, res) {
res.send(cfg);
});
app.post('/svc/config', function (req, res) {
var body = "";
try {
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
try {
var o = JSON.parse(body);
console.log(JSON.stringify(o));
} catch(e) {
console.log("Got error when receiving config: " + e);
res.send(422, "Got error when parsing the new config file: " + e);
return;
}
var newName = "config.json.new";
fs.writeFile(newName, body, function(err) {
if(!err) {
var cfgName = "config.json";
var backupName = "config.json.old";
try {
fs.unlinkSync(backupName);
} catch(e) {
console.log("Got error when removing old config file, continuing: " + e);
}
fs.linkSync(cfgName, backupName);
fs.renameSync(newName, cfgName);
res.send("OK", function() {
// TODO: This function doesn't exist, but it will crash the program and then it will restart...
restart();
});
} else {
res.send(500, err);
}
});
});
} catch(e) {
console.log("Got error when receiving config: " + e);
res.send(422, "Got error when parsing the new config file: " + e);
}
});
app.get('/svc/lampDrivers', function (req, res) {
res.send([
{"id": "milightRBG",
"name": "RGB"},
{"id": "milightRBGW",
"name": "RGBW"},
]);
});
/*
app.get('/', function (req, res) {
res.render('index', {title: 'Hej', message: 'Hello there!'});
});
*/
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
console.log("Server listening at", addr.address + ":" + addr.port);
});
}