Add proxyEndpointsExtensionPoint

Signed-off-by: Matt Benson <gudnabrsam@gmail.com>
This commit is contained in:
Matt Benson
2024-10-18 14:09:51 -05:00
parent 41ab5cf4a0
commit 11b001cf4d
14 changed files with 235 additions and 1 deletions
+28
View File
@@ -15,7 +15,35 @@
*/
import { createBackend } from '@backstage/backend-defaults';
import { createBackendModule } from '@backstage/backend-plugin-api';
import { proxyEndpointsExtensionPoint } from '@backstage/plugin-proxy-node/alpha';
const backend = createBackend();
backend.add(import('../src/alpha'));
backend.add(
createBackendModule({
pluginId: 'proxy',
moduleId: 'demo-additional-endpoints',
register: reg => {
reg.registerInit({
deps: {
proxyEndpoints: proxyEndpointsExtensionPoint,
},
init: async ({ proxyEndpoints }) => {
proxyEndpoints.addProxyEndpoints({
...Object.fromEntries(
['foo', 'bar', 'baz'].map(msv => [
`/${msv}`,
{ target: `http://${msv}.org` },
]),
),
'/gocd': 'http://cannot-override-config.no',
});
},
});
},
}),
);
backend.start();
+1
View File
@@ -56,6 +56,7 @@
"@backstage/backend-common": "^0.25.0",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/plugin-proxy-node": "workspace:^",
"@backstage/types": "workspace:^",
"@types/express": "^4.17.6",
"express": "^4.17.1",
+3
View File
@@ -6,6 +6,7 @@
import { BackendFeature } from '@backstage/backend-plugin-api';
import { DiscoveryService } from '@backstage/backend-plugin-api';
import express from 'express';
import { JsonObject } from '@backstage/types';
import { Logger } from 'winston';
import { RootConfigService } from '@backstage/backend-plugin-api';
@@ -18,6 +19,8 @@ export default proxyPlugin;
// @public @deprecated (undocumented)
export interface RouterOptions {
// (undocumented)
additionalEndpoints?: JsonObject;
// (undocumented)
config: RootConfigService;
// (undocumented)
+10
View File
@@ -20,6 +20,8 @@ import {
coreServices,
} from '@backstage/backend-plugin-api';
import { createRouterInternal } from './service/router';
import { proxyEndpointsExtensionPoint } from '@backstage/plugin-proxy-node/alpha';
import { JsonObject } from '@backstage/types';
/**
* The proxy backend plugin.
@@ -29,6 +31,13 @@ import { createRouterInternal } from './service/router';
export const proxyPlugin = createBackendPlugin({
pluginId: 'proxy',
register(env) {
const additionalEndpoints = {};
env.registerExtensionPoint(proxyEndpointsExtensionPoint, {
addProxyEndpoints(endpoints: JsonObject) {
Object.assign(additionalEndpoints, endpoints);
},
});
env.registerInit({
deps: {
config: coreServices.rootConfig,
@@ -42,6 +51,7 @@ export const proxyPlugin = createBackendPlugin({
discovery,
logger: loggerToWinstonLogger(logger),
httpRouterService: httpRouter,
additionalEndpoints,
});
},
});
+5 -1
View File
@@ -63,6 +63,7 @@ export interface RouterOptions {
discovery: DiscoveryService;
skipInvalidProxies?: boolean;
reviveConsumedRequestBodies?: boolean;
additionalEndpoints?: JsonObject;
}
export interface ProxyConfig extends Options {
@@ -315,7 +316,10 @@ export async function createRouterInternal(
const externalUrl = await options.discovery.getExternalBaseUrl('proxy');
const { pathname: pathPrefix } = new URL(externalUrl);
const proxyConfig = readProxyConfig(options.config, options.logger);
const proxyConfig = {
...(options.additionalEndpoints ?? {}),
...readProxyConfig(options.config, options.logger),
};
configureMiddlewares(
proxyOptions,
currentRouter,