-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (62 loc) · 1.76 KB
/
index.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
import compression from "compression";
import express from "express";
import { createServer } from "http";
import { fileURLToPath } from "url";
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { join } from "path";
import { hostname } from "os";
import cors from "cors";
import { createBareServer } from "@tomphttp/bare-server-node";
import proxy from "express-http-proxy";
const publicPath = fileURLToPath(new URL("./public/", import.meta.url));
let port = 8080;
const bare = createBareServer("/bare/");
const app = express();
const server = createServer();
app.use(compression());
app.use(cors());
app.use(express.static(publicPath, { maxAge: "1y" }));
app.use("/uv/", express.static(uvPath));
app.use(
'/cdn',
proxy(`https://3kh0-assets.silvereen.net`, {
proxyReqPathResolver: (req) => `/3kh0-assets/${req.url}`,
})
);
app.use((req, res) => {
res.status(404);
res.sendFile(join(publicPath, "404.html"));
});
server.on("request", (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else {
app(req, res);
}
});
server.on("upgrade", (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.on("listening", () => {
const address = server.address();
console.log("Listening on:");
console.log(`\thttp://localhost:${address.port}`);
console.log(`\thttp://${hostname()}:${address.port}`);
console.log(
`\thttp://${
address.family === "IPv6" ? `[${address.address}]` : address.address
}:${address.port}`
);
});
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
function shutdown() {
console.log("SIGTERM signal received: closing HTTP server");
server.close();
bare.close();
}
server.listen(port);