-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
index.js
56 lines (45 loc) · 1.36 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
import { createBareServer } from "@tomphttp/bare-server-node";
import { createServer } from "node:http";
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { dynamicPath } from "@nebula-services/dynamic";
import express from "express";
const routes = [
["/", "index"],
["/math", "games"],
["/physics", "apps"],
["/settings", "settings"]
];
const navItems = [
["/", "Home"],
["/math", "Games"],
["/physics", "Apps"],
["/settings", "Settings"]
];
const bare = createBareServer("/bare/");
const app = express();
app.set("view engine", "ejs")
app.use(express.static("./public"));
app.use("/uv/", express.static(uvPath));
app.use("/dynamic/", express.static(dynamicPath))
for (const [path, page] of routes) {
app.get(path, (_, res) => res.render("layout", {
path,
navItems,
page
}));
}
app.use((_, res) => res.status(404).render("404"));
const httpServer = createServer();
httpServer.on("request", (req, res) => {
if (bare.shouldRoute(req)) bare.routeRequest(req, res);
else app(req, res);
});
httpServer.on("error", (err) => console.log(err));
httpServer.on("upgrade", (req, socket, head) => {
if (bare.shouldRoute(req)) bare.routeUpgrade(req, socket, head);
else socket.end();
});
httpServer.listen({ port: process.env.PORT || 8080 }, () => {
const addr = httpServer.address();
console.log(`\x1b[42m\x1b[1m shuttle\n Port: ${addr.port}\x1b[0m`);
});