From 53b9c0766deb5305cb20ad54f1d70d4a42fa451e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 7 May 2020 16:10:05 +0200 Subject: [PATCH] Move the plugin init into their own files to keep index clean --- packages/backend/package.json | 3 +- packages/backend/src/index.ts | 34 +++++--------------- packages/backend/src/plugins/inventory.ts | 36 ++++++++++++++++++++++ packages/backend/src/plugins/scaffolder.ts | 29 +++++++++++++++++ packages/backend/src/types.ts | 21 +++++++++++++ 5 files changed, 96 insertions(+), 27 deletions(-) create mode 100644 packages/backend/src/plugins/inventory.ts create mode 100644 packages/backend/src/plugins/scaffolder.ts create mode 100644 packages/backend/src/types.ts diff --git a/packages/backend/package.json b/packages/backend/package.json index 32be276ef2..5e398a89d5 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -32,7 +32,8 @@ "@types/helmet": "^0.0.45", "jest": "^25.1.0", "tsc-watch": "^4.2.3", - "typescript": "^3.8.3" + "typescript": "^3.8.3", + "winston": "^3.2.1" }, "nodemonConfig": { "watch": "./dist" diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index a22befd794..31b53f6d30 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -28,39 +28,21 @@ import { notFoundHandler, requestLoggingHandler, } from '@backstage/backend-common'; -import { - AggregatorInventory, - createRouter as inventoryRouter, - StaticInventory, -} from '@backstage/plugin-inventory-backend'; -import { - createRouter as scaffolderRouter, - DiskStorage, - CookieCutter, -} from '@backstage/plugin-scaffolder-backend'; +import inventory from './plugins/inventory'; +import scaffolder from './plugins/scaffolder'; import compression from 'compression'; import cors from 'cors'; import express from 'express'; import helmet from 'helmet'; +import { PluginEnvironment } from './types'; const DEFAULT_PORT = 7000; const PORT = parseInt(process.env.PORT ?? '', 10) || DEFAULT_PORT; -const logger = getRootLogger().child({ type: 'plugin' }); +const pluginEnvironment: PluginEnvironment = { + logger: getRootLogger().child({ type: 'plugin' }), +}; async function main() { - const inventory = new AggregatorInventory(); - inventory.enlist( - new StaticInventory([ - { id: 'component1' }, - { id: 'component2' }, - { id: 'component3' }, - { id: 'component4' }, - ]), - ); - - const storage = new DiskStorage({ logger }); - const templater = new CookieCutter(); - const app = express(); app.use(helmet()); @@ -68,8 +50,8 @@ async function main() { app.use(compression()); app.use(express.json()); app.use(requestLoggingHandler()); - app.use('/inventory', await inventoryRouter({ inventory, logger })); - app.use('/scaffolder', await scaffolderRouter({ storage, templater, logger })); + app.use('/inventory', await inventory(pluginEnvironment)); + app.use('/scaffolder', await scaffolder(pluginEnvironment)); app.use(notFoundHandler()); app.use(errorHandler()); diff --git a/packages/backend/src/plugins/inventory.ts b/packages/backend/src/plugins/inventory.ts new file mode 100644 index 0000000000..d48a62b5fe --- /dev/null +++ b/packages/backend/src/plugins/inventory.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { + AggregatorInventory, + createRouter, + StaticInventory, +} from '@backstage/plugin-inventory-backend'; +import type { PluginEnvironment } from '../types'; + +export default async function ({ logger }: PluginEnvironment) { + const inventory = new AggregatorInventory(); + inventory.enlist( + new StaticInventory([ + { id: 'component1' }, + { id: 'component2' }, + { id: 'component3' }, + { id: 'component4' }, + ]), + ); + + return await createRouter({ inventory, logger }); +} diff --git a/packages/backend/src/plugins/scaffolder.ts b/packages/backend/src/plugins/scaffolder.ts new file mode 100644 index 0000000000..311c9197aa --- /dev/null +++ b/packages/backend/src/plugins/scaffolder.ts @@ -0,0 +1,29 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { + CookieCutter, + createRouter, + DiskStorage, +} from '@backstage/plugin-scaffolder-backend'; +import type { PluginEnvironment } from '../types'; + +export default async function ({ logger }: PluginEnvironment) { + const storage = new DiskStorage({ logger }); + const templater = new CookieCutter(); + + return await createRouter({ storage, templater, logger }); +} diff --git a/packages/backend/src/types.ts b/packages/backend/src/types.ts new file mode 100644 index 0000000000..196337aec7 --- /dev/null +++ b/packages/backend/src/types.ts @@ -0,0 +1,21 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 type { Logger } from 'winston'; + +export type PluginEnvironment = { + logger: Logger; +};