second draft of the Bazaar plugin

signed-off-by: lykkeaxlin <lykkeaxlin@hotmail.com>

Co-authored-by: klaraab <klarabroman@live.se>
Signed-off-by: Lykke Axlin <lykkeaxlin@hotmail.com>
This commit is contained in:
Lykke Axlin
2021-09-14 13:11:18 +02:00
parent 34fa0b3450
commit eb75e42ce7
66 changed files with 7165 additions and 5574 deletions
+17
View File
@@ -0,0 +1,17 @@
/*
* Copyright 2020 The Backstage Authors
*
* 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.
*/
export * from './service/router';
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright 2020 The Backstage Authors
*
* 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 { getRootLogger } from '@backstage/backend-common';
import yn from 'yn';
import { startStandaloneServer } from './service/standaloneServer';
const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7000;
const enableCors = yn(process.env.PLUGIN_CORS, { default: false });
const logger = getRootLogger();
startStandaloneServer({ port, enableCors, logger }).catch(err => {
logger.error(err);
process.exit(1);
});
process.on('SIGINT', () => {
logger.info('CTRL+C pressed; exiting.');
process.exit(0);
});
@@ -0,0 +1,189 @@
/*
* Copyright 2020 The Backstage Authors
*
* 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 {
errorHandler,
PluginDatabaseManager,
resolvePackagePath,
useHotMemoize,
} from '@backstage/backend-common';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import Knex from 'knex';
import { Config } from '@backstage/config';
export interface RouterOptions {
logger: Logger;
database?: PluginDatabaseManager;
config: Config;
}
export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger, config } = options;
const db = await options.database?.getClient();
const connection = config
.getConfig('backend')
.getConfig('database')
.getConfig('connection');
logger.info('Initializing Bazaar backend');
const database = useHotMemoize(module, () => {
const knex = Knex({
client: 'postgresql',
connection: {
database: 'backstage_plugin_bazaar',
user: connection.getString('user'),
password: connection.getString('password'),
},
useNullAsDefault: true,
});
knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => {
resource.run('PRAGMA foreign_keys = ON', () => {});
});
return knex;
});
const migrationsDir = resolvePackagePath(
'@internal/plugin-bazaar-backend',
'migrations',
);
await database?.migrate.latest({
directory: migrationsDir,
});
const router = Router();
router.use(express.json());
router.get('/health', (_, response) => {
logger.info('PONG!');
response.send({ status: 'ok' });
});
router.get('/members', async (request, response) => {
const entityRef = request.headers.entity_ref;
const data = await db
?.select('*')
.from('public.members')
.where({ entity_ref: entityRef });
if (data?.length) {
response.send({ status: 'ok', data: data });
} else {
response.send({ status: 'ok', data: [] });
}
});
router.put('/members/add', async (request, response) => {
const userId = request.headers.user_id;
const entityRef = request.headers.entity_ref;
await db
?.insert({
entity_ref: entityRef,
user_id: userId,
})
.into('public.members');
response.send({ status: 'ok' });
});
router.delete('/members/remove', async (request, response) => {
const userId = request.headers.user_id;
const entityRef = request.headers.entity_ref;
const count = await db?.('public.members')
.where({ entity_ref: entityRef })
.andWhere('user_id', userId)
.del();
if (count) {
response.send({ status: 'ok' });
} else {
response.status(404).json({ message: 'Record not found' });
}
});
router.get('/metadata', async (request, response) => {
const entityRef = request.headers.entity_ref;
const data = await db
?.select('*')
.from('public.metadata')
.where({ entity_ref: entityRef });
if (data?.length) {
response.send({ status: 'ok', data: data });
} else {
response.status(404).json({ message: 'Record not found' });
}
});
router.get('/entities', async (_, response) => {
const data = await db?.select('*').from('public.metadata');
response.send({ status: 'ok', data: data });
});
router.put('/metadata', async (request, response) => {
const entityRef = request.headers.entity_ref;
const { name, announcement, status } = request.body;
const count = await db?.('public.metadata')
.where({ entity_ref: entityRef })
.update({
announcement: announcement,
status: status,
});
if (count) {
response.send({ status: 'ok' });
} else {
await db
?.insert({
name: name,
entity_ref: entityRef,
announcement: announcement,
status: status,
})
.into('public.metadata');
response.send({ status: 'ok' });
}
});
router.delete('/metadata', async (request, response) => {
const entityRef = request.headers.entity_ref;
const count = await db?.('public.metadata')
.where({ entity_ref: entityRef })
.del();
if (count) {
response.send({ status: 'ok' });
} else {
response.status(404).json({ message: 'Record not found' });
}
});
router.use(errorHandler());
return router;
}
@@ -0,0 +1,58 @@
/*
* Copyright 2020 The Backstage Authors
*
* 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 {
createServiceBuilder,
PluginDatabaseManager,
loadBackendConfig,
} from '@backstage/backend-common';
import { Server } from 'http';
import { Logger } from 'winston';
import { createRouter } from './router';
export interface ServerOptions {
port: number;
enableCors: boolean;
logger: Logger;
database?: PluginDatabaseManager;
}
export async function startStandaloneServer(
options: ServerOptions,
): Promise<Server> {
const logger = options.logger.child({ service: 'bazaar-backend' });
const config = await loadBackendConfig({ logger, argv: process.argv });
const router = await createRouter({
logger,
database: options.database,
config: config,
});
let service = createServiceBuilder(module)
.setPort(options.port)
.addRouter('/bazaar', router);
if (options.enableCors) {
service = service.enableCors({ origin: 'http://localhost:3000' });
}
return await service.start().catch(err => {
logger.error(err);
process.exit(1);
});
}
module.hot?.accept();
+17
View File
@@ -0,0 +1,17 @@
/*
* Copyright 2020 The Backstage Authors
*
* 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.
*/
export {};