From fbecb2ebda6707e8d4647536eba8ae010f38aa17 Mon Sep 17 00:00:00 2001 From: Alec Jacobs Date: Tue, 16 Apr 2024 16:10:29 -0700 Subject: [PATCH] feat(backend-app-api): add ability to configure node server in root http router Signed-off-by: Alec Jacobs --- packages/backend-app-api/api-report.md | 5 +++ .../rootHttpRouterServiceFactory.ts | 44 +++++++++++-------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md index 63f43eff2e..5720c1c60f 100644 --- a/packages/backend-app-api/api-report.md +++ b/packages/backend-app-api/api-report.md @@ -37,6 +37,7 @@ import { RootHttpRouterService } from '@backstage/backend-plugin-api'; import { RootLifecycleService } from '@backstage/backend-plugin-api'; import { RootLoggerService } from '@backstage/backend-plugin-api'; import { SchedulerService } from '@backstage/backend-plugin-api'; +import type { Server } from 'node:http'; import { ServiceFactory } from '@backstage/backend-plugin-api'; import { ServiceFactoryOrFunction } from '@backstage/backend-plugin-api'; import { TokenManagerService } from '@backstage/backend-plugin-api'; @@ -287,6 +288,8 @@ export interface RootHttpRouterConfigureContext { // (undocumented) app: Express_2; // (undocumented) + applyDefaults: () => void; + // (undocumented) config: RootConfigService; // (undocumented) lifecycle: LifecycleService; @@ -296,6 +299,8 @@ export interface RootHttpRouterConfigureContext { middleware: MiddlewareFactory; // (undocumented) routes: RequestHandler; + // (undocumented) + server: Server; } // @public (undocumented) diff --git a/packages/backend-app-api/src/services/implementations/rootHttpRouter/rootHttpRouterServiceFactory.ts b/packages/backend-app-api/src/services/implementations/rootHttpRouter/rootHttpRouterServiceFactory.ts index 4c410e25ea..a469a2f303 100644 --- a/packages/backend-app-api/src/services/implementations/rootHttpRouter/rootHttpRouterServiceFactory.ts +++ b/packages/backend-app-api/src/services/implementations/rootHttpRouter/rootHttpRouterServiceFactory.ts @@ -22,6 +22,7 @@ import { LoggerService, } from '@backstage/backend-plugin-api'; import express, { RequestHandler, Express } from 'express'; +import type { Server } from 'node:http'; import { createHttpServer, MiddlewareFactory, @@ -34,11 +35,13 @@ import { DefaultRootHttpRouter } from './DefaultRootHttpRouter'; */ export interface RootHttpRouterConfigureContext { app: Express; + server: Server; middleware: MiddlewareFactory; routes: RequestHandler; config: RootConfigService; logger: LoggerService; lifecycle: LifecycleService; + applyDefaults: () => void; } /** @@ -54,15 +57,8 @@ export type RootHttpRouterFactoryOptions = { configure?(context: RootHttpRouterConfigureContext): void; }; -function defaultConfigure(context: RootHttpRouterConfigureContext) { - const { app, routes, middleware } = context; - app.use(middleware.helmet()); - app.use(middleware.cors()); - app.use(middleware.compression()); - app.use(middleware.logging()); - app.use(routes); - app.use(middleware.notFound()); - app.use(middleware.error()); +function defaultConfigure({ applyDefaults }: RootHttpRouterConfigureContext) { + applyDefaults(); } /** @public */ @@ -81,22 +77,32 @@ export const rootHttpRouterServiceFactory = createServiceFactory( const router = DefaultRootHttpRouter.create({ indexPath }); const middleware = MiddlewareFactory.create({ config, logger }); - - configure({ - app, - routes: router.handler(), - middleware, - config, - logger, - lifecycle, - }); - + const routes = router.handler(); const server = await createHttpServer( app, readHttpServerOptions(config.getOptionalConfig('backend')), { logger }, ); + configure({ + app, + server, + routes, + middleware, + config, + logger, + lifecycle, + applyDefaults() { + app.use(middleware.helmet()); + app.use(middleware.cors()); + app.use(middleware.compression()); + app.use(middleware.logging()); + app.use(routes); + app.use(middleware.notFound()); + app.use(middleware.error()); + }, + }); + lifecycle.addShutdownHook(() => server.stop()); await server.start();