Merge pull request #16710 from backstage/search/backend-system-migration

[Search] Migrate search to new backend system
This commit is contained in:
Camila Belo
2023-03-22 09:21:15 +01:00
committed by GitHub
68 changed files with 1898 additions and 116 deletions
@@ -0,0 +1,12 @@
## API Report File for "@backstage/plugin-search-backend"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { BackendFeature } from '@backstage/backend-plugin-api';
// @alpha
export const searchPlugin: () => BackendFeature;
// (No @packageDocumentation comment for this package)
```
+18 -3
View File
@@ -6,9 +6,22 @@
"types": "src/index.ts",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
"access": "public"
},
"exports": {
".": "./src/index.ts",
"./alpha": "./src/alpha.ts",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"alpha": [
"src/alpha.ts"
],
"package.json": [
"package.json"
]
}
},
"backstage": {
"role": "backend-plugin"
@@ -24,6 +37,7 @@
},
"dependencies": {
"@backstage/backend-common": "workspace:^",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/errors": "workspace:^",
"@backstage/plugin-auth-node": "workspace:^",
@@ -43,6 +57,7 @@
"zod": "~3.18.0"
},
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"@types/supertest": "^2.0.8",
"supertest": "^6.1.3"
+31
View File
@@ -0,0 +1,31 @@
/*
* 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 { startTestBackend } from '@backstage/backend-test-utils';
import request from 'supertest';
import { searchPlugin } from './alpha';
describe('searchPlugin', () => {
it('should serve search results on query endpoint', async () => {
const { server } = await startTestBackend({
features: [searchPlugin()],
});
const response = await request(server).get('/api/search/query');
expect(response.status).toBe(200);
expect(response.body).toEqual({ numberOfResults: 0, results: [] });
});
});
+130
View File
@@ -0,0 +1,130 @@
/*
* 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 {
RegisterCollatorParameters,
RegisterDecoratorParameters,
LunrSearchEngine,
} from '@backstage/plugin-search-backend-node';
import {
searchIndexServiceRef,
searchIndexRegistryExtensionPoint,
SearchIndexRegistryExtensionPoint,
SearchEngineRegistryExtensionPoint,
searchEngineRegistryExtensionPoint,
} from '@backstage/plugin-search-backend-node/alpha';
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;
}
}
/**
* The Search plugin is responsible for starting search indexing processes and return search results.
* @alpha
*/
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,
searchIndexService: searchIndexServiceRef,
},
async init({ config, logger, permissions, http, searchIndexService }) {
let searchEngine = searchEngineRegistry.getSearchEngine();
if (!searchEngine) {
searchEngine = new LunrSearchEngine({
logger: loggerToWinstonLogger(logger),
});
}
const collators = searchIndexRegistry.getCollators();
const decorators = searchIndexRegistry.getDecorators();
await searchIndexService.start({
searchEngine,
collators,
decorators,
});
const router = await createRouter({
config,
permissions,
logger: loggerToWinstonLogger(logger),
engine: searchEngine,
types: searchIndexService.getDocumentTypes(),
});
http.use(router);
},
});
},
});