Move the plugin init into their own files to keep index clean

This commit is contained in:
Fredrik Adelöw
2020-05-07 16:10:05 +02:00
parent bf4ad8d84f
commit 53b9c0766d
5 changed files with 96 additions and 27 deletions
+2 -1
View File
@@ -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"
+8 -26
View File
@@ -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());
+36
View File
@@ -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 });
}
@@ -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 });
}
+21
View File
@@ -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;
};