-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
111 lines (89 loc) · 2.61 KB
/
index.ts
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
import startupValidation from "./util/startupValidation";
startupValidation();
import express, { Express, Request, Response } from "express";
import bodyParser from "body-parser";
import { getHosts, getToken, updateHost } from "./api";
import getTargetHosts from "./util/getTargetHosts";
import validateSecret from "./util/validateSecret";
const app: Express = express();
app.use(bodyParser.json());
const port = parseInt(process.env.PORT ?? "3000");
const host = process.env.HOST ?? "localhost";
app.post(
"/toggle",
async (req: Request<{}, {}, ToggleRequest>, res: Response) => {
if (!validateSecret(req)) {
res.status(401).send();
return;
}
const token = await getToken();
if (!token) {
res.status(401).send("Bad SA");
return;
}
const hosts = await getHosts(token);
if (!hosts) {
res.status(412).send("Unable to fetch nginx proxy manager hosts");
return;
}
const targetHosts = getTargetHosts(hosts);
if (!targetHosts) {
res.status(404).send("Target hosts are not found");
return;
}
const { green, blue } = targetHosts;
const newGreen = await updateHost(green.id, blue.forward_host, token);
const newBlue = await updateHost(blue.id, green.forward_host, token);
res.send({ green: newGreen, blue: newBlue });
}
);
app.get("/blue", async (req: Request<{}, {}, ToggleRequest>, res: Response) => {
if (!validateSecret(req)) {
res.status(401).send();
return;
}
const token = await getToken();
if (!token) {
res.status(401).send("Bad SA");
return;
}
const hosts = await getHosts(token);
if (!hosts) {
res.status(412).send("Unable to fetch nginx proxy manager hosts");
return;
}
const targetHosts = getTargetHosts(hosts);
if (!targetHosts) {
res.status(404).send("Target hosts are not found");
return;
}
res.send({ blue: targetHosts.blue });
});
app.get("/green", async (req: Request<{}, {}, ToggleRequest>, res: Response) => {
if (!validateSecret(req)) {
res.status(401).send();
return;
}
const token = await getToken();
if (!token) {
res.status(401).send("Bad SA");
return;
}
const hosts = await getHosts(token);
if (!hosts) {
res.status(412).send("Unable to fetch nginx proxy manager hosts");
return;
}
const targetHosts = getTargetHosts(hosts);
if (!targetHosts) {
res.status(404).send("Target hosts are not found");
return;
}
res.send({ green: targetHosts.green });
});
app.listen(3000, host, () => {
console.log(`⚡️[BillyGin]: Server is running at http://${host}:${port}`);
});
type ToggleRequest = {
secret: string;
};