[inventory] move out inventory instantiation + renaming
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
+6
-3
@@ -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());
|
||||
|
||||
+17
-3
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user