This commit is contained in:
2025-01-06 14:45:23 +01:00
commit b918c65cf7
8 changed files with 859 additions and 0 deletions

33
src/index.ts Normal file
View File

@ -0,0 +1,33 @@
import "dotenv/config";
import fastify from "fastify";
import { readdirSync } from "fs";
const app = fastify();
const routes = readdirSync(`${import.meta.dirname}/routes`, {
recursive: true,
});
for (let file of routes) {
if (typeof file === "string") {
if (!file.endsWith(".ts")) {
continue;
}
file = file.replaceAll("\\", "/");
let route = `/${file.split(".").slice(0, -1).join(".")}`;
route = route.replaceAll("_", ":");
const routePath = route.endsWith("/index") ? route.slice(0, -6) : route;
console.log(`Loading route: ${routePath}`);
app.register((await import(`./routes/${file}`)).default, {
prefix: routePath,
});
}
}
await app.listen({ port: Number(process.env.PORT), host: process.env.HOST });
console.log("App ready on", `http://${process.env.HOST}:${process.env.PORT}`);

7
src/routes/index.ts Normal file
View File

@ -0,0 +1,7 @@
import type { FastifyInstance } from "fastify";
export default (fastify: FastifyInstance) => {
fastify.get("/", () => {
return { appName: "code-containers" };
});
};