2024-08-27 16:40:11 +02:00
|
|
|
import type { OpenAPIHono } from "@hono/zod-openapi";
|
2024-08-19 14:43:54 +02:00
|
|
|
import type { Config } from "~/packages/config-manager/config.type";
|
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-08-27 17:20:36 +02:00
|
|
|
export const createServer = (config: Config, app: OpenAPIHono<HonoEnv>) =>
|
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,
|
2024-04-18 03:53:42 +02:00
|
|
|
tls: config.http.tls.enabled
|
|
|
|
|
? {
|
|
|
|
|
key: Bun.file(config.http.tls.key),
|
|
|
|
|
cert: Bun.file(config.http.tls.cert),
|
|
|
|
|
passphrase: config.http.tls.passphrase,
|
|
|
|
|
ca: config.http.tls.ca
|
|
|
|
|
? Bun.file(config.http.tls.ca)
|
|
|
|
|
: undefined,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2024-04-07 07:30:49 +02:00
|
|
|
hostname: config.http.bind || "0.0.0.0", // defaults to "0.0.0.0"
|
2024-09-04 22:52:43 +02:00
|
|
|
async fetch(req, server) {
|
|
|
|
|
const output = await app.fetch(req, { ip: server.requestIP(req) });
|
|
|
|
|
|
|
|
|
|
if (config.logging.log_responses) {
|
|
|
|
|
await debugResponse(output.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
2024-04-07 07:30:49 +02:00
|
|
|
},
|
|
|
|
|
});
|