Merge pull request #707 from spotify/freben/iteraaaaate

[inventory] move out inventory instantiation + renaming
This commit is contained in:
Fredrik Adelöw
2020-05-04 16:05:32 +02:00
committed by GitHub
9 changed files with 55 additions and 59 deletions
+16 -7
View File
@@ -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());
-23
View File
@@ -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');
});
+1
View File
@@ -14,4 +14,5 @@
* limitations under the License.
*/
export * from './inventory';
export * from './service/router';
@@ -19,7 +19,7 @@ import { Component, Inventory } from './types';
export class AggregatorInventory implements Inventory {
inventories: Inventory[] = [];
list(): Promise<Array<Component>> {
list(): Promise<Component[]> {
return Promise.all(this.inventories.map((i) => i.list())).then((lists) =>
lists.flat(),
);
@@ -19,7 +19,7 @@ import { Component, Inventory } from './types';
export class StaticInventory implements Inventory {
constructor(private components: Component[]) {}
list(): Promise<Array<Component>> {
list(): Promise<Component[]> {
return Promise.resolve([...this.components]);
}
+10 -10
View File
@@ -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);
});
@@ -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<express.Router> {
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) => {
@@ -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<express.Application> {
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());
@@ -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<Server> {
export async function startStandaloneServer(
options: ServerOptions,
): Promise<Server> {
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,
});