From 49fe8c20005c8a09780a044f917bf388b096ff33 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Tue, 28 Feb 2023 14:34:34 +0100 Subject: [PATCH] define interfaces for extension points and services Signed-off-by: Emma Indal Co-authored-by: Camila Loiola --- .../search-backend-node/src/IndexBuilder.ts | 7 ++- plugins/search-backend-node/src/index.ts | 2 +- plugins/search-backend-node/src/types.ts | 26 ++++++++ plugins/search-backend/src/plugin.ts | 59 +++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 plugins/search-backend/src/plugin.ts diff --git a/plugins/search-backend-node/src/IndexBuilder.ts b/plugins/search-backend-node/src/IndexBuilder.ts index 907519f289..9397c098aa 100644 --- a/plugins/search-backend-node/src/IndexBuilder.ts +++ b/plugins/search-backend-node/src/IndexBuilder.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { createServiceRef } from '@backstage/backend-plugin-api'; import { DocumentDecoratorFactory, DocumentTypeInfo, @@ -23,16 +24,20 @@ import { Transform, pipeline } from 'stream'; import { Logger } from 'winston'; import { Scheduler } from './Scheduler'; import { + IndexBuilderService, IndexBuilderOptions, RegisterCollatorParameters, RegisterDecoratorParameters, } from './types'; +export const searchIndexBuilderRef = createServiceRef({ + id: 'search.index-node', +}); /** * Used for adding collators, decorators and compile them into tasks which are added to a scheduler returned to the caller. * @public */ -export class IndexBuilder { +export class IndexBuilder implements IndexBuilderService { private collators: Record; private decorators: Record; private documentTypes: Record; diff --git a/plugins/search-backend-node/src/index.ts b/plugins/search-backend-node/src/index.ts index a188509c1c..1bcd31de93 100644 --- a/plugins/search-backend-node/src/index.ts +++ b/plugins/search-backend-node/src/index.ts @@ -20,7 +20,7 @@ * @packageDocumentation */ -export { IndexBuilder } from './IndexBuilder'; +export { IndexBuilder, searchIndexBuilderRef } from './IndexBuilder'; export { Scheduler } from './Scheduler'; export * from './collators'; export { LunrSearchEngine } from './engines'; diff --git a/plugins/search-backend-node/src/types.ts b/plugins/search-backend-node/src/types.ts index dfcf4d12ce..94b21cceec 100644 --- a/plugins/search-backend-node/src/types.ts +++ b/plugins/search-backend-node/src/types.ts @@ -18,9 +18,11 @@ import { TaskRunner } from '@backstage/backend-tasks'; import { DocumentCollatorFactory, DocumentDecoratorFactory, + DocumentTypeInfo, SearchEngine, } from '@backstage/plugin-search-common'; import { Logger } from 'winston'; +import { Scheduler } from './Scheduler'; /** * Options required to instantiate the index builder. @@ -57,3 +59,27 @@ export interface RegisterDecoratorParameters { */ factory: DocumentDecoratorFactory; } + +export type IndexBuilderServiceBuildOptions = { + searchEngine: SearchEngine; + collators: RegisterCollatorParameters[]; + decorators: RegisterDecoratorParameters[]; +}; +export interface IndexBuilderService { + build(options: IndexBuilderServiceBuildOptions): Promise<{ + scheduler: Scheduler; + }>; + getDocumentTypes(): Record; +} + +export interface SearchIndexRegistryExtensionPoint { + addCollator(options: RegisterCollatorParameters): void; + addDecorator(options: RegisterDecoratorParameters): void; + getCollators(): RegisterCollatorParameters[]; + getDecorators(): RegisterDecoratorParameters[]; +} + +export interface SearchEngineRegistryExtensionPoint { + setSearchEngine(searchEngine: SearchEngine): void; + getSearchEngine(): SearchEngine; +} diff --git a/plugins/search-backend/src/plugin.ts b/plugins/search-backend/src/plugin.ts new file mode 100644 index 0000000000..5d7c7935a8 --- /dev/null +++ b/plugins/search-backend/src/plugin.ts @@ -0,0 +1,59 @@ +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; +import { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + searchIndexBuilderRef, + searchEngineExtensionPoint, + searchIndexExtensionPoint, +} from '@backstage/plugin-search-backend-node'; + +import { createRouter } from './service/router'; + +export const searchPlugin = createBackendPlugin({ + pluginId: 'search', + register(env) { + env.registerInit({ + deps: { + logger: coreServices.logger, + config: coreServices.config, + permissions: coreServices.permissions, + http: coreServices.httpRouter, + searchIndexBuilder: searchIndexBuilderRef, + searchEngineRegistry: searchEngineExtensionPoint, + searchIndexRegistry: searchIndexExtensionPoint, + }, + async init({ + config, + logger, + permissions, + http, + searchEngineRegistry, + searchIndexRegistry, + searchIndexBuilder, + }) { + const searchEngine = searchEngineRegistry.getSearchEngine(); + const collators = searchIndexRegistry.getCollators(); + const decorators = searchIndexRegistry.getDecorators(); + + const { scheduler } = searchIndexBuilder.build({ + searchEngine, + collators, + decorators, + }); + scheduler.start(); + + const router = await createRouter({ + config, + permissions, + logger: loggerToWinstonLogger(logger), + engine: searchEngine, + types: searchIndexBuilder.getDocumentTypes(), + }); + // We register the router with the http service. + http.use(router); + }, + }); + }, +});