55 */
66
77import { Hono } from "hono" ;
8+ import { logger } from "hono/logger" ;
9+ import { requestId } from "hono/request-id" ;
10+ import { secureHeaders } from "hono/secure-headers" ;
811import { parseArgs } from "node:util" ;
912import { getPlatformProxy } from "wrangler" ;
1013import api from "./index.js" ;
1114import { createAuth } from "./lib/auth.js" ;
1215import type { AppContext } from "./lib/context.js" ;
1316import { createDb } from "./lib/db.js" ;
1417import type { Env } from "./lib/env.js" ;
18+ import { errorHandler , notFoundHandler } from "./lib/middleware.js" ;
1519
1620const { values : args } = parseArgs ( {
1721 args : Bun . argv . slice ( 2 ) ,
@@ -27,6 +31,15 @@ type CloudflareEnv = {
2731
2832const app = new Hono < AppContext > ( ) ;
2933
34+ // Error and 404 handlers (must be on top-level app)
35+ app . onError ( errorHandler ) ;
36+ app . notFound ( notFoundHandler ) ;
37+
38+ // Standard middleware
39+ app . use ( secureHeaders ( ) ) ;
40+ app . use ( requestId ( ) ) ;
41+ app . use ( logger ( ) ) ;
42+
3043// persist:true maintains state across restarts in .wrangler directory
3144const cf = await getPlatformProxy < CloudflareEnv > ( {
3245 configPath : "./wrangler.jsonc" ,
@@ -37,7 +50,7 @@ const cf = await getPlatformProxy<CloudflareEnv>({
3750// Inject context with two database connections:
3851// - db: Hyperdrive caching for read-heavy queries
3952// - dbDirect: No cache for writes and transactions
40- app . use ( "*" , async ( c , next ) => {
53+ app . use ( async ( c , next ) => {
4154 const db = createDb ( cf . env . HYPERDRIVE_CACHED ) ;
4255 const dbDirect = createDb ( cf . env . HYPERDRIVE_DIRECT ) ;
4356
0 commit comments