events-backend: add dev setup

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-05-20 13:52:26 +02:00
parent d0a007356b
commit 85abf247ef
3 changed files with 77 additions and 0 deletions
+75
View File
@@ -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();
+1
View File
@@ -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:^",
+1
View File
@@ -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:^"