feat(backend-app-api): add ability to configure node server in root http router

Signed-off-by: Alec Jacobs <cajacobs5401@gmail.com>
This commit is contained in:
Alec Jacobs
2024-04-16 16:10:29 -07:00
parent 96ee5194b5
commit fbecb2ebda
2 changed files with 30 additions and 19 deletions
+5
View File
@@ -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)
@@ -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();