-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (50 loc) · 1.71 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
var exec = require('child_process').exec;
const configPath = "config.json";
const serverPath = "server.json";
const loadConfig = function () {
let configFile = fs.readFileSync(configPath);
const config = JSON.parse(configFile.toString());
let serverFile = fs.readFileSync(serverPath);
const servers = JSON.parse(serverFile.toString());
const upstreamFilePath = config.upstream_file;
let upstreamContent = "";
if (config.sorted) {
config.upstreams.sort((a, b) => {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
});
}
config.upstreams.forEach(upstream => {
upstreamContent += `upstream ${upstream.name} {\n`;
upstreamContent += "\tip_hash;\n\n"
servers.forEach(server => {
if (server.active) {
upstreamContent += `\tserver\t${server.name}:${upstream.port};\n`;
}
});
upstreamContent += "}\n\n"
});
fs.writeFile(upstreamFilePath, upstreamContent, function (err) {
if (err) console.log(err);
console.log(`${new Date()} Successfully written config file to ${upstreamFilePath}.`);
});
if (config.reload_nginx_config) {
exec(config.reload_nginx_command, function (err, stdout, stderr) {
if (err) {
console.error("Something is wrong.");
console.error(err);
}
else {
console.info("nginx config reloaded.");
console.info(stdout);
}
});
}
}
fs.watchFile(configPath, loadConfig);
fs.watchFile(serverPath, loadConfig);
loadConfig();