Files
picobook/src/server/index.ts
T
2026-06-10 21:47:30 -04:00

42 lines
1.0 KiB
TypeScript

// Import the framework and instantiate it
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import { fastifyWebsocket } from "@fastify/websocket";
import { routeList } from "./routelist.ts";
import { attachRoute } from "./util/routewrap.ts";
const server = Fastify({
logger: true,
});
server.register(fastifyWebsocket);
server.register(fastifyStatic, {
root: new URL("public", import.meta.url).toString().slice("file://".length),
prefix: "/",
});
routeList.forEach((firRoute) => {
attachRoute(server, firRoute);
});
server.setNotFoundHandler((req, res) => {
if (!req.url.startsWith("/api")) {
res.sendFile("index.html");
}
});
// 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
await server.listen({
port: parseInt(process.env["PORT"] ?? "3000"),
host: "0.0.0.0",
});
} catch (err) {
server.log.error(err);
process.exit(1);
}