2023-09-11 05:54:14 +02:00
|
|
|
import { getConfig } from "@config";
|
2023-09-15 05:21:38 +02:00
|
|
|
import { appendFile } from "fs/promises";
|
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-15 07:08:59 +02:00
|
|
|
console.log("[+] Starting Lysand...");
|
2023-09-11 05:46:20 +02:00
|
|
|
|
2023-09-11 05:54:14 +02:00
|
|
|
const config = getConfig();
|
2023-09-15 05:21:38 +02:00
|
|
|
const requests_log = Bun.file(process.cwd() + "/logs/requests.log");
|
|
|
|
|
|
|
|
|
|
if (!(await requests_log.exists())) {
|
|
|
|
|
console.log("[+] requests.log does not exist, creating it...");
|
|
|
|
|
await Bun.write(process.cwd() + "/logs/requests.log", "");
|
|
|
|
|
}
|
2023-09-11 05:54:14 +02:00
|
|
|
|
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) {
|
2023-09-15 05:21:38 +02:00
|
|
|
if (config.logging.log_requests_verbose) {
|
|
|
|
|
await appendFile(
|
|
|
|
|
`${process.cwd()}/logs/requests.log`,
|
|
|
|
|
`[${new Date().toISOString()}] ${req.method} ${
|
|
|
|
|
req.url
|
|
|
|
|
}\n\tHeaders:\n`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Add headers
|
|
|
|
|
|
|
|
|
|
const headers = req.headers.entries();
|
|
|
|
|
|
|
|
|
|
for (const [key, value] of headers) {
|
|
|
|
|
await appendFile(
|
|
|
|
|
`${process.cwd()}/logs/requests.log`,
|
|
|
|
|
`\t\t${key}: ${value}\n`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const body = await req.clone().text();
|
|
|
|
|
|
|
|
|
|
await appendFile(
|
|
|
|
|
`${process.cwd()}/logs/requests.log`,
|
|
|
|
|
`\tBody:\n\t${body}\n`
|
|
|
|
|
);
|
|
|
|
|
} else if (config.logging.log_requests) {
|
|
|
|
|
await appendFile(
|
|
|
|
|
process.cwd() + "/logs/requests.log",
|
|
|
|
|
`[${new Date().toISOString()}] ${req.method} ${req.url}\n`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-11 05:31:08 +02:00
|
|
|
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-15 07:08:59 +02:00
|
|
|
console.log("[+] Lysand started!");
|