backend-app-api: added index plugin option for http router service

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-09-26 16:40:11 +02:00
parent 571ff04b0c
commit 0027a749cd
4 changed files with 25 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-app-api': patch
---
Added possibility to configure index plugin of the HTTP router service.
+6 -1
View File
@@ -58,9 +58,14 @@ export const discoveryFactory: (
// @public (undocumented)
export const httpRouterFactory: (
options?: undefined,
options?: HttpRouterFactoryOptions | undefined,
) => ServiceFactory<HttpRouterService>;
// @public (undocumented)
export type HttpRouterFactoryOptions = {
indexPlugin?: string;
};
// @public (undocumented)
export const loggerFactory: (options?: undefined) => ServiceFactory<Logger>;
@@ -24,6 +24,16 @@ import Router from 'express-promise-router';
import { Handler } from 'express';
import { createServiceBuilder } from '@backstage/backend-common';
/**
* @public
*/
export type HttpRouterFactoryOptions = {
/**
* The plugin ID used for the index route. Defaults to 'app'
*/
indexPlugin?: string;
};
/** @public */
export const httpRouterFactory = createServiceFactory({
service: httpRouterServiceRef,
@@ -31,8 +41,9 @@ export const httpRouterFactory = createServiceFactory({
config: configServiceRef,
plugin: pluginMetadataServiceRef,
},
async factory({ config }) {
async factory({ config }, options?: HttpRouterFactoryOptions) {
const rootRouter = Router();
const defaultPluginId = options?.indexPlugin ?? 'app';
const service = createServiceBuilder(module)
.loadConfig(config)
@@ -42,7 +53,7 @@ export const httpRouterFactory = createServiceFactory({
return async ({ plugin }) => {
const pluginId = plugin.getId();
const path = pluginId ? `/api/${pluginId}` : '';
const path = pluginId === defaultPluginId ? '' : `/api/${pluginId}`;
return {
use(handler: Handler) {
rootRouter.use(path, handler);
@@ -25,3 +25,4 @@ export { schedulerFactory } from './schedulerService';
export { tokenManagerFactory } from './tokenManagerService';
export { urlReaderFactory } from './urlReaderService';
export { httpRouterFactory } from './httpRouterService';
export type { HttpRouterFactoryOptions } from './httpRouterService';