import { Hono } from "hono"; import { requestLogger } from "@atbb/logger/middleware"; import { createApiRoutes } from "../routes/index.js"; import type { AppContext } from "./app-context.js"; /** * Create the Hono application with routes and middleware. * Routes can access the database and other services via the injected context. */ export function createApp(ctx: AppContext) { const app = new Hono(); app.use("*", requestLogger(ctx.logger)); // Global error handler for unhandled errors app.onError((err, c) => { ctx.logger.error("Unhandled error in route handler", { path: c.req.path, method: c.req.method, error: err.message, stack: err.stack, }); return c.json( { error: "An internal error occurred. Please try again later.", ...(process.env.NODE_ENV !== "production" && { details: err.message, }), }, 500 ); }); // OAuth client metadata endpoint // Serve metadata from the OAuth client library app.get("/.well-known/oauth-client-metadata", (c) => { return c.json(ctx.oauthClient.clientMetadata); }); app.route("/api", createApiRoutes(ctx)); return app; }