Merge pull request #13852 from backstage/rugvip/app-backend-port

app-backend: add implementation for new backend system
This commit is contained in:
Patrik Oldsberg
2022-09-27 11:29:21 +02:00
committed by GitHub
20 changed files with 299 additions and 11 deletions
+9 -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>;
@@ -69,6 +74,9 @@ export const permissionsFactory: (
options?: undefined,
) => ServiceFactory<PermissionAuthorizer | PermissionEvaluator>;
// @public (undocumented)
export const rootLoggerFactory: (options?: undefined) => ServiceFactory<Logger>;
// @public (undocumented)
export const schedulerFactory: (
options?: undefined,
@@ -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,21 +41,28 @@ export const httpRouterFactory = createServiceFactory({
config: configServiceRef,
plugin: pluginMetadataServiceRef,
},
async factory({ config }) {
async factory({ config }, options?: HttpRouterFactoryOptions) {
const defaultPluginId = options?.indexPlugin ?? 'app';
const apiRouter = Router();
const rootRouter = Router();
const service = createServiceBuilder(module)
.loadConfig(config)
.addRouter('/api', apiRouter)
.addRouter('', rootRouter);
await service.start();
return async ({ plugin }) => {
const pluginId = plugin.getId();
const path = pluginId ? `/api/${pluginId}` : '';
return {
use(handler: Handler) {
rootRouter.use(path, handler);
if (pluginId === defaultPluginId) {
rootRouter.use(handler);
} else {
apiRouter.use(`/${pluginId}`, handler);
}
},
};
};
@@ -19,8 +19,10 @@ export { configFactory } from './configService';
export { databaseFactory } from './databaseService';
export { discoveryFactory } from './discoveryService';
export { loggerFactory } from './loggerService';
export { rootLoggerFactory } from './rootLoggerService';
export { permissionsFactory } from './permissionsService';
export { schedulerFactory } from './schedulerService';
export { tokenManagerFactory } from './tokenManagerService';
export { urlReaderFactory } from './urlReaderService';
export { httpRouterFactory } from './httpRouterService';
export type { HttpRouterFactoryOptions } from './httpRouterService';
@@ -39,7 +39,7 @@ class BackstageLogger implements Logger {
}
/** @public */
export const loggerFactory = createServiceFactory({
export const rootLoggerFactory = createServiceFactory({
service: rootLoggerServiceRef,
deps: {},
async factory() {
@@ -24,6 +24,7 @@ import {
httpRouterFactory,
loggerFactory,
permissionsFactory,
rootLoggerFactory,
schedulerFactory,
tokenManagerFactory,
urlReaderFactory,
@@ -36,6 +37,7 @@ export const defaultServiceFactories = [
databaseFactory,
discoveryFactory,
loggerFactory,
rootLoggerFactory,
permissionsFactory,
schedulerFactory,
tokenManagerFactory,
+1
View File
@@ -26,6 +26,7 @@
},
"dependencies": {
"@backstage/backend-defaults": "workspace:^",
"@backstage/plugin-app-backend": "workspace:^",
"@backstage/plugin-catalog-backend": "workspace:^",
"@backstage/plugin-scaffolder-backend": "workspace:^"
},
+2
View File
@@ -17,9 +17,11 @@
import { catalogPlugin } from '@backstage/plugin-catalog-backend';
import { scaffolderCatalogModule } from '@backstage/plugin-scaffolder-backend';
import { createBackend } from '@backstage/backend-defaults';
import { appPlugin } from '@backstage/plugin-app-backend';
const backend = createBackend();
backend.add(catalogPlugin());
backend.add(scaffolderCatalogModule());
backend.add(appPlugin({ appPackageName: 'example-app' }));
backend.start();
@@ -31,7 +31,7 @@ describe('createExtensionPoint', () => {
});
describe('createBackendPlugin', () => {
it('should create an BackendPlugin', () => {
it('should create a BackendPlugin', () => {
const plugin = createBackendPlugin({
id: 'x',
register(_reg, _options: { a: string }) {},
@@ -103,7 +103,7 @@ describe('createBackendPlugin', () => {
});
describe('createBackendModule', () => {
it('should create an BackendModule', () => {
it('should create a BackendModule', () => {
const mod = createBackendModule({
pluginId: 'x',
moduleId: 'y',
@@ -63,10 +63,18 @@ export async function startTestBackend<
if (Array.isArray(serviceDef)) {
// if type is ExtensionPoint?
// do something differently?
const [ref, impl] = serviceDef;
if (ref.scope === 'plugin') {
return createServiceFactory({
service: ref,
deps: {},
factory: async () => async () => impl,
});
}
return createServiceFactory({
service: serviceDef[0],
service: ref,
deps: {},
factory: async () => async () => serviceDef[1],
factory: async () => impl,
});
}
return serviceDef as ServiceFactory;