Skip to content

Commit

Permalink
add imported events to the async api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Aug 14, 2024
1 parent f10e80d commit 45eb5d5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/engine/paima-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {
registerDocsPrecompiles,
registerDocsOpenAPI,
registerValidationErrorHandler,
registerDocsAppEvents,
} from './server.js';
export { TimeoutError } from './utils.js';

Expand Down
33 changes: 22 additions & 11 deletions packages/engine/paima-runtime/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { merge, isErrorResult } from 'openapi-merge';
import { doLog, ENV, logError } from '@paima/utils';
import path from 'path';
import { ValidateError } from 'tsoa';
import { BuiltinEvents, toAsyncApi } from '@paima/events';
import { BuiltinEvents, EventPathAndDef, toAsyncApi } from '@paima/events';
import YAML from 'yaml';
import { evmRpcEngine } from './evm-rpc/eip1193.js';
import { StatusCodes } from 'http-status-codes';
Expand Down Expand Up @@ -141,17 +141,27 @@ function registerDocsOpenAPI(userStateMachineApi: object | undefined): void {
);
}

server.get(`/${DocPaths.Root}/${DocPaths.AsyncApi.Root}/${DocPaths.AsyncApi.Spec}`, (_, res) => {
const asyncApi = toAsyncApi(
{
backendUri: ENV.MQTT_ENGINE_BROKER_URL,
// TODO: batcher docs theoretically should be hosted separately in some batcher-managed server
batcherUri: ENV.MQTT_BATCHER_BROKER_URL,
},
BuiltinEvents
function registerDocsAppEvents(events: Record<string, EventPathAndDef[]>): void {
const appEvents: [string, EventPathAndDef][] = Object.entries(events).flatMap(
([name, definitions]) =>
definitions.map((definition: EventPathAndDef, index: number): [string, EventPathAndDef] => [
name + index,
definition,
])
);
res.send(YAML.stringify(asyncApi, null, 2));
});

server.get(`/${DocPaths.Root}/${DocPaths.AsyncApi.Root}/${DocPaths.AsyncApi.Spec}`, (_, res) => {
const asyncApi = toAsyncApi(
{
backendUri: ENV.MQTT_ENGINE_BROKER_URL,
// TODO: batcher docs theoretically should be hosted separately in some batcher-managed server
batcherUri: ENV.MQTT_BATCHER_BROKER_URL,
},
(Object.entries(BuiltinEvents) as [string, EventPathAndDef][]).concat(appEvents)
);
res.send(YAML.stringify(asyncApi, null, 2));
});
}

server.get(`/${DocPaths.Root}/${DocPaths.AsyncApi.Root}/${DocPaths.AsyncApi.Ui}`, (_, res) => {
res.sendFile(path.join(__dirname, 'public', 'asyncapi.html'));
Expand All @@ -174,4 +184,5 @@ export {
registerDocsPrecompiles,
registerDocsOpenAPI,
registerValidationErrorHandler,
registerDocsAppEvents,
};
2 changes: 2 additions & 0 deletions packages/engine/paima-standalone/src/utils/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FunnelFactory } from '@paima/funnel';
import paimaRuntime, {
registerDocsAppEvents,
registerDocsOpenAPI,
registerDocsPrecompiles,
registerValidationErrorHandler,
Expand Down Expand Up @@ -172,6 +173,7 @@ export const runPaimaEngine = async (): Promise<void> => {
registerDocsOpenAPI(importOpenApiJson());
registerDocsPrecompiles(precompilesImport.precompiles);
registerValidationErrorHandler();
registerDocsAppEvents(eventsImport.events);

void engine.run(ENV.STOP_BLOCKHEIGHT, ENV.SERVER_ONLY_MODE);
} else {
Expand Down
9 changes: 3 additions & 6 deletions packages/paima-sdk/paima-events/src/builtin-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ type HostInfo = {
backendUri: string;
batcherUri?: string;
};
export function toAsyncApi(
info: HostInfo,
events: Record<string, EventPathAndDef>
): AsyncAPI300Schema {
export function toAsyncApi(info: HostInfo, events: [string, EventPathAndDef][]): AsyncAPI300Schema {
const parsedUrl = new URL(info.backendUri);
const servers: NonNullable<AsyncAPI300Schema['servers']> = {
[PaimaEventBrokerNames.PaimaEngine]: {
Expand All @@ -132,7 +129,7 @@ export function toAsyncApi(
}

const channels: Channels = {};
for (const [k, v] of Object.entries(events)) {
for (const [k, v] of events) {
if (v.broker === PaimaEventBrokerNames.Batcher && info.batcherUri == null) {
continue;
}
Expand Down Expand Up @@ -169,7 +166,7 @@ export function toAsyncApi(
}

const operations: Operations = {};
for (const [k, v] of Object.entries(events)) {
for (const [k, v] of events) {
if (v.broker === PaimaEventBrokerNames.Batcher && info.batcherUri == null) {
continue;
}
Expand Down

0 comments on commit 45eb5d5

Please sign in to comment.