diff --git a/packages/backend/package.json b/packages/backend/package.json index 171e169da..77308d2d5 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -18,6 +18,7 @@ }, "dependencies": { "@backstage/backend-common": "^0.19.8", + "@backstage/backend-defaults": "^0.2.6", "@backstage/backend-tasks": "^0.5.11", "@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..321b7c176 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(); \ No newline at end of file diff --git a/packages/backend/src/plugins/graphql.ts b/packages/backend/src/plugins/graphql.ts index e338b914f..aa16f006b 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,8 +9,9 @@ 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, 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 0ff8a7927..2bfe8119b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3439,8 +3439,8 @@ __metadata: linkType: hard "@backstage/backend-app-api@npm:^0.5.6": - version: 0.5.6 - resolution: "@backstage/backend-app-api@npm:0.5.6" + version: 0.5.7 + resolution: "@backstage/backend-app-api@npm:0.5.7" dependencies: "@backstage/backend-common": ^0.19.8 "@backstage/backend-plugin-api": ^0.6.6 @@ -3472,7 +3472,7 @@ __metadata: stoppable: ^1.1.0 winston: ^3.2.1 winston-transport: ^4.5.0 - checksum: 2e9574642a167cb5e37e8ee18f1ffb5d40da491ae5de89c1fba84f7c8fd5986a9a51b9191a829e546e91a6aa0d276855a27254d0af21e40e1e2ed3d99323825d + checksum: dda9f785545a65006612ede044fa0abf1c7e8ea7874ca4f41f9b1c8789acb71a468203591212c313ed147cdf43ef12a94b03d34d342056ddd33fea0e58a8fde4 languageName: node linkType: hard @@ -3543,6 +3543,17 @@ __metadata: languageName: node linkType: hard +"@backstage/backend-defaults@npm:^0.2.6": + version: 0.2.6 + resolution: "@backstage/backend-defaults@npm:0.2.6" + dependencies: + "@backstage/backend-app-api": ^0.5.6 + "@backstage/backend-common": ^0.19.8 + "@backstage/backend-plugin-api": ^0.6.6 + checksum: 7f37dc12b13423770db170ae53c3576c2e4562a6b4781bd59ca515a269e82b324c262b18cbff281e6498e17f09615c983d50c076c288bd9fe6724025ce4a60d6 + 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" @@ -11686,6 +11697,7 @@ __metadata: resolution: "backend@workspace:packages/backend" dependencies: "@backstage/backend-common": ^0.19.8 + "@backstage/backend-defaults": ^0.2.6 "@backstage/backend-tasks": ^0.5.11 "@backstage/catalog-client": ^1.4.5 "@backstage/catalog-model": ^1.4.3