diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index fca79aef1b..253959c61b 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -12,7 +12,8 @@ "test": "backstage-cli test", "prepack": "backstage-cli prepack", "postpack": "backstage-cli postpack", - "clean": "backstage-cli clean" + "clean": "backstage-cli clean", + "mock-data": "./scripts/mock-data" }, "dependencies": { "@backstage/backend-common": "^0.1.1-alpha.6", diff --git a/plugins/catalog-backend/scripts/mock-data b/plugins/catalog-backend/scripts/mock-data new file mode 100644 index 0000000000..7edc8c422b --- /dev/null +++ b/plugins/catalog-backend/scripts/mock-data @@ -0,0 +1,9 @@ +#!/usr/bin/env sh +curl \ +--location \ +--request POST 'localhost:3003/locations' \ +--header 'Content-Type: application/json' \ +--data-raw '{ + "type": "github", + "target": "https://github.com/spotify/backstage/blob/master/plugins/catalog-backend/fixtures/two_components.yaml" +}' diff --git a/plugins/catalog-backend/src/database/DatabaseManager.ts b/plugins/catalog-backend/src/database/DatabaseManager.ts index bc7c7b8dae..f0fbe92600 100644 --- a/plugins/catalog-backend/src/database/DatabaseManager.ts +++ b/plugins/catalog-backend/src/database/DatabaseManager.ts @@ -18,7 +18,7 @@ import Knex from 'knex'; import path from 'path'; import { Logger } from 'winston'; import { CommonDatabase } from './CommonDatabase'; -import type { Database } from './types'; +import { Database } from './types'; export class DatabaseManager { public static async createDatabase( @@ -31,4 +31,18 @@ export class DatabaseManager { }); return new CommonDatabase(knex, logger); } + + public static async createInMemoryDatabase( + logger: Logger, + ): Promise { + const knex = Knex({ + client: 'sqlite3', + connection: ':memory:', + useNullAsDefault: true, + }); + knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => { + resource.run('PRAGMA foreign_keys = ON', () => {}); + }); + return DatabaseManager.createDatabase(knex, logger); + } } diff --git a/plugins/catalog-backend/src/service/standaloneServer.ts b/plugins/catalog-backend/src/service/standaloneServer.ts index 2fd4afc7cd..79cf72df4d 100644 --- a/plugins/catalog-backend/src/service/standaloneServer.ts +++ b/plugins/catalog-backend/src/service/standaloneServer.ts @@ -16,8 +16,13 @@ import { Server } from 'http'; import { Logger } from 'winston'; -import { StaticEntitiesCatalog } from '../catalog'; import { createStandaloneApplication } from './standaloneApplication'; +import { DatabaseEntitiesCatalog } from '../catalog/DatabaseEntitiesCatalog'; +import { DatabaseManager } from '../database/DatabaseManager'; +import { DatabaseLocationsCatalog } from '../catalog/DatabaseLocationsCatalog'; +import { LocationReaders } from '../ingestion/source/LocationReaders'; +import { IngestionModels, DescriptorParsers, HigherOrderOperations } from '..'; +import { EntityPolicies } from '@backstage/catalog-model'; export interface ServerOptions { port: number; @@ -30,25 +35,28 @@ export async function startStandaloneServer( ): Promise { const logger = options.logger.child({ service: 'catalog-backend' }); - const entitiesCatalog = new StaticEntitiesCatalog([ - { - apiVersion: 'backstage.io/v1beta1', - kind: 'Component', - metadata: { name: 'c1' }, - spec: { type: 'service' }, - }, - { - apiVersion: 'backstage.io/v1beta1', - kind: 'Component', - metadata: { name: 'c2' }, - spec: { type: 'service' }, - }, - ]); + const db = await DatabaseManager.createInMemoryDatabase(logger); + + const entitiesCatalog = new DatabaseEntitiesCatalog(db); + const locationsCatalog = new DatabaseLocationsCatalog(db); + const ingestionModel = new IngestionModels( + new LocationReaders(), + new DescriptorParsers(), + new EntityPolicies(), + ); + const higherOrderOperation = new HigherOrderOperations( + entitiesCatalog, + locationsCatalog, + ingestionModel, + logger, + ); logger.debug('Creating application...'); const app = await createStandaloneApplication({ enableCors: options.enableCors, entitiesCatalog, + locationsCatalog, + higherOrderOperation, logger, });