ccyyss commited on
Commit
98c7051
·
1 Parent(s): 9c39dab

Upload index.js

Browse files
Files changed (1) hide show
  1. src/index.js +74 -0
src/index.js ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { createBareServer } from "@tomphttp/bare-server-node";
2
+ import express from "express";
3
+ import { createServer } from "node:http";
4
+ import { publicPath } from "ultraviolet-static";
5
+ import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
6
+ import { join } from "node:path";
7
+ import { hostname } from "node:os";
8
+
9
+ const bare = createBareServer("/bare/");
10
+ const app = express();
11
+
12
+ // Load our publicPath first and prioritize it over UV.
13
+ app.use(express.static(publicPath));
14
+ // Load vendor files last.
15
+ // The vendor's uv.config.js won't conflict with our uv.config.js inside the publicPath directory.
16
+ app.use("/uv/", express.static(uvPath));
17
+
18
+ // Error for everything else
19
+ app.use((req, res) => {
20
+ res.status(404);
21
+ res.sendFile(join(publicPath, "404.html"));
22
+ });
23
+
24
+ const server = createServer();
25
+
26
+ server.on("request", (req, res) => {
27
+ if (bare.shouldRoute(req)) {
28
+ bare.routeRequest(req, res);
29
+ } else {
30
+ app(req, res);
31
+ }
32
+ });
33
+
34
+ server.on("upgrade", (req, socket, head) => {
35
+ if (bare.shouldRoute(req)) {
36
+ bare.routeUpgrade(req, socket, head);
37
+ } else {
38
+ socket.end();
39
+ }
40
+ });
41
+
42
+ let port = parseInt(process.env.PORT || "");
43
+
44
+ if (isNaN(port)) port = 8080;
45
+
46
+ server.on("listening", () => {
47
+ const address = server.address();
48
+
49
+ // by default we are listening on 0.0.0.0 (every interface)
50
+ // we just need to list a few
51
+ console.log("Listening on:");
52
+ console.log(`\thttp://localhost:${address.port}`);
53
+ console.log(`\thttp://${hostname()}:${address.port}`);
54
+ console.log(
55
+ `\thttp://${
56
+ address.family === "IPv6" ? `[${address.address}]` : address.address
57
+ }:${address.port}`
58
+ );
59
+ });
60
+
61
+ // https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html
62
+ process.on("SIGINT", shutdown);
63
+ process.on("SIGTERM", shutdown);
64
+
65
+ function shutdown() {
66
+ console.log("SIGTERM signal received: closing HTTP server");
67
+ server.close();
68
+ bare.close();
69
+ process.exit(0);
70
+ }
71
+
72
+ server.listen({
73
+ port,
74
+ });