Skip to content

Commit

Permalink
Merge pull request #180 from backstage/revert-179-feat/migrate-to-new…
Browse files Browse the repository at this point in the history
…-backend-system

Revert "Migrate to new Backend System"
  • Loading branch information
awanlin authored Oct 24, 2023
2 parents 44c00cf + fc4e6f8 commit b39e3ca
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 44 deletions.
1 change: 0 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
},
"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",
Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PluginEnvironment } from './types';

describe('test', () => {
it('unbreaks the test runner', () => {
const unbreaker = {} as PluginEnvironment;
expect(unbreaker).toBeTruthy();
});
});
151 changes: 126 additions & 25 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,126 @@
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();
/*
* 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);
});
4 changes: 1 addition & 3 deletions packages/backend/src/plugins/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CatalogClient } from '@backstage/catalog-client';
import { createRouter } from '@frontside/backstage-plugin-graphql-backend';
import {
createCatalogLoader,
Expand All @@ -9,9 +8,8 @@ import { PluginEnvironment } from '../types';

export default async function createPlugin({
logger,
discovery,
catalogClient,
}: PluginEnvironment): Promise<Router> {
const catalogClient = new CatalogClient({ discoveryApi: discovery });
return await createRouter({
modules: [Catalog()],
logger,
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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;
Expand All @@ -23,4 +24,5 @@ export type PluginEnvironment = {
scheduler: PluginTaskScheduler;
permissions: PermissionEvaluator;
identity: IdentityApi;
catalogClient: CatalogClient;
};
18 changes: 3 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3439,8 +3439,8 @@ __metadata:
linkType: hard

"@backstage/backend-app-api@npm:^0.5.6":
version: 0.5.7
resolution: "@backstage/backend-app-api@npm:0.5.7"
version: 0.5.6
resolution: "@backstage/backend-app-api@npm:0.5.6"
dependencies:
"@backstage/backend-common": ^0.19.8
"@backstage/backend-plugin-api": ^0.6.6
Expand Down Expand Up @@ -3472,7 +3472,7 @@ __metadata:
stoppable: ^1.1.0
winston: ^3.2.1
winston-transport: ^4.5.0
checksum: dda9f785545a65006612ede044fa0abf1c7e8ea7874ca4f41f9b1c8789acb71a468203591212c313ed147cdf43ef12a94b03d34d342056ddd33fea0e58a8fde4
checksum: 2e9574642a167cb5e37e8ee18f1ffb5d40da491ae5de89c1fba84f7c8fd5986a9a51b9191a829e546e91a6aa0d276855a27254d0af21e40e1e2ed3d99323825d
languageName: node
linkType: hard

Expand Down Expand Up @@ -3543,17 +3543,6 @@ __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"
Expand Down Expand Up @@ -11697,7 +11686,6 @@ __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
Expand Down

0 comments on commit b39e3ca

Please sign in to comment.