chore: scaffold turbo monorepo with hono api and react admin web
This commit is contained in:
49
apps/api/src/app.ts
Normal file
49
apps/api/src/app.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { cors } from 'hono/cors'
|
||||
import { Hono } from 'hono'
|
||||
import { requestId } from 'hono/request-id'
|
||||
import { secureHeaders } from 'hono/secure-headers'
|
||||
|
||||
import { auth } from '@/lib/auth'
|
||||
import { logger } from '@/lib/logger'
|
||||
import { errorHandler, notFoundHandler } from '@/lib/problem-details'
|
||||
import { healthModule } from '@/modules/health'
|
||||
import type { AppEnv } from '@/types'
|
||||
|
||||
const allowedOrigins = (process.env.CORS_ORIGIN ?? 'http://localhost:5173')
|
||||
.split(',')
|
||||
.map((origin) => origin.trim())
|
||||
.filter(Boolean)
|
||||
|
||||
export function createApp() {
|
||||
const app = new Hono<AppEnv>()
|
||||
|
||||
app.use(secureHeaders())
|
||||
app.use(cors({ origin: allowedOrigins, credentials: true }))
|
||||
app.use(requestId())
|
||||
app.use(async (c, next) => {
|
||||
const requestLogger = logger.child({ requestId: c.get('requestId') })
|
||||
c.set('logger', requestLogger)
|
||||
|
||||
const start = performance.now()
|
||||
await next()
|
||||
requestLogger.info(
|
||||
{
|
||||
method: c.req.method,
|
||||
path: c.req.path,
|
||||
status: c.res.status,
|
||||
durationMs: Math.round(performance.now() - start),
|
||||
},
|
||||
'request',
|
||||
)
|
||||
})
|
||||
|
||||
// BetterAuth owns every route under /api/auth/*.
|
||||
app.on(['POST', 'GET'], '/api/auth/*', (c) => auth.handler(c.req.raw))
|
||||
|
||||
app.route('/health', healthModule)
|
||||
|
||||
app.notFound(notFoundHandler)
|
||||
app.onError(errorHandler)
|
||||
|
||||
return app
|
||||
}
|
||||
Reference in New Issue
Block a user