Skip to content

Commit 0621909

Browse files
committed
feat(app): integrate Hono framework for improved routing and error handling
- Replaced the previous application instance with Hono for better routing capabilities. - Updated OpenAPI route registration to use a new internal path structure. - Enhanced error handling by utilizing Hono's built-in error management features. - Adjusted logging to reflect the new route paths for OpenAPI documentation. Signed-off-by: Innei <tukon479@gmail.com>
1 parent 3f27b97 commit 0621909

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

be/apps/core/src/app.factory.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { env } from '@afilmory/env'
44
import type { HonoHttpApplication } from '@afilmory/framework'
55
import { createApplication, createLogger, createZodValidationPipe, HttpException } from '@afilmory/framework'
66
import { BizException } from 'core/errors'
7+
import { Hono } from 'hono'
78

89
import { PgPoolProvider } from './database/database.provider'
910
import { AllExceptionsFilter } from './filters/all-exceptions.filter'
@@ -31,10 +32,15 @@ const GlobalValidationPipe = createZodValidationPipe({
3132
const honoErrorLogger = createLogger('HonoErrorHandler')
3233

3334
export async function createConfiguredApp(options: BootstrapOptions = {}): Promise<HonoHttpApplication> {
34-
const app = await createApplication(AppModules, {
35-
globalPrefix: options.globalPrefix ?? '/api',
36-
})
37-
35+
const hono = new Hono()
36+
registerOpenApiRoutes(hono, { globalPrefix: options.globalPrefix ?? '/api' })
37+
const app = await createApplication(
38+
AppModules,
39+
{
40+
globalPrefix: options.globalPrefix ?? '/api',
41+
},
42+
hono,
43+
)
3844
const container = app.getContainer()
3945

4046
app.useGlobalFilters(new AllExceptionsFilter())
@@ -51,9 +57,6 @@ export async function createConfiguredApp(options: BootstrapOptions = {}): Promi
5157
const redisProvider = container.resolve(RedisProvider)
5258
await redisProvider.warmup()
5359

54-
registerOpenApiRoutes(app.getInstance(), { globalPrefix: options.globalPrefix ?? '/api' })
55-
56-
const hono = app.getInstance()
5760
hono.onError((error, context) => {
5861
if (error instanceof BizException) {
5962
return new Response(JSON.stringify(error.toResponse()), {

be/apps/core/src/openapi.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ function normalizePrefix(prefix: string): string {
2121

2222
export function registerOpenApiRoutes(app: Hono, options: RegisterOpenApiOptions): void {
2323
const prefix = normalizePrefix(options.globalPrefix)
24-
const specPath = `${prefix}/openapi.json`
25-
const docsPath = `${prefix}/docs`
2624

2725
const document = createOpenApiDocument(AppModules, {
2826
title: 'Core Service API',
@@ -32,14 +30,14 @@ export function registerOpenApiRoutes(app: Hono, options: RegisterOpenApiOptions
3230
servers: prefix ? [{ url: prefix }] : undefined,
3331
})
3432

35-
app.get(specPath || '/openapi.json', (context) => context.json(document))
33+
app.get('/internal/openapi.json', (context) => context.json(document))
3634

37-
app.get(docsPath || '/docs', (context) => {
35+
app.get('/internal/docs', (context) => {
3836
context.header('content-type', 'text/html; charset=utf-8')
39-
return context.html(renderScalarHtml(specPath || '/openapi.json'))
37+
return context.html(renderScalarHtml('/internal/openapi.json'))
4038
})
4139

42-
logger.info(`OpenAPI routes registered: http://localhost:${env.PORT}${docsPath}`)
40+
logger.info(`OpenAPI routes registered: http://localhost:${env.PORT}/internal/docs`)
4341
}
4442

4543
function renderScalarHtml(specUrl: string): string {

be/packages/framework/src/application.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ function isOnApplicationShutdownHook(value: unknown): value is OnApplicationShut
113113
}
114114

115115
export class HonoHttpApplication {
116-
private readonly app = new Hono()
117116
private readonly container: DependencyContainer
118117
private readonly globalEnhancers: GlobalEnhancerRegistry = createDefaultRegistry()
119118
private readonly registeredModules = new Set<Constructor>()
@@ -143,6 +142,7 @@ export class HonoHttpApplication {
143142
constructor(
144143
private readonly rootModule: Constructor,
145144
private readonly options: ApplicationOptions = {},
145+
private readonly app: Hono,
146146
) {
147147
this.logger = options.logger ?? createLogger('Framework')
148148
this.diLogger = this.logger.extend('DI')
@@ -1313,8 +1313,9 @@ export class HonoHttpApplication {
13131313
export async function createApplication(
13141314
rootModule: Constructor,
13151315
options: ApplicationOptions = {},
1316+
hono: Hono = new Hono(),
13161317
): Promise<HonoHttpApplication> {
1317-
const app = new HonoHttpApplication(rootModule, options)
1318+
const app = new HonoHttpApplication(rootModule, options, hono)
13181319
await app.init()
13191320
return app
13201321
}

0 commit comments

Comments
 (0)