TechDocs: Use discovery instead of config to read base url

This commit is contained in:
Himanshu Mishra
2020-11-17 21:03:56 +01:00
parent c01bf361a4
commit 3b314807c6
6 changed files with 32 additions and 23 deletions
+1
View File
@@ -23,6 +23,7 @@
"@backstage/backend-common": "^0.2.0",
"@backstage/catalog-model": "^0.2.0",
"@backstage/config": "^0.1.1",
"@backstage/core-api": "^0.2.1",
"@types/dockerode": "^2.5.34",
"@types/express": "^4.17.6",
"command-exists-promise": "^2.0.2",
@@ -53,7 +53,7 @@ export async function startStandaloneServer(
const techdocsGenerator = new TechdocsGenerator(logger, config);
generators.register('techdocs', techdocsGenerator);
const publisher = new LocalPublish(logger, config);
const publisher = new LocalPublish(logger, discovery);
const dockerClient = new Docker();
@@ -17,7 +17,7 @@
/* eslint-disable no-restricted-syntax */
import fs from 'fs-extra';
import path from 'path';
import { getVoidLogger } from '@backstage/backend-common';
import { getVoidLogger, SingleHostDiscovery } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import { LocalPublish } from './local';
@@ -38,8 +38,21 @@ const logger = getVoidLogger();
describe('local publisher', () => {
it('should publish generated documentation dir', async () => {
const testConfig = ConfigReader.fromConfigs([{ context: '', data: {} }]);
const publisher = new LocalPublish(logger, testConfig);
const testConfig = ConfigReader.fromConfigs([
{
context: '',
data: {
backend: {
baseUrl: 'http://localhost:7000',
listen: {
port: 7000,
},
},
},
},
]);
const testDiscovery = SingleHostDiscovery.fromConfig(testConfig);
const publisher = new LocalPublish(logger, testDiscovery);
const mockEntity = createMockEntity();
@@ -18,15 +18,15 @@ import { Logger } from 'winston';
import { Entity } from '@backstage/catalog-model';
import { PublisherBase } from './types';
import { resolvePackagePath } from '@backstage/backend-common';
import { Config } from '@backstage/config';
import { DiscoveryApi } from '@backstage/core-api';
export class LocalPublish implements PublisherBase {
private readonly logger: Logger;
private readonly config: Config;
private readonly discoveryApi: DiscoveryApi;
constructor(logger: Logger, config: Config) {
constructor(logger: Logger, discoveryApi: DiscoveryApi) {
this.logger = logger;
this.config = config;
this.discoveryApi = discoveryApi;
}
publish({
@@ -66,9 +66,10 @@ export class LocalPublish implements PublisherBase {
reject(err);
}
const backendBaseUrl = this.config.getString('backend.baseUrl');
resolve({
remoteUrl: `${backendBaseUrl}/api/techdocs/static/docs/${entity.metadata.name}`,
this.discoveryApi.getBaseUrl('techdocs').then(techdocsApiUrl => {
resolve({
remoteUrl: `${techdocsApiUrl}/static/docs/${entity.metadata.name}`,
});
});
});
});