From 1a79af33f2934442aacb0fd4f69c241f70152c71 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Mon, 6 Mar 2023 10:35:24 +0100 Subject: [PATCH] clean up comments and dependencies Signed-off-by: Emma Indal Co-authored-by: Camila Loiola --- packages/backend-next/package.json | 5 ++ packages/backend-next/src/index.ts | 60 +------------- packages/backend-next/src/plugins/search.ts | 82 +++++++++++++++++++ .../package.json | 2 + .../src/engines/ElasticSearchSearchEngine.ts | 2 +- plugins/search-backend/src/plugin.ts | 2 +- yarn.lock | 6 ++ 7 files changed, 98 insertions(+), 61 deletions(-) create mode 100644 packages/backend-next/src/plugins/search.ts diff --git a/packages/backend-next/package.json b/packages/backend-next/package.json index 14ec71f352..8df5ace282 100644 --- a/packages/backend-next/package.json +++ b/packages/backend-next/package.json @@ -25,10 +25,15 @@ "clean": "backstage-cli package clean" }, "dependencies": { + "@backstage/backend-common": "workspace:^", "@backstage/backend-defaults": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/plugin-app-backend": "workspace:^", "@backstage/plugin-catalog-backend": "workspace:^", + "@backstage/plugin-explore-backend": "workspace:^", "@backstage/plugin-scaffolder-backend": "workspace:^", + "@backstage/plugin-search-backend": "workspace:^", + "@backstage/plugin-search-backend-node": "workspace:^", "@backstage/plugin-techdocs-backend": "workspace:^", "@backstage/plugin-todo-backend": "workspace:^" }, diff --git a/packages/backend-next/src/index.ts b/packages/backend-next/src/index.ts index 93161de9a2..cf275ae3ad 100644 --- a/packages/backend-next/src/index.ts +++ b/packages/backend-next/src/index.ts @@ -21,56 +21,7 @@ import { appPlugin } from '@backstage/plugin-app-backend/alpha'; import { todoPlugin } from '@backstage/plugin-todo-backend'; import { techdocsPlugin } from '@backstage/plugin-techdocs-backend/alpha'; import { searchPlugin } from '@backstage/plugin-search-backend'; -import { elasticSearchEngineModule } from '@backstage/plugin-search-backend-module-elasticsearch'; -import { - coreServices, - createBackendModule, -} from '@backstage/backend-plugin-api'; -import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node'; -import { DefaultCatalogCollatorFactory } from '@backstage/plugin-catalog-backend'; -// import { pgSearchEngineModule } from '@backstage/plugin-search-backend-module-pg'; -// import { lunrSearchEngineModule } from '@backstage/plugin-search-backend-node/'; - -// TODO: move to search-backend-node? -export const searchIndexRegistry = createBackendModule({ - moduleId: 'searchIndexRegistry', - pluginId: 'search', - register(env) { - env.registerInit({ - deps: { - indexRegistry: searchIndexRegistryExtensionPoint, - config: coreServices.config, - discovery: coreServices.discovery, - tokenManager: coreServices.tokenManager, - logger: coreServices.logger, - scheduler: coreServices.scheduler, - }, - async init({ - indexRegistry, - config, - discovery, - tokenManager, - scheduler, - }) { - const schedule = scheduler.createScheduledTaskRunner({ - frequency: { minutes: 10 }, - timeout: { minutes: 15 }, - // A 3 second delay gives the backend server a chance to initialize before - // any collators are executed, which may attempt requests against the API. - initialDelay: { seconds: 3 }, - }); - - indexRegistry.addCollator({ - schedule, - factory: DefaultCatalogCollatorFactory.fromConfig(config, { - discovery: discovery, - tokenManager: tokenManager, - }), - }); - }, - }); - }, -}); +import { searchIndexRegistry } from './plugins/search'; const backend = createBackend(); @@ -81,15 +32,6 @@ backend.add(todoPlugin()); backend.add(techdocsPlugin()); backend.add(searchPlugin()); -// just as example of search engine module, remove before shipping and use default -backend.add( - elasticSearchEngineModule({ - indexTemplate: { - name: 'my-custom-template', - body: { index_patterns: ['*index*'], template: {} }, - }, - }), -); backend.add(searchIndexRegistry()); backend.start(); diff --git a/packages/backend-next/src/plugins/search.ts b/packages/backend-next/src/plugins/search.ts new file mode 100644 index 0000000000..559e6a9cd1 --- /dev/null +++ b/packages/backend-next/src/plugins/search.ts @@ -0,0 +1,82 @@ +/* + * 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, + createBackendModule, +} from '@backstage/backend-plugin-api'; +import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node'; +import { DefaultCatalogCollatorFactory } from '@backstage/plugin-catalog-backend'; +import { DefaultTechDocsCollatorFactory } from '@backstage/plugin-techdocs-backend'; +import { ToolDocumentCollatorFactory } from '@backstage/plugin-explore-backend'; +import { loggerToWinstonLogger } from '@backstage/backend-common'; + +export const searchIndexRegistry = createBackendModule({ + moduleId: 'searchIndexRegistry', + pluginId: 'search', + register(env) { + env.registerInit({ + deps: { + indexRegistry: searchIndexRegistryExtensionPoint, + config: coreServices.config, + discovery: coreServices.discovery, + tokenManager: coreServices.tokenManager, + logger: coreServices.logger, + scheduler: coreServices.scheduler, + }, + async init({ + indexRegistry, + config, + logger, + discovery, + tokenManager, + scheduler, + }) { + const schedule = scheduler.createScheduledTaskRunner({ + frequency: { minutes: 10 }, + timeout: { minutes: 15 }, + // A 3 second delay gives the backend server a chance to initialize before + // any collators are executed, which may attempt requests against the API. + initialDelay: { seconds: 3 }, + }); + + indexRegistry.addCollator({ + schedule, + factory: DefaultCatalogCollatorFactory.fromConfig(config, { + discovery, + tokenManager, + }), + }); + + indexRegistry.addCollator({ + schedule, + factory: DefaultTechDocsCollatorFactory.fromConfig(config, { + discovery, + logger: loggerToWinstonLogger(logger), + tokenManager, + }), + }); + + indexRegistry.addCollator({ + schedule, + factory: ToolDocumentCollatorFactory.fromConfig(config, { + discovery, + logger: loggerToWinstonLogger(logger), + }), + }); + }, + }); + }, +}); diff --git a/plugins/search-backend-module-elasticsearch/package.json b/plugins/search-backend-module-elasticsearch/package.json index e854687360..aff861b784 100644 --- a/plugins/search-backend-module-elasticsearch/package.json +++ b/plugins/search-backend-module-elasticsearch/package.json @@ -23,6 +23,8 @@ "clean": "backstage-cli package clean" }, "dependencies": { + "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", "@backstage/plugin-search-backend-node": "workspace:^", "@backstage/plugin-search-common": "workspace:^", diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts index dbd8db7a0d..f368799673 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts @@ -557,7 +557,7 @@ export const elasticSearchEngineModule = createBackendModule( searchEngine.setTranslator(options.translator); } - // set custom index template if available TODO(emmaindal): make it possible to pass in mutliple index templates + // set custom index template if available if (options?.indexTemplate) { searchEngine.setIndexTemplate(options.indexTemplate); } diff --git a/plugins/search-backend/src/plugin.ts b/plugins/search-backend/src/plugin.ts index 5e17f03740..35a9835ef6 100644 --- a/plugins/search-backend/src/plugin.ts +++ b/plugins/search-backend/src/plugin.ts @@ -117,7 +117,7 @@ export const searchPlugin = createBackendPlugin({ engine: searchEngine, types: searchIndexBuilder.getDocumentTypes(), }); - // We register the router with the http service. + http.use(router); }, }); diff --git a/yarn.lock b/yarn.lock index 6615236acf..3c787dceb3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7773,6 +7773,7 @@ __metadata: resolution: "@backstage/plugin-search-backend-module-elasticsearch@workspace:plugins/search-backend-module-elasticsearch" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" "@backstage/plugin-search-backend-node": "workspace:^" @@ -22481,11 +22482,16 @@ __metadata: version: 0.0.0-use.local resolution: "example-backend-next@workspace:packages/backend-next" dependencies: + "@backstage/backend-common": "workspace:^" "@backstage/backend-defaults": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/plugin-app-backend": "workspace:^" "@backstage/plugin-catalog-backend": "workspace:^" + "@backstage/plugin-explore-backend": "workspace:^" "@backstage/plugin-scaffolder-backend": "workspace:^" + "@backstage/plugin-search-backend": "workspace:^" + "@backstage/plugin-search-backend-node": "workspace:^" "@backstage/plugin-techdocs-backend": "workspace:^" "@backstage/plugin-todo-backend": "workspace:^" languageName: unknown