Merge pull request #16224 from backstage/freben/config-options

Moved the options of the config and root HTTP router services out to their factories
This commit is contained in:
Fredrik Adelöw
2023-02-07 11:38:50 +01:00
committed by GitHub
4 changed files with 55 additions and 48 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-app-api': patch
---
Moved the options of the `config` and `rootHttpRouter` services out to the factories themselves, where they belong
+6 -2
View File
@@ -53,7 +53,9 @@ export interface Backend {
export const cacheFactory: () => ServiceFactory<PluginCacheManager>;
// @public (undocumented)
export const configFactory: () => ServiceFactory<ConfigService>;
export const configFactory: (
options?: ConfigFactoryOptions | undefined,
) => ServiceFactory<ConfigService>;
// @public (undocumented)
export interface ConfigFactoryOptions {
@@ -230,7 +232,9 @@ export interface RootHttpRouterConfigureOptions {
}
// @public (undocumented)
export const rootHttpRouterFactory: () => ServiceFactory<RootHttpRouterService>;
export const rootHttpRouterFactory: (
options?: RootHttpRouterFactoryOptions | undefined,
) => ServiceFactory<RootHttpRouterService>;
// @public (undocumented)
export type RootHttpRouterFactoryOptions = {
@@ -35,13 +35,14 @@ export interface ConfigFactoryOptions {
}
/** @public */
export const configFactory = createServiceFactory({
service: coreServices.config,
deps: {},
async factory({}, options?: ConfigFactoryOptions) {
const { argv = process.argv, remote } = options ?? {};
const { config } = await loadBackendConfig({ argv, remote });
return config;
},
});
export const configFactory = createServiceFactory(
(options?: ConfigFactoryOptions) => ({
service: coreServices.config,
deps: {},
async factory({}) {
const { argv = process.argv, remote } = options ?? {};
const { config } = await loadBackendConfig({ argv, remote });
return config;
},
}),
);
@@ -69,45 +69,42 @@ function defaultConfigure({
}
/** @public */
export const rootHttpRouterFactory = createServiceFactory({
service: coreServices.rootHttpRouter,
deps: {
config: coreServices.config,
rootLogger: coreServices.rootLogger,
lifecycle: coreServices.rootLifecycle,
},
async factory(
{ config, rootLogger, lifecycle },
{
indexPath,
configure = defaultConfigure,
}: RootHttpRouterFactoryOptions = {},
) {
const logger = rootLogger.child({ service: 'rootHttpRouter' });
const app = express();
export const rootHttpRouterFactory = createServiceFactory(
(options?: RootHttpRouterFactoryOptions) => ({
service: coreServices.rootHttpRouter,
deps: {
config: coreServices.config,
rootLogger: coreServices.rootLogger,
lifecycle: coreServices.rootLifecycle,
},
async factory({ config, rootLogger, lifecycle }) {
const { indexPath, configure = defaultConfigure } = options ?? {};
const logger = rootLogger.child({ service: 'rootHttpRouter' });
const app = express();
const router = DefaultRootHttpRouter.create({ indexPath });
const middleware = MiddlewareFactory.create({ config, logger });
const router = DefaultRootHttpRouter.create({ indexPath });
const middleware = MiddlewareFactory.create({ config, logger });
configure({
app,
routes: router.handler(),
middleware,
config,
logger,
lifecycle,
});
configure({
app,
routes: router.handler(),
middleware,
config,
logger,
lifecycle,
});
const server = await createHttpServer(
app,
readHttpServerOptions(config.getOptionalConfig('backend')),
{ logger },
);
const server = await createHttpServer(
app,
readHttpServerOptions(config.getOptionalConfig('backend')),
{ logger },
);
lifecycle.addShutdownHook(() => server.stop());
lifecycle.addShutdownHook(() => server.stop());
await server.start();
await server.start();
return router;
},
});
return router;
},
}),
);