server/utils/server.ts

33 lines
1 KiB
TypeScript
Raw Normal View History

import type { OpenAPIHono, z } from "@hono/zod-openapi";
import type { Server } from "bun";
import type { ConfigSchema } from "~/classes/config/schema.ts";
import type { HonoEnv } from "~/types/api";
import { debugResponse } from "./api.ts";
export const createServer = (
config: z.infer<typeof ConfigSchema>,
app: OpenAPIHono<HonoEnv>,
): Server =>
2024-04-07 07:30:49 +02:00
Bun.serve({
port: config.http.bind_port,
reusePort: true,
tls: config.http.tls
? {
key: config.http.tls.key.file,
cert: config.http.tls.cert.file,
passphrase: config.http.tls.passphrase,
ca: config.http.tls.ca?.file,
}
: undefined,
hostname: config.http.bind,
async fetch(req, server): Promise<Response> {
const output = await app.fetch(req, { ip: server.requestIP(req) });
if (config.logging.types.responses) {
await debugResponse(output.clone());
}
return output;
2024-04-07 07:30:49 +02:00
},
});