From 92b495328bd3a3627c2b91b107c3175b10d44ab2 Mon Sep 17 00:00:00 2001 From: Oleg S <97077423+RobotSail@users.noreply.github.com> Date: Mon, 6 Mar 2023 17:00:01 -0500 Subject: [PATCH] expose the techdocs-backend plugin using the new system Signed-off-by: Oleg S <97077423+RobotSail@users.noreply.github.com> --- .changeset/large-feet-wash.md | 6 ++ packages/backend-next/package.json | 1 + packages/backend-next/src/index.ts | 2 + plugins/techdocs-backend/api-report.md | 4 ++ plugins/techdocs-backend/package.json | 1 + plugins/techdocs-backend/src/index.ts | 1 + plugins/techdocs-backend/src/plugin.ts | 95 ++++++++++++++++++++++++++ yarn.lock | 2 + 8 files changed, 112 insertions(+) create mode 100644 .changeset/large-feet-wash.md create mode 100644 plugins/techdocs-backend/src/plugin.ts diff --git a/.changeset/large-feet-wash.md b/.changeset/large-feet-wash.md new file mode 100644 index 0000000000..15e3c54b42 --- /dev/null +++ b/.changeset/large-feet-wash.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-techdocs-backend': minor +'example-backend-next': minor +--- + +Expose the techdocs-backend plugin using the new plugin system diff --git a/packages/backend-next/package.json b/packages/backend-next/package.json index 9f7ec8306a..5d8a8cb1ec 100644 --- a/packages/backend-next/package.json +++ b/packages/backend-next/package.json @@ -29,6 +29,7 @@ "@backstage/plugin-app-backend": "workspace:^", "@backstage/plugin-catalog-backend": "workspace:^", "@backstage/plugin-scaffolder-backend": "workspace:^", + "@backstage/plugin-techdocs-backend": "workspace:^", "@backstage/plugin-todo-backend": "workspace:^" }, "devDependencies": { diff --git a/packages/backend-next/src/index.ts b/packages/backend-next/src/index.ts index 8e29244ebf..0f7af110e9 100644 --- a/packages/backend-next/src/index.ts +++ b/packages/backend-next/src/index.ts @@ -19,6 +19,7 @@ import { catalogModuleTemplateKind } from '@backstage/plugin-scaffolder-backend/ import { createBackend } from '@backstage/backend-defaults'; import { appPlugin } from '@backstage/plugin-app-backend/alpha'; import { todoPlugin } from '@backstage/plugin-todo-backend'; +import { techdocsPlugin } from '@backstage/plugin-techdocs-backend'; const backend = createBackend(); @@ -26,4 +27,5 @@ backend.add(catalogPlugin()); backend.add(catalogModuleTemplateKind()); backend.add(appPlugin({ appPackageName: 'example-app' })); backend.add(todoPlugin()); +backend.add(techdocsPlugin()); backend.start(); diff --git a/plugins/techdocs-backend/api-report.md b/plugins/techdocs-backend/api-report.md index 1f6aa1d66a..fe8db9a135 100644 --- a/plugins/techdocs-backend/api-report.md +++ b/plugins/techdocs-backend/api-report.md @@ -5,6 +5,7 @@ ```ts /// +import { BackendFeature } from '@backstage/backend-plugin-api'; import { CatalogApi } from '@backstage/catalog-client'; import { CatalogClient } from '@backstage/catalog-client'; import { Config } from '@backstage/config'; @@ -129,5 +130,8 @@ export type TechDocsCollatorOptions = { export { TechDocsDocument }; +// @public +export const techdocsPlugin: () => BackendFeature; + export * from '@backstage/plugin-techdocs-node'; ``` diff --git a/plugins/techdocs-backend/package.json b/plugins/techdocs-backend/package.json index c9fff25b70..6389487eec 100644 --- a/plugins/techdocs-backend/package.json +++ b/plugins/techdocs-backend/package.json @@ -34,6 +34,7 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/catalog-client": "workspace:^", "@backstage/catalog-model": "workspace:^", "@backstage/config": "workspace:^", diff --git a/plugins/techdocs-backend/src/index.ts b/plugins/techdocs-backend/src/index.ts index dc5d87d1b3..06259d3d3c 100644 --- a/plugins/techdocs-backend/src/index.ts +++ b/plugins/techdocs-backend/src/index.ts @@ -37,6 +37,7 @@ export type { TechDocsCollatorFactoryOptions, TechDocsCollatorOptions, } from './search'; +export { techdocsPlugin } from './plugin'; /** * @deprecated Use directly from @backstage/plugin-techdocs-node diff --git a/plugins/techdocs-backend/src/plugin.ts b/plugins/techdocs-backend/src/plugin.ts new file mode 100644 index 0000000000..7cffcc8fbd --- /dev/null +++ b/plugins/techdocs-backend/src/plugin.ts @@ -0,0 +1,95 @@ +/* + * 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 { + DockerContainerRunner, + loggerToWinstonLogger, + cacheToPluginCacheManager, +} from '@backstage/backend-common'; +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; + +import { + Preparers, + Generators, + Publisher, +} from '@backstage/plugin-techdocs-node'; +import Docker from 'dockerode'; +import { createRouter } from './service'; + +/** + * The TechDocs plugin is responsible for serving and building documentation for any entity. + * @public + */ +export const techdocsPlugin = createBackendPlugin({ + pluginId: 'techdocs-backend', + register(env) { + env.registerInit({ + deps: { + config: coreServices.config, + logger: coreServices.logger, + urlReader: coreServices.urlReader, + http: coreServices.httpRouter, + discovery: coreServices.discovery, + cache: coreServices.cache, + }, + async init({ config, logger, urlReader, http, discovery, cache }) { + const winstonLogger = loggerToWinstonLogger(logger); + // Preparers are responsible for fetching source files for documentation. + const preparers = await Preparers.fromConfig(config, { + reader: urlReader, + logger: winstonLogger, + }); + + // Docker client (conditionally) used by the generators, based on techdocs.generators config. + const dockerClient = new Docker(); + const containerRunner = new DockerContainerRunner({ dockerClient }); + + // Generators are used for generating documentation sites. + const generators = await Generators.fromConfig(config, { + logger: winstonLogger, + containerRunner, + }); + + // Publisher is used for + // 1. Publishing generated files to storage + // 2. Fetching files from storage and passing them to TechDocs frontend. + const publisher = await Publisher.fromConfig(config, { + logger: winstonLogger, + discovery: discovery, + }); + + // checks if the publisher is working and logs the result + await publisher.getReadiness(); + + const cacheManager = cacheToPluginCacheManager(cache); + http.use( + await createRouter({ + logger: winstonLogger, + cache: cacheManager, + preparers, + generators, + publisher, + config, + discovery, + }), + ); + }, + }); + }, +}); diff --git a/yarn.lock b/yarn.lock index 663107e961..6a8317bfce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8538,6 +8538,7 @@ __metadata: resolution: "@backstage/plugin-techdocs-backend@workspace:plugins/techdocs-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/catalog-client": "workspace:^" "@backstage/catalog-model": "workspace:^" @@ -22870,6 +22871,7 @@ __metadata: "@backstage/plugin-app-backend": "workspace:^" "@backstage/plugin-catalog-backend": "workspace:^" "@backstage/plugin-scaffolder-backend": "workspace:^" + "@backstage/plugin-techdocs-backend": "workspace:^" "@backstage/plugin-todo-backend": "workspace:^" languageName: unknown linkType: soft