import type { RuntimeMode } from "../shared/api"; import type { ServerConfig } from "./config"; import type { StaticAssets } from "./static"; import { createApiError, jsonResponse } from "./helpers"; import { handleHealth } from "./routes/health"; import { serveStaticAsset } from "./static"; export interface StartServerOptions { config: ServerConfig; mode: RuntimeMode; staticAssets?: StaticAssets; } export function startServer(options: StartServerOptions) { const { config, mode, staticAssets } = options; const server = Bun.serve({ fetch(req) { if (staticAssets) { return serveStaticAsset(new URL(req.url).pathname, staticAssets); } return new Response("Frontend is served by Vite dev server on :5173", { status: 404 }); }, hostname: config.host, port: config.port, routes: { "/api/*": () => jsonResponse(createApiError("API route not found", 404), { mode, status: 404 }), "/health": { GET: () => handleHealth(mode), }, }, }); console.log(`{{app-name}} listening on ${server.url}`); return server; }