From 7c6b7b9b94b3ee43c47f9fa7c8dcfd261de7067a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Fri, 1 May 2020 22:31:54 +0200 Subject: [PATCH] [inventory] move out inventory instantiation + renaming --- packages/backend/src/index.ts | 23 +++++++++++++------ packages/backend/src/test/index.ts | 23 ------------------- plugins/inventory-backend/src/index.ts | 1 + .../src/inventory/AggregatorInventory.ts | 2 +- .../src/inventory/StaticInventory.ts | 2 +- plugins/inventory-backend/src/run.ts | 20 ++++++++-------- .../inventory-backend/src/service/router.ts | 14 +++-------- ...pplication.ts => standaloneApplication.ts} | 9 +++++--- .../{server.ts => standaloneServer.ts} | 20 +++++++++++++--- 9 files changed, 55 insertions(+), 59 deletions(-) delete mode 100644 packages/backend/src/test/index.ts rename plugins/inventory-backend/src/service/{application.ts => standaloneApplication.ts} (83%) rename plugins/inventory-backend/src/service/{server.ts => standaloneServer.ts} (70%) diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index e9fc15f1c1..f8456a0e34 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -28,21 +28,31 @@ import { notFoundHandler, requestLoggingHandler, } from '@backstage/backend-common'; -import { createRouter as inventoryRouter } from '@backstage/plugin-inventory-backend'; +import { + AggregatorInventory, + createRouter as inventoryRouter, + StaticInventory, +} from '@backstage/plugin-inventory-backend'; import { createScaffolder } from '@backstage/plugin-scaffolder-backend'; import compression from 'compression'; import cors from 'cors'; import express from 'express'; import helmet from 'helmet'; -import { testRouter } from './test'; const DEFAULT_PORT = 7000; const PORT = parseInt(process.env.PORT ?? '', 10) || DEFAULT_PORT; const logger = getRootLogger().child({ type: 'plugin' }); async function main() { - const inventory = await inventoryRouter({ logger }); - const scaffolder = createScaffolder(); + const inventory = new AggregatorInventory(); + inventory.enlist( + new StaticInventory([ + { id: 'component1' }, + { id: 'component2' }, + { id: 'component3' }, + { id: 'component4' }, + ]), + ); const app = express(); @@ -51,9 +61,8 @@ async function main() { app.use(compression()); app.use(express.json()); app.use(requestLoggingHandler()); - app.use('/test', testRouter); - app.use('/inventory', inventory); - app.use('/scaffolder', scaffolder); + app.use('/inventory', await inventoryRouter({ inventory, logger })); + app.use('/scaffolder', createScaffolder()); app.use(notFoundHandler()); app.use(errorHandler()); diff --git a/packages/backend/src/test/index.ts b/packages/backend/src/test/index.ts deleted file mode 100644 index ce3f9f8c39..0000000000 --- a/packages/backend/src/test/index.ts +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 { Router } from 'express'; - -export const testRouter = Router(); - -testRouter.get('/', async (_, res) => { - res.status(200).send('hello'); -}); diff --git a/plugins/inventory-backend/src/index.ts b/plugins/inventory-backend/src/index.ts index 7612c392a2..52b4ed9a53 100644 --- a/plugins/inventory-backend/src/index.ts +++ b/plugins/inventory-backend/src/index.ts @@ -14,4 +14,5 @@ * limitations under the License. */ +export * from './inventory'; export * from './service/router'; diff --git a/plugins/inventory-backend/src/inventory/AggregatorInventory.ts b/plugins/inventory-backend/src/inventory/AggregatorInventory.ts index babea8c7b9..ef01be962f 100644 --- a/plugins/inventory-backend/src/inventory/AggregatorInventory.ts +++ b/plugins/inventory-backend/src/inventory/AggregatorInventory.ts @@ -19,7 +19,7 @@ import { Component, Inventory } from './types'; export class AggregatorInventory implements Inventory { inventories: Inventory[] = []; - list(): Promise> { + list(): Promise { return Promise.all(this.inventories.map((i) => i.list())).then((lists) => lists.flat(), ); diff --git a/plugins/inventory-backend/src/inventory/StaticInventory.ts b/plugins/inventory-backend/src/inventory/StaticInventory.ts index fdfc2afde7..95a4466392 100644 --- a/plugins/inventory-backend/src/inventory/StaticInventory.ts +++ b/plugins/inventory-backend/src/inventory/StaticInventory.ts @@ -19,7 +19,7 @@ import { Component, Inventory } from './types'; export class StaticInventory implements Inventory { constructor(private components: Component[]) {} - list(): Promise> { + list(): Promise { return Promise.resolve([...this.components]); } diff --git a/plugins/inventory-backend/src/run.ts b/plugins/inventory-backend/src/run.ts index 9694a60c33..0d5c3a5c12 100644 --- a/plugins/inventory-backend/src/run.ts +++ b/plugins/inventory-backend/src/run.ts @@ -15,20 +15,20 @@ */ import { getRootLogger } from '@backstage/backend-common'; -import { startServer } from './service/server'; +import { startStandaloneServer } from './service/standaloneServer'; -startServer({ - port: process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 3003, - enableCors: process.env.PLUGIN_CORS - ? Boolean(process.env.PLUGIN_CORS) - : false, - logger: getRootLogger(), -}).catch((err) => { - getRootLogger().error(err); +const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 3003; +const enableCors = process.env.PLUGIN_CORS + ? Boolean(process.env.PLUGIN_CORS) + : false; +const logger = getRootLogger(); + +startStandaloneServer({ port, enableCors, logger }).catch((err) => { + logger.error(err); process.exit(1); }); process.on('SIGINT', () => { - getRootLogger().info('CTRL+C pressed; exiting.'); + logger.info('CTRL+C pressed; exiting.'); process.exit(0); }); diff --git a/plugins/inventory-backend/src/service/router.ts b/plugins/inventory-backend/src/service/router.ts index afa4a543b3..efaff00cb8 100644 --- a/plugins/inventory-backend/src/service/router.ts +++ b/plugins/inventory-backend/src/service/router.ts @@ -16,27 +16,19 @@ import express from 'express'; import { Logger } from 'winston'; -import { AggregatorInventory, StaticInventory } from '../inventory'; +import { Inventory } from '../inventory'; export interface RouterOptions { + inventory: Inventory; logger: Logger; } export async function createRouter( options: RouterOptions, ): Promise { + const inventory = options.inventory; const logger = options.logger.child({ plugin: 'inventory' }); - const inventory = new AggregatorInventory(); - inventory.enlist( - new StaticInventory([ - { id: 'component1' }, - { id: 'component2' }, - { id: 'component3' }, - { id: 'component4' }, - ]), - ); - const router = express.Router(); router .get('/', async (req, res) => { diff --git a/plugins/inventory-backend/src/service/application.ts b/plugins/inventory-backend/src/service/standaloneApplication.ts similarity index 83% rename from plugins/inventory-backend/src/service/application.ts rename to plugins/inventory-backend/src/service/standaloneApplication.ts index e5cf151050..8f0518bb8d 100644 --- a/plugins/inventory-backend/src/service/application.ts +++ b/plugins/inventory-backend/src/service/standaloneApplication.ts @@ -24,26 +24,29 @@ import cors from 'cors'; import express from 'express'; import helmet from 'helmet'; import { Logger } from 'winston'; +import { Inventory } from '../inventory'; import { createRouter } from './router'; export interface ApplicationOptions { enableCors: boolean; + inventory: Inventory; logger: Logger; } -export async function createApplication( +export async function createStandaloneApplication( options: ApplicationOptions, ): Promise { + const { enableCors, inventory, logger } = options; const app = express(); app.use(helmet()); - if (options.enableCors) { + if (enableCors) { app.use(cors()); } app.use(compression()); app.use(express.json()); app.use(requestLoggingHandler()); - app.use('/', await createRouter({ logger: options.logger })); + app.use('/', await createRouter({ inventory, logger })); app.use(notFoundHandler()); app.use(errorHandler()); diff --git a/plugins/inventory-backend/src/service/server.ts b/plugins/inventory-backend/src/service/standaloneServer.ts similarity index 70% rename from plugins/inventory-backend/src/service/server.ts rename to plugins/inventory-backend/src/service/standaloneServer.ts index 9715a72fef..be339c5ecc 100644 --- a/plugins/inventory-backend/src/service/server.ts +++ b/plugins/inventory-backend/src/service/standaloneServer.ts @@ -16,7 +16,8 @@ import { Server } from 'http'; import { Logger } from 'winston'; -import { createApplication } from './application'; +import { AggregatorInventory, StaticInventory } from '../inventory'; +import { createStandaloneApplication } from './standaloneApplication'; export interface ServerOptions { port: number; @@ -24,12 +25,25 @@ export interface ServerOptions { logger: Logger; } -export async function startServer(options: ServerOptions): Promise { +export async function startStandaloneServer( + options: ServerOptions, +): Promise { const logger = options.logger.child({ service: 'inventory-backend' }); + const inventory = new AggregatorInventory(); + inventory.enlist( + new StaticInventory([ + { id: 'component1' }, + { id: 'component2' }, + { id: 'component3' }, + { id: 'component4' }, + ]), + ); + logger.debug('Creating application...'); - const app = await createApplication({ + const app = await createStandaloneApplication({ enableCors: options.enableCors, + inventory, logger, });