seahaven-ap/packages/api/src/app.ts

29 lines
861 B
TypeScript
Raw Normal View History

import { Hono } from "hono";
import type { ApiEnv } from "./env.js";
import type { Db } from "./db/client.js";
import { createAuthMiddleware, type AppBindings } from "./auth/middleware.js";
import { createHealthRoutes } from "./routes/health.js";
import { createMeRoutes } from "./routes/me.js";
export function createApp(env: ApiEnv, handle: Db) {
const app = new Hono<AppBindings>();
const auth = createAuthMiddleware(env, handle);
app.route("/", createHealthRoutes(handle));
const api = new Hono<AppBindings>();
api.use("*", auth);
api.route("/", createMeRoutes());
app.route("/api", api);
app.notFound((c) => c.json({ error: "Not found." }, 404));
app.onError((error, c) => {
console.error(error);
return c.json({ error: "Internal server error." }, 500);
});
return app;
}
export type App = ReturnType<typeof createApp>;