-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
117 lines (107 loc) · 2.82 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
106
107
108
109
110
111
112
113
114
115
116
117
const url = "https://" + process.env.SERVICE_URL;
const port = process.env.PORT || 3000;
const keepalive_interval = process.env.KEEPALIVE || 10;
const express = require("express");
const app = express();
var exec = require("child_process").exec;
const { createProxyMiddleware } = require("http-proxy-middleware");
var request = require("request");
var fs = require("fs");
var path = require("path");
app.get("/", function (req, res) {
res.send("hello world");
});
app.get("/start", function (req, res) {
let cmdStr = "[ -e entrypoint.sh ] && bash entrypoint.sh; chmod +x ./web.js && ./web.js -c ./config.json >/dev/null 2>&1 &";
exec(cmdStr, function (err, stdout, stderr) {
if (err) {
res.send("Web start error:" + err);
}
else {
res.send("Web start success!");
}
});
});
// keepalive begin
function keepalive() {
request(url, function (error, response, body) {
if (error) {
console.log("Keepalive error: " + error);
}
//else {
// console.log("Keepalive success, response: " + body);
//}
});
exec("pgrep -laf web.js", function (err, stdout, stderr) {
if (stdout.includes("./web.js -c ./config.json")) {
console.log("Web service is working");
}
else {
exec(
"chmod +x web.js && ./web.js -c ./config.json >/dev/null 2>&1 &", function (err, stdout, stderr) {
if (err) {
console.log("Keepalive restart error: " + err);
}
else {
console.log("Keepalive restart success!");
}
}
);
}
});
}
setInterval(keepalive, keepalive_interval * 1000);
app.get("/download", function (req, res) {
download_web((err) => {
if (err) {
res.send("web.js downloaded successfully!");
}
else {
res.send("web.js download failed: " + err);
}
});
});
app.use(
"/",
createProxyMiddleware({
changeOrigin: true,
onProxyReq: function onProxyReq(proxyReq, req, res) {},
pathRewrite: {
"^/": "/"
},
target: "http://127.0.0.1:8080/",
ws: true
})
);
function download_web(callback) {
let fileName = "web.js";
let web_url =
"https://github.com/fscarmen2/Argo-X-Container-PaaS/raw/main/files/web.js";
let stream = fs.createWriteStream(path.join("./", fileName));
request(web_url)
.pipe(stream)
.on("close", function (err) {
if (err) {
callback("Failed to download web.js: " + err);
}
else {
callback(null);
}
});
}
download_web((err) => {
if (err) {
console.log("Failed to download web.js: " + err);
}
else {
console.log("web.js downloaded successfully!");
}
});
exec("bash entrypoint.sh", function (err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));