2025-02-15 02:47:29 +01:00
|
|
|
import type { OpenAPIHono, z } from "@hono/zod-openapi";
|
2024-11-02 00:43:33 +01:00
|
|
|
import type { Server } from "bun";
|
2025-02-15 02:47:29 +01:00
|
|
|
import type { ConfigSchema } from "~/classes/config/schema.ts";
|
2024-08-27 17:20:36 +02:00
|
|
|
import type { HonoEnv } from "~/types/api";
|
2024-10-04 15:22:48 +02:00
|
|
|
import { debugResponse } from "./api.ts";
|
2024-03-11 03:04:14 +01:00
|
|
|
|
2024-11-02 00:43:33 +01:00
|
|
|
export const createServer = (
|
2025-02-15 02:47:29 +01:00
|
|
|
config: z.infer<typeof ConfigSchema>,
|
2024-11-02 00:43:33 +01:00
|
|
|
app: OpenAPIHono<HonoEnv>,
|
|
|
|
|
): Server =>
|
2024-04-07 07:30:49 +02:00
|
|
|
Bun.serve({
|
|
|
|
|
port: config.http.bind_port,
|
2024-05-13 23:36:46 +02:00
|
|
|
reusePort: true,
|
2025-02-15 02:47:29 +01:00
|
|
|
tls: config.http.tls
|
2024-04-18 03:53:42 +02:00
|
|
|
? {
|
2025-02-15 02:47:29 +01:00
|
|
|
key: config.http.tls.key.file,
|
|
|
|
|
cert: config.http.tls.cert.file,
|
2024-04-18 03:53:42 +02:00
|
|
|
passphrase: config.http.tls.passphrase,
|
2025-02-15 02:47:29 +01:00
|
|
|
ca: config.http.tls.ca?.file,
|
2024-04-18 03:53:42 +02:00
|
|
|
}
|
|
|
|
|
: undefined,
|
2025-02-15 02:47:29 +01:00
|
|
|
hostname: config.http.bind,
|
2024-11-02 00:43:33 +01:00
|
|
|
async fetch(req, server): Promise<Response> {
|
2024-09-04 22:52:43 +02:00
|
|
|
const output = await app.fetch(req, { ip: server.requestIP(req) });
|
|
|
|
|
|
2025-02-15 02:47:29 +01:00
|
|
|
if (config.logging.types.responses) {
|
2024-09-04 22:52:43 +02:00
|
|
|
await debugResponse(output.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
2024-04-07 07:30:49 +02:00
|
|
|
},
|
|
|
|
|
});
|