Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet-Node
Path: blob/main/src/index.js
516 views
1
import { createServer } from "node:http";
2
import { join } from "node:path";
3
import { hostname } from "node:os";
4
import wisp from "wisp-server-node";
5
import Fastify from "fastify";
6
import fastifyStatic from "@fastify/static";
7
8
// static paths
9
import { publicPath } from "ultraviolet-static";
10
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
11
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
12
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
13
14
const fastify = Fastify({
15
serverFactory: (handler) => {
16
return createServer()
17
.on("request", (req, res) => {
18
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
19
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
20
handler(req, res);
21
})
22
.on("upgrade", (req, socket, head) => {
23
if (req.url.endsWith("/wisp/")) wisp.routeRequest(req, socket, head);
24
else socket.end();
25
});
26
},
27
});
28
29
fastify.register(fastifyStatic, {
30
root: publicPath,
31
decorateReply: true,
32
});
33
34
fastify.get("/uv/uv.config.js", (req, res) => {
35
return res.sendFile("uv/uv.config.js", publicPath);
36
});
37
38
fastify.register(fastifyStatic, {
39
root: uvPath,
40
prefix: "/uv/",
41
decorateReply: false,
42
});
43
44
fastify.register(fastifyStatic, {
45
root: epoxyPath,
46
prefix: "/epoxy/",
47
decorateReply: false,
48
});
49
50
fastify.register(fastifyStatic, {
51
root: baremuxPath,
52
prefix: "/baremux/",
53
decorateReply: false,
54
});
55
56
fastify.server.on("listening", () => {
57
const address = fastify.server.address();
58
59
// by default we are listening on 0.0.0.0 (every interface)
60
// we just need to list a few
61
console.log("Listening on:");
62
console.log(`\thttp://localhost:${address.port}`);
63
console.log(`\thttp://${hostname()}:${address.port}`);
64
console.log(
65
`\thttp://${
66
address.family === "IPv6" ? `[${address.address}]` : address.address
67
}:${address.port}`
68
);
69
});
70
71
process.on("SIGINT", shutdown);
72
process.on("SIGTERM", shutdown);
73
74
function shutdown() {
75
console.log("SIGTERM signal received: closing HTTP server");
76
fastify.close();
77
process.exit(0);
78
}
79
80
let port = parseInt(process.env.PORT || "");
81
82
if (isNaN(port)) port = 8080;
83
84
fastify.listen({
85
port: port,
86
host: "0.0.0.0",
87
});
88
89