Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wsdl): use client catalog on backend side to load definition #7

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/silent-jars-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@dweber019/backstage-plugin-api-docs-module-wsdl-backend': patch
'@dweber019/backstage-plugin-api-docs-module-wsdl': patch
---

Use client catalog on backend side to load definition instead of providing definition from frontend.
2 changes: 2 additions & 0 deletions app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ catalog:
# Local example data, file locations are relative to the backend process, typically `packages/backend`
- type: file
target: ../entities/wsdl-api-example.yaml
- type: file
target: ../entities/wsdl-api-example-big.yaml
- type: file
target: ../entities/end-of-life-resource-examples.yaml

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/plugins/apiDocsModuleWsdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { PluginEnvironment } from '../types';
export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
return await createRouter({ logger: env.logger });
return await createRouter({ logger: env.logger, discovery: env.discovery });
}
209 changes: 209 additions & 0 deletions packages/entities/wsdl-api-example-big.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugins/api-docs-module-wsdl-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { PluginEnvironment } from '../types';
export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
return await createRouter({ logger: env.logger });
return await createRouter({ logger: env.logger, discovery: env.discovery });
}
```

Expand Down
2 changes: 2 additions & 0 deletions plugins/api-docs-module-wsdl-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"dependencies": {
"@backstage/backend-common": "^0.20.1",
"@backstage/backend-plugin-api": "^0.6.9",
"@backstage/catalog-client": "^1.6.0",
"@backstage/catalog-model": "^1.4.4",
"@types/express": "^4.17.6",
"cross-fetch": "^3.1.5",
"express": "^4.17.1",
Expand Down
4 changes: 3 additions & 1 deletion plugins/api-docs-module-wsdl-backend/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export const apiDocsModuleWsdlPlugin = createBackendPlugin({
deps: {
logger: coreServices.logger,
httpRouter: coreServices.httpRouter,
discovery: coreServices.discovery,
},
async init({ logger, httpRouter }) {
async init({ logger, httpRouter, discovery }) {
httpRouter.use(
await createRouter({
logger: loggerToWinstonLogger(logger),
discovery,
}),
);
},
Expand Down
27 changes: 19 additions & 8 deletions plugins/api-docs-module-wsdl-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import express from 'express';
import Router from 'express-promise-router';
import fetch from 'cross-fetch';
import { Logger } from 'winston';
import { CatalogClient } from '@backstage/catalog-client';
import { PluginEndpointDiscovery } from '@backstage/backend-common';
import { ApiEntity } from '@backstage/catalog-model';
// @ts-ignore
import SaxonJS from 'saxon-js';
import styleSheet from '../stylesheet.sef.json';

export interface RouterOptions {
logger: Logger;
}

const downloadExternalSchema = async (uri: string, logger: Logger) => {
try {
const value = await fetch(uri);
Expand Down Expand Up @@ -100,11 +99,19 @@ const wsdlToHtml = async (xml: string, logger: Logger) => {
).then((output: { principalResult: string }) => output.principalResult);
};

export interface RouterOptions {
logger: Logger;
discovery: PluginEndpointDiscovery;
}

/** @public */
export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger } = options;
const { logger, discovery } = options;

const catalogClient = new CatalogClient({ discoveryApi: discovery });

const router = Router();
router.use(express.text());

Expand All @@ -113,9 +120,13 @@ export async function createRouter(
response.json({ status: 'ok' });
});

router.post('/v1/convert', async (req, res) => {
const entityLogger = logger.child({ entity: req.query.entityRef });
const result = await wsdlToHtml(req.body.toString(), entityLogger);
router.get('/v1/convert', async (req, res) => {
const entityRef = req.query.entityRef as string;
const entityLogger = logger.child({ entity: entityRef });
const apiEntity = (await catalogClient.getEntityByRef(
entityRef,
)) as ApiEntity;
const result = await wsdlToHtml(apiEntity.spec.definition, entityLogger);
res.send(result);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createServiceBuilder } from '@backstage/backend-common';
import {
createServiceBuilder,
HostDiscovery,
loadBackendConfig,
} from '@backstage/backend-common';
import { Server } from 'http';
import { Logger } from 'winston';
import { createRouter } from './router';
Expand All @@ -16,7 +20,11 @@ export async function startStandaloneServer(
service: 'api-docs-module-wsdl-backend',
});
logger.info('Starting application server...');
const router = await createRouter({ logger });
const config = await loadBackendConfig({ logger, argv: process.argv });
const router = await createRouter({
logger,
discovery: HostDiscovery.fromConfig(config),
});

let service = createServiceBuilder(module)
.setPort(options.port)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ export class ApiDocsModuleWsdlClient implements ApiDocsModuleWsdlApi {
this.identityApi = options.identityApi;
}

async convert(xml: string, entityRef: string): Promise<string> {
async convert(entityRef: string): Promise<string> {
const baseUrl = await this.discoveryApi.getBaseUrl('api-docs-module-wsdl');
const { token } = await this.identityApi.getCredentials();

const res = await fetch(`${baseUrl}/v1/convert?entityRef=${entityRef}`, {
method: 'POST',
method: 'GET',
headers: token
? {
Authorization: `Bearer ${token}`,
}
: undefined,
body: xml,
});

if (!res.ok) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/api-docs-module-wsdl/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ApiDocsModuleWsdlApi {
*
* @public
*/
convert(xml: string, entityRef: string): Promise<string>;
convert(entityRef: string): Promise<string>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export const WsdlDefinition = ({ definition }: WsdlDefinitionProps) => {
const apiDocsModuleWsdlDocApi = useApi(apiDocsModuleWsdlApiRef);
const { entity } = useEntity();
const result = useAsync(() => {
return apiDocsModuleWsdlDocApi.convert(
definition,
stringifyEntityRef(entity),
);
return apiDocsModuleWsdlDocApi.convert(stringifyEntityRef(entity));
}, [definition]);

if (result.loading) {
Expand Down
Loading
Loading