Skip to content

Commit

Permalink
feat: migrated to new backend system again
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Wanlin <awanlin@spotify.com>
  • Loading branch information
awanlin committed Nov 9, 2023
1 parent 590f791 commit 2179d00
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 138 deletions.
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 0 additions & 8 deletions packages/backend/src/index.test.ts

This file was deleted.

151 changes: 25 additions & 126 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 4 additions & 2 deletions packages/backend/src/plugins/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CatalogClient } from '@backstage/catalog-client';
import { createRouter } from '@frontside/backstage-plugin-graphql-backend';
import {
createCatalogLoader,
Expand All @@ -8,11 +9,12 @@ import { PluginEnvironment } from '../types';

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

0 comments on commit 2179d00

Please sign in to comment.