From 4f759aeef4ceb42c504ff0dabd1d1054654190d8 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Wed, 1 Mar 2023 21:15:20 +0100 Subject: [PATCH] feat(search): create search plugin implementation Signed-off-by: Camila Belo --- plugins/search-backend/package.json | 1 + plugins/search-backend/src/index.ts | 3 + plugins/search-backend/src/plugin.ts | 100 ++++++++++++++++++++++----- 3 files changed, 87 insertions(+), 17 deletions(-) diff --git a/plugins/search-backend/package.json b/plugins/search-backend/package.json index 02d4d53a92..bbd5d78f79 100644 --- a/plugins/search-backend/package.json +++ b/plugins/search-backend/package.json @@ -24,6 +24,7 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/plugin-auth-node": "workspace:^", diff --git a/plugins/search-backend/src/index.ts b/plugins/search-backend/src/index.ts index 851efd1875..0775a64378 100644 --- a/plugins/search-backend/src/index.ts +++ b/plugins/search-backend/src/index.ts @@ -21,3 +21,6 @@ */ export * from './service/router'; + +// TODO: export as alfa subpath +export { searchPlugin } from './plugin'; diff --git a/plugins/search-backend/src/plugin.ts b/plugins/search-backend/src/plugin.ts index 5d7c7935a8..5e17f03740 100644 --- a/plugins/search-backend/src/plugin.ts +++ b/plugins/search-backend/src/plugin.ts @@ -1,43 +1,109 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import { coreServices, createBackendPlugin, } from '@backstage/backend-plugin-api'; import { loggerToWinstonLogger } from '@backstage/backend-common'; import { - searchIndexBuilderRef, - searchEngineExtensionPoint, - searchIndexExtensionPoint, + searchIndexBuilderService, + searchIndexRegistryExtensionPoint, + SearchIndexRegistryExtensionPoint, + searchEngineRegistryExtensionPoint, + RegisterCollatorParameters, + RegisterDecoratorParameters, + SearchEngineRegistryExtensionPoint, + LunrSearchEngine, } from '@backstage/plugin-search-backend-node'; import { createRouter } from './service/router'; +import { SearchEngine } from '@backstage/plugin-search-common'; + +class SearchIndexRegistry implements SearchIndexRegistryExtensionPoint { + private collators: RegisterCollatorParameters[] = []; + private decorators: RegisterDecoratorParameters[] = []; + + public addCollator(options: RegisterCollatorParameters): void { + this.collators.push(options); + } + + public addDecorator(options: RegisterDecoratorParameters): void { + this.decorators.push(options); + } + + public getCollators(): RegisterCollatorParameters[] { + return this.collators; + } + + public getDecorators(): RegisterDecoratorParameters[] { + return this.decorators; + } +} + +class SearchEngineRegistry implements SearchEngineRegistryExtensionPoint { + private searchEngine: SearchEngine | null = null; + + public setSearchEngine(searchEngine: SearchEngine): void { + if (this.searchEngine) { + throw new Error('Multiple Search engines is not supported at this time'); + } + this.searchEngine = searchEngine; + } + + public getSearchEngine(): SearchEngine | null { + return this.searchEngine; + } +} export const searchPlugin = createBackendPlugin({ pluginId: 'search', register(env) { + const searchIndexRegistry = new SearchIndexRegistry(); + env.registerExtensionPoint( + searchIndexRegistryExtensionPoint, + searchIndexRegistry, + ); + + const searchEngineRegistry = new SearchEngineRegistry(); + env.registerExtensionPoint( + searchEngineRegistryExtensionPoint, + searchEngineRegistry, + ); + env.registerInit({ deps: { logger: coreServices.logger, config: coreServices.config, permissions: coreServices.permissions, http: coreServices.httpRouter, - searchIndexBuilder: searchIndexBuilderRef, - searchEngineRegistry: searchEngineExtensionPoint, - searchIndexRegistry: searchIndexExtensionPoint, + searchIndexBuilder: searchIndexBuilderService, }, - async init({ - config, - logger, - permissions, - http, - searchEngineRegistry, - searchIndexRegistry, - searchIndexBuilder, - }) { - const searchEngine = searchEngineRegistry.getSearchEngine(); + async init({ config, logger, permissions, http, searchIndexBuilder }) { + let searchEngine = searchEngineRegistry.getSearchEngine(); + if (!searchEngine) { + searchEngine = new LunrSearchEngine({ + logger: loggerToWinstonLogger(logger), + }); + } + const collators = searchIndexRegistry.getCollators(); const decorators = searchIndexRegistry.getDecorators(); - const { scheduler } = searchIndexBuilder.build({ + const { scheduler } = await searchIndexBuilder.build({ searchEngine, collators, decorators,