server/index.ts

38 lines
947 B
TypeScript
Raw Normal View History

2023-09-11 05:54:14 +02:00
import { getConfig } from "@config";
2023-09-12 22:48:10 +02:00
import "reflect-metadata";
2023-09-14 04:25:45 +02:00
import { AppDataSource } from "~database/datasource";
2023-09-11 05:54:14 +02:00
2023-09-11 05:31:08 +02:00
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: process.cwd() + "/server/api",
2023-09-13 02:29:13 +02:00
});
2023-09-11 05:31:08 +02:00
2023-09-11 05:46:20 +02:00
console.log("[+] Starting FediProject...");
2023-09-11 05:54:14 +02:00
const config = getConfig();
2023-09-14 04:25:45 +02:00
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
2023-09-11 05:31:08 +02:00
Bun.serve({
2023-09-11 05:54:14 +02:00
port: config.http.port,
2023-09-14 04:25:45 +02:00
hostname: config.http.base_url || "0.0.0.0", // defaults to "0.0.0.0"
2023-09-11 05:31:08 +02:00
async fetch(req) {
const matchedRoute = router.match(req);
if (matchedRoute) {
2023-09-11 05:46:20 +02:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
2023-09-13 02:29:13 +02:00
return (await import(matchedRoute.filePath)).default(
req,
matchedRoute
) as Response | Promise<Response>;
2023-09-11 05:31:08 +02:00
} else {
2023-09-11 05:46:20 +02:00
return new Response(undefined, {
2023-09-11 05:31:08 +02:00
status: 404,
statusText: "Route not found",
});
}
},
});
2023-09-11 05:46:20 +02:00
2023-09-13 02:29:13 +02:00
console.log("[+] FediProject started!");