From 85abf247eff54b2939d1d700a6070c694f75bb7c Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 20 May 2024 13:52:26 +0200 Subject: [PATCH] events-backend: add dev setup Signed-off-by: Patrik Oldsberg --- plugins/events-backend/dev/index.ts | 75 +++++++++++++++++++++++++++++ plugins/events-backend/package.json | 1 + yarn.lock | 1 + 3 files changed, 77 insertions(+) create mode 100644 plugins/events-backend/dev/index.ts diff --git a/plugins/events-backend/dev/index.ts b/plugins/events-backend/dev/index.ts new file mode 100644 index 0000000000..c0626543c8 --- /dev/null +++ b/plugins/events-backend/dev/index.ts @@ -0,0 +1,75 @@ +/* + * Copyright 2024 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 { createBackend } from '@backstage/backend-defaults'; +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; +import { eventsServiceRef } from '@backstage/plugin-events-node'; + +const backend = createBackend(); + +backend.add(import('../src/alpha')); + +backend.add( + createBackendPlugin({ + pluginId: 'producer', + register(reg) { + reg.registerInit({ + deps: { + events: eventsServiceRef, + logger: coreServices.logger, + }, + async init({ events, logger }) { + setInterval(() => { + logger.info(`Publishing event to topic 'test'`); + events.publish({ + eventPayload: { foo: 'bar' }, + topic: 'test', + metadata: { meta: 'baz' }, + }); + }, 5000); + }, + }); + }, + }), +); + +backend.add( + createBackendPlugin({ + pluginId: 'consumer', + register(reg) { + reg.registerInit({ + deps: { + events: eventsServiceRef, + logger: coreServices.logger, + }, + async init({ events, logger }) { + events.subscribe({ + id: 'test-1', + topics: ['test'], + async onEvent(event) { + logger.info(`Received event: ${JSON.stringify(event, null, 2)}`); + }, + }); + }, + }); + }, + }), +); + +backend.start(); diff --git a/plugins/events-backend/package.json b/plugins/events-backend/package.json index 34f64d05e0..c25109cf82 100644 --- a/plugins/events-backend/package.json +++ b/plugins/events-backend/package.json @@ -61,6 +61,7 @@ "winston": "^3.2.1" }, "devDependencies": { + "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@backstage/plugin-events-backend-test-utils": "workspace:^", diff --git a/yarn.lock b/yarn.lock index 8a3b7b8023..cee8a0c9ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6138,6 +6138,7 @@ __metadata: resolution: "@backstage/plugin-events-backend@workspace:plugins/events-backend" dependencies: "@backstage/backend-common": ^0.25.0 + "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/cli": "workspace:^"