define interfaces for extension points and services
Signed-off-by: Emma Indal <emma.indahl@gmail.com> Co-authored-by: Camila Loiola <camilaibs@gmail.com>
This commit is contained in:
@@ -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<IndexBuilderService>({
|
||||
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<string, RegisterCollatorParameters>;
|
||||
private decorators: Record<string, DocumentDecoratorFactory[]>;
|
||||
private documentTypes: Record<string, DocumentTypeInfo>;
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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<string, DocumentTypeInfo>;
|
||||
}
|
||||
|
||||
export interface SearchIndexRegistryExtensionPoint {
|
||||
addCollator(options: RegisterCollatorParameters): void;
|
||||
addDecorator(options: RegisterDecoratorParameters): void;
|
||||
getCollators(): RegisterCollatorParameters[];
|
||||
getDecorators(): RegisterDecoratorParameters[];
|
||||
}
|
||||
|
||||
export interface SearchEngineRegistryExtensionPoint {
|
||||
setSearchEngine(searchEngine: SearchEngine): void;
|
||||
getSearchEngine(): SearchEngine;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user