From 2179d00d296b1502a36362b1b70c2be2f4e634c6 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Thu, 9 Nov 2023 09:30:29 -0600 Subject: [PATCH] feat: migrated to new backend system again Signed-off-by: Andre Wanlin --- packages/backend/package.json | 1 + packages/backend/src/index.test.ts | 8 -- packages/backend/src/index.ts | 151 ++++-------------------- packages/backend/src/plugins/graphql.ts | 6 +- packages/backend/src/types.ts | 2 - yarn.lock | 12 ++ 6 files changed, 42 insertions(+), 138 deletions(-) delete mode 100644 packages/backend/src/index.test.ts diff --git a/packages/backend/package.json b/packages/backend/package.json index 044759f24..72e5c1ed3 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -18,6 +18,7 @@ }, "dependencies": { "@backstage/backend-common": "^0.19.9-next.2", + "@backstage/backend-defaults": "^0.2.7-next.2", "@backstage/backend-tasks": "^0.5.12-next.2", "@backstage/catalog-client": "^1.4.5", "@backstage/catalog-model": "^1.4.3", diff --git a/packages/backend/src/index.test.ts b/packages/backend/src/index.test.ts deleted file mode 100644 index 7814b8cad..000000000 --- a/packages/backend/src/index.test.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { PluginEnvironment } from './types'; - -describe('test', () => { - it('unbreaks the test runner', () => { - const unbreaker = {} as PluginEnvironment; - expect(unbreaker).toBeTruthy(); - }); -}); diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 3ce28e356..039728984 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -1,126 +1,25 @@ -/* - * Hi! - * - * Note that this is an EXAMPLE Backstage backend. Please check the README. - * - * Happy hacking! - */ - -import { - CacheManager, - DatabaseManager, - HostDiscovery, - ServerTokenManager, - UrlReaders, - createServiceBuilder, - getRootLogger, - loadBackendConfig, - notFoundHandler, -} from '@backstage/backend-common'; - -import { Config } from '@backstage/config'; -import { PluginEnvironment } from './types'; -import Router from 'express-promise-router'; -import { ServerPermissionClient } from '@backstage/plugin-permission-node'; -import { TaskScheduler } from '@backstage/backend-tasks'; -import app from './plugins/app'; -import auth from './plugins/auth'; -import badges from './plugins/badges'; -import catalog from './plugins/catalog'; -import explore from './plugins/explore'; -import proxy from './plugins/proxy'; -import search from './plugins/search'; -import techdocs from './plugins/techdocs'; -import todo from './plugins/todo'; -import { DefaultIdentityClient } from '@backstage/plugin-auth-node'; -import { CatalogClient } from '@backstage/catalog-client'; -import graphql from './plugins/graphql'; - -function makeCreateEnv(config: Config) { - const root = getRootLogger(); - const reader = UrlReaders.default({ logger: root, config }); - root.info(`Created UrlReader ${reader}`); - const discovery = HostDiscovery.fromConfig(config); - const tokenManager = ServerTokenManager.noop(); - const databaseManager = DatabaseManager.fromConfig(config); - const permissions = ServerPermissionClient.fromConfig(config, { - discovery, - tokenManager, - }); - const catalogClient = new CatalogClient({ - discoveryApi: discovery, - }); - const cacheManager = CacheManager.fromConfig(config); - const taskScheduler = TaskScheduler.fromConfig(config, { databaseManager }); - const identity = DefaultIdentityClient.create({ - discovery, - }); - - return (plugin: string): PluginEnvironment => { - const logger = root.child({ type: 'plugin', plugin }); - const database = databaseManager.forPlugin(plugin); - const cache = cacheManager.forPlugin(plugin); - const scheduler = taskScheduler.forPlugin(plugin); - - return { - logger, - cache, - database, - config, - reader, - discovery, - tokenManager, - permissions, - scheduler, - identity, - catalogClient, - }; - }; -} - -async function main() { - const config = await loadBackendConfig({ - argv: process.argv, - logger: getRootLogger(), - }); - const createEnv = makeCreateEnv(config); - - const catalogEnv = createEnv('catalog'); - const authEnv = createEnv('auth'); - const proxyEnv = createEnv('proxy'); - const searchEnv = createEnv('search'); - const techdocsEnv = createEnv('techdocs'); - const todoEnv = createEnv('todo'); - const appEnv = createEnv('app'); - const badgesEnv = createEnv('badges'); - const exploreEnv = createEnv('explore'); - const graphqlEnv = createEnv('graphql'); - - const apiRouter = Router(); - apiRouter.use('/catalog', await catalog(catalogEnv)); - apiRouter.use('/auth', await auth(authEnv)); - apiRouter.use('/search', await search(searchEnv)); - apiRouter.use('/techdocs', await techdocs(techdocsEnv)); - apiRouter.use('/todo', await todo(todoEnv)); - apiRouter.use('/proxy', await proxy(proxyEnv)); - apiRouter.use('/badges', await badges(badgesEnv)); - apiRouter.use('/explore', await explore(exploreEnv)); - apiRouter.use('/graphql', await graphql(graphqlEnv)); - apiRouter.use(notFoundHandler()); - - const service = createServiceBuilder(module) - .loadConfig(config) - .addRouter('/api', apiRouter) - .addRouter('', await app(appEnv)); - - await service.start().catch(err => { - console.log(err); - process.exit(1); - }); -} - -module.hot?.accept(); -main().catch(error => { - console.error('Backend failed to start up', error); - process.exit(1); -}); +import { legacyPlugin } from '@backstage/backend-common'; +import { createBackend } from '@backstage/backend-defaults'; +import { graphqlPlugin } from '@frontside/backstage-plugin-graphql-backend'; +import { graphqlModuleCatalog } from '@frontside/backstage-plugin-graphql-backend-module-catalog'; + +const backend = createBackend(); + +backend.add(import('@backstage/plugin-app-backend/alpha')); +backend.add(import('@backstage/plugin-auth-backend')); +backend.add(import('@backstage/plugin-badges-backend')); +backend.add(import('@backstage/plugin-catalog-backend/alpha')); +// TODO:(awanlin) replace when this is completed: https://github.com/backstage/backstage/pull/20551 +backend.add(legacyPlugin('explore', import('./plugins/explore'))); +// TODO:(awanlin) update with import when available +backend.add(graphqlPlugin); +backend.add(graphqlModuleCatalog()); +backend.add(import('@backstage/plugin-proxy-backend/alpha')); +backend.add(import('@backstage/plugin-search-backend/alpha')); +backend.add(import('@backstage/plugin-search-backend-module-catalog/alpha')); +backend.add(import('@backstage/plugin-search-backend-module-explore/alpha')); +backend.add(import('@backstage/plugin-search-backend-module-techdocs/alpha')); +backend.add(import('@backstage/plugin-techdocs-backend/alpha')); +backend.add(import('@backstage/plugin-todo-backend')); + +backend.start(); diff --git a/packages/backend/src/plugins/graphql.ts b/packages/backend/src/plugins/graphql.ts index e338b914f..e2b13538e 100644 --- a/packages/backend/src/plugins/graphql.ts +++ b/packages/backend/src/plugins/graphql.ts @@ -1,3 +1,4 @@ +import { CatalogClient } from '@backstage/catalog-client'; import { createRouter } from '@frontside/backstage-plugin-graphql-backend'; import { createCatalogLoader, @@ -8,11 +9,12 @@ import { PluginEnvironment } from '../types'; export default async function createPlugin({ logger, - catalogClient, + discovery, }: PluginEnvironment): Promise { + const catalogClient = new CatalogClient({ discoveryApi: discovery }); return await createRouter({ modules: [Catalog()], logger, loaders: { ...createCatalogLoader(catalogClient) }, }); -} +} \ No newline at end of file diff --git a/packages/backend/src/types.ts b/packages/backend/src/types.ts index b8977f824..c42628273 100644 --- a/packages/backend/src/types.ts +++ b/packages/backend/src/types.ts @@ -11,7 +11,6 @@ import { IdentityApi } from '@backstage/plugin-auth-node'; import { Logger } from 'winston'; import { PermissionEvaluator } from '@backstage/plugin-permission-common'; import { PluginTaskScheduler } from '@backstage/backend-tasks'; -import { CatalogClient } from '@backstage/catalog-client'; export type PluginEnvironment = { logger: Logger; @@ -24,5 +23,4 @@ export type PluginEnvironment = { scheduler: PluginTaskScheduler; permissions: PermissionEvaluator; identity: IdentityApi; - catalogClient: CatalogClient; }; diff --git a/yarn.lock b/yarn.lock index 88ae21e36..8fa1eec6e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3657,6 +3657,17 @@ __metadata: languageName: node linkType: hard +"@backstage/backend-defaults@npm:^0.2.7-next.2": + version: 0.2.7-next.2 + resolution: "@backstage/backend-defaults@npm:0.2.7-next.2" + dependencies: + "@backstage/backend-app-api": ^0.5.8-next.2 + "@backstage/backend-common": ^0.19.9-next.2 + "@backstage/backend-plugin-api": ^0.6.7-next.2 + checksum: 5d5fe64c0b130991ec4b1be667fafd29e929b5c6336d61f20122c474a938bf80d5e62a7e3ca75945ec52d4c9eedb8b5b91c8a4ae8612e61fd586bd6e7f3f09b8 + languageName: node + linkType: hard + "@backstage/backend-dev-utils@npm:^0.1.2": version: 0.1.2 resolution: "@backstage/backend-dev-utils@npm:0.1.2" @@ -12419,6 +12430,7 @@ __metadata: resolution: "backend@workspace:packages/backend" dependencies: "@backstage/backend-common": ^0.19.9-next.2 + "@backstage/backend-defaults": ^0.2.7-next.2 "@backstage/backend-tasks": ^0.5.12-next.2 "@backstage/catalog-client": ^1.4.5 "@backstage/catalog-model": ^1.4.3