-
Notifications
You must be signed in to change notification settings - Fork 4
/
restarter.js
111 lines (99 loc) · 2.89 KB
/
restarter.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
const http = require("http");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
async function yo(_req, res) {
respond(res, { hi: "hello" });
}
const hostname = "0.0.0.0";
const port = process.env.PORT ? parseInt(process.env.PORT) : 3003;
function is2b() {
return (
process.env.SECOND_BRAIN === "true" || process.env.SECOND_BRAIN === "1"
);
}
http
.createServer(async (req, res) => {
if (req.method === "OPTIONS") {
return end(res, 200, "");
}
console.log("=>", req.url, req.method);
if (!req.url) return console.error("no url");
const url = req.url.split("?")[0];
if (req.method === "GET") {
console.log(url);
if (url === "/yo") {
yo(req, res);
}
}
if (req.method === "POST") {
if (url === "/restart") {
const body = await readBody(req);
if (body.password !== process.env.PASSWORD) {
return failure(res, "wrong password");
}
const scripts = [
`docker pull sphinxlightning/sphinx-swarm:latest`,
`docker stop sphinx-swarm`,
`docker rm sphinx-swarm`,
];
if (is2b()) {
scripts.push(`docker-compose -f second-brain.yml up sphinx-swarm -d`);
} else {
scripts.push(`docker-compose up sphinx-swarm -d`);
}
console.log("exec!");
try {
for (const sc of scripts) {
const { stdout, stderr } = await exec(sc);
console.log(stdout);
console.log("error:", stderr);
}
respond(res, { ok: true });
} catch (e) {
console.log("error:", e);
failure(res, e.message);
}
}
}
})
.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
function respond(res, response) {
end(res, 200, JSON.stringify(response));
}
function failure(res, err_msg) {
end(res, 401, JSON.stringify({ error: err_msg }));
}
function readBody(req) {
return new Promise((resolve, reject) => {
req.setEncoding("utf8");
req.on("data", function (data) {
try {
const body = JSON.parse(data);
resolve(body);
} catch (e) {
reject(e);
}
});
});
}
function end(res, status, data) {
const headers = {};
headers["Content-Type"] = "application/json";
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = false;
headers["Access-Control-Max-Age"] = "86400"; // 24 hours
headers["Access-Control-Allow-Headers"] =
"X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
res.writeHead(200, headers);
res.end(data);
}
/*
curl http://localhost:3003/yo
curl --header "Content-Type: application/json" \
--request POST \
--data '{"password":"123"}' \
http://localhost:3003/restart
*/