2023-10-29 19:28:07 +00:00
|
|
|
// Import the framework and instantiate it
|
2026-01-14 12:44:20 -08:00
|
|
|
import Fastify from "fastify";
|
|
|
|
|
import fastifyStatic from "@fastify/static";
|
|
|
|
|
import { fastifyWebsocket } from "@fastify/websocket";
|
2023-10-29 19:28:07 +00:00
|
|
|
import { routeList } from "./routelist.ts";
|
2024-04-03 20:29:27 -07:00
|
|
|
import { attachRoute } from "./util/routewrap.ts";
|
2023-10-29 19:28:07 +00:00
|
|
|
|
|
|
|
|
const server = Fastify({
|
2026-01-14 12:44:20 -08:00
|
|
|
logger: true,
|
2023-10-29 19:28:07 +00:00
|
|
|
});
|
|
|
|
|
|
2024-04-03 20:29:27 -07:00
|
|
|
server.register(fastifyWebsocket);
|
|
|
|
|
|
2023-10-29 19:28:07 +00:00
|
|
|
server.register(fastifyStatic, {
|
2026-01-14 12:44:20 -08:00
|
|
|
root: new URL("public", import.meta.url).toString().slice("file://".length),
|
|
|
|
|
prefix: "/",
|
2023-10-29 19:28:07 +00:00
|
|
|
});
|
|
|
|
|
|
2026-01-14 12:44:20 -08:00
|
|
|
routeList.forEach((firRoute) => {
|
2024-04-03 20:29:27 -07:00
|
|
|
attachRoute(server, firRoute);
|
2024-03-31 19:40:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.setNotFoundHandler((req, res) => {
|
|
|
|
|
if (!req.url.startsWith("/api")) {
|
2026-01-14 12:44:20 -08:00
|
|
|
res.sendFile("index.html");
|
2024-03-31 19:40:06 -07:00
|
|
|
}
|
|
|
|
|
});
|
2023-10-29 19:28:07 +00:00
|
|
|
|
|
|
|
|
// Run the server!
|
|
|
|
|
try {
|
|
|
|
|
// Note: host needs to be 0.0.0.0 rather than omitted or localhost, otherwise
|
|
|
|
|
// it always returns an empty reply when used inside docker...
|
|
|
|
|
// See: https://github.com/fastify/fastify/issues/935
|
2026-01-14 12:44:20 -08:00
|
|
|
await server.listen({
|
|
|
|
|
port: parseInt(process.env["PORT"] ?? "3000"),
|
|
|
|
|
host: "0.0.0.0",
|
|
|
|
|
});
|
2023-10-29 19:28:07 +00:00
|
|
|
} catch (err) {
|
2026-01-14 12:44:20 -08:00
|
|
|
server.log.error(err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|