-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (49 loc) · 1.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
import express from "express";
import fs from "fs";
import http from "http";
import { createProxyMiddleware } from "http-proxy-middleware";
import https from "https";
const wsAddress = process.env.WS_ADDRESS || "ws://127.0.0.1:8080"
const httpAddress = process.env.K8S_API_ADDRESS || "https://kubernetes.default.svc"
const port = process.env.SERVER_PORT || 3000
const host = process.env.NODE_ENV === "production" ? "0.0.0.0" : "127.0.0.1"
console.log(`Server starting. wsAddress=${wsAddress}, httpAddress=${httpAddress}, port=${port}, host=${host}, env=${process.env.NODE_ENV}`);
const app = express();
const server = http.createServer(app);
let k8sToken;
let certs;
if (fs.existsSync("/var/run/secrets/kubernetes.io/serviceaccount/token")) {
console.log("Authenticated with Kubernetes API.");
k8sToken = fs.readFileSync("/var/run/secrets/kubernetes.io/serviceaccount/token", "utf-8");
certs = [
fs.readFileSync("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt", "utf-8"),
]
} else {
console.log("Using Kubernetes API without authentication.");
k8sToken = undefined;
certs = [];
}
// Proxy Kubernetes API
app.use("/k8s", createProxyMiddleware({
target: httpAddress,
pathRewrite: { "^/k8s": "" },
logLevel: "debug",
secure: k8sToken !== undefined,
changeOrigin: k8sToken !== undefined,
agent: new https.Agent({
rejectUnauthorized: k8sToken !== undefined,
ca: certs
}),
followRedirects: true,
headers: {
"Authorization": `Bearer ${k8sToken}`,
},
}));
// Proxy WebSockets
app.use("/ws", createProxyMiddleware(wsAddress, { ws: true }));
// Serve built static content
app.use("/", express.static("dist"));
process.on("SIGINT", server.close);
process.on("SIGTERM", server.close);
server.listen(port, host);
console.log(`Server listening on ${host}:${port}.`);