@backstage/plugin-gateway-backend 0.0.0-nightly-20250422023845

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # @backstage/plugin-gateway-backend
2
+
3
+ ## 0.0.0-nightly-20250422023845
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @backstage/backend-plugin-api@0.0.0-nightly-20250422023845
9
+
10
+ ## 1.0.0
11
+
12
+ ### Major Changes
13
+
14
+ - 6b5681c: Initial release of `@backstage/plugin-gateway-backend`
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies
19
+ - @backstage/backend-plugin-api@1.3.0
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Gateway Backend Plugin
2
+
3
+ A plugin for managing request routing in distributed Backstage deployments.
4
+
5
+ ## Overview
6
+
7
+ This plugin is designed for organizations that have [split their backend plugins across multiple Backstage deployments](https://backstage.io/docs/backend-system/building-backends/index#split-into-multiple-backends) and implemented a custom Discovery service to resolve backend plugin URLs.
8
+
9
+ While a custom discovery service handles routing between backend plugins, it doesn't address frontend-to-backend routing without either:
10
+
11
+ - Hardcoding URLs in the frontend
12
+ - Implementing a custom reverse proxy
13
+
14
+ The Gateway Backend Plugin solves this by providing a centralized routing solution in a dedicated "gateway" Backstage deployment. It routes frontend requests to the appropriate backend plugins using the Discovery service, while prioritizing local plugins when available.
15
+
16
+ ## Installation
17
+
18
+ 1. Install the plugin package:
19
+
20
+ ```bash
21
+ # From your root directory
22
+ yarn --cwd packages/backend add @backstage/plugin-gateway-backend
23
+ ```
24
+
25
+ 2. Add the plugin to your backend in `packages/backend/src/index.ts`:
26
+
27
+ ```diff
28
+ const backend = createBackend();
29
+ // ...
30
+ + backend.add(import('@backstage/plugin-gateway-backend'));
31
+ ```
32
+
33
+ 3. Configure the `baseUrl` in your `app-config.yaml` to point to your gateway deployment:
34
+
35
+ ```yaml
36
+ backend:
37
+ # The baseUrl of your gateway Backstage deployment
38
+ baseUrl: http://gateway-backstage-backend.example.com
39
+ ```
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var plugin = require('./plugin.cjs.js');
6
+
7
+
8
+
9
+ exports.default = plugin.gatewayPlugin;
10
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
@@ -0,0 +1,10 @@
1
+ import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
2
+
3
+ /**
4
+ * gateway backend plugin
5
+ *
6
+ * @public
7
+ */
8
+ declare const gatewayPlugin: _backstage_backend_plugin_api.BackendFeature;
9
+
10
+ export { gatewayPlugin as default };
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ var backendPluginApi = require('@backstage/backend-plugin-api');
4
+ var router = require('./router.cjs.js');
5
+ var alpha = require('@backstage/backend-plugin-api/alpha');
6
+
7
+ const gatewayPlugin = backendPluginApi.createBackendPlugin({
8
+ pluginId: "gateway",
9
+ register(env) {
10
+ env.registerInit({
11
+ deps: {
12
+ logger: backendPluginApi.coreServices.logger,
13
+ rootHttpRouter: backendPluginApi.coreServices.rootHttpRouter,
14
+ instanceMeta: alpha.instanceMetadataServiceRef,
15
+ discovery: backendPluginApi.coreServices.discovery
16
+ },
17
+ async init({ logger, discovery, instanceMeta, rootHttpRouter }) {
18
+ rootHttpRouter.use(
19
+ "/api/:pluginId",
20
+ router.createRouter({
21
+ discovery,
22
+ instanceMeta,
23
+ logger
24
+ })
25
+ );
26
+ }
27
+ });
28
+ }
29
+ });
30
+
31
+ exports.gatewayPlugin = gatewayPlugin;
32
+ //# sourceMappingURL=plugin.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.cjs.js","sources":["../src/plugin.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n coreServices,\n createBackendPlugin,\n} from '@backstage/backend-plugin-api';\nimport { createRouter } from './router';\nimport { instanceMetadataServiceRef } from '@backstage/backend-plugin-api/alpha';\nimport { Handler } from 'express';\n\n/**\n * gateway backend plugin\n *\n * @public\n */\nexport const gatewayPlugin = createBackendPlugin({\n pluginId: 'gateway',\n register(env) {\n env.registerInit({\n deps: {\n logger: coreServices.logger,\n rootHttpRouter: coreServices.rootHttpRouter,\n instanceMeta: instanceMetadataServiceRef,\n discovery: coreServices.discovery,\n },\n async init({ logger, discovery, instanceMeta, rootHttpRouter }) {\n rootHttpRouter.use(\n '/api/:pluginId',\n createRouter({\n discovery,\n instanceMeta,\n logger,\n }) as Handler,\n );\n },\n });\n },\n});\n"],"names":["createBackendPlugin","coreServices","instanceMetadataServiceRef","createRouter"],"mappings":";;;;;;AA4BO,MAAM,gBAAgBA,oCAAoB,CAAA;AAAA,EAC/C,QAAU,EAAA,SAAA;AAAA,EACV,SAAS,GAAK,EAAA;AACZ,IAAA,GAAA,CAAI,YAAa,CAAA;AAAA,MACf,IAAM,EAAA;AAAA,QACJ,QAAQC,6BAAa,CAAA,MAAA;AAAA,QACrB,gBAAgBA,6BAAa,CAAA,cAAA;AAAA,QAC7B,YAAc,EAAAC,gCAAA;AAAA,QACd,WAAWD,6BAAa,CAAA;AAAA,OAC1B;AAAA,MACA,MAAM,IAAK,CAAA,EAAE,QAAQ,SAAW,EAAA,YAAA,EAAc,gBAAkB,EAAA;AAC9D,QAAe,cAAA,CAAA,GAAA;AAAA,UACb,gBAAA;AAAA,UACAE,mBAAa,CAAA;AAAA,YACX,SAAA;AAAA,YACA,YAAA;AAAA,YACA;AAAA,WACD;AAAA,SACH;AAAA;AACF,KACD,CAAA;AAAA;AAEL,CAAC;;;;"}
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ var httpProxyMiddleware = require('http-proxy-middleware');
4
+ var api = require('@opentelemetry/api');
5
+ var core = require('@opentelemetry/core');
6
+
7
+ function createRouter({
8
+ discovery,
9
+ instanceMeta
10
+ }) {
11
+ const localPluginIds = new Set(
12
+ instanceMeta.getInstalledFeatures().filter((f) => f.type === "plugin").map((f) => f.pluginId)
13
+ );
14
+ const proxy = httpProxyMiddleware.createProxyMiddleware({
15
+ changeOrigin: true,
16
+ router: async (req) => {
17
+ const pluginId = req.params.pluginId;
18
+ return discovery.getBaseUrl(pluginId);
19
+ },
20
+ on: {
21
+ proxyRes(proxyRes, _req, res) {
22
+ proxyRes.on("close", () => {
23
+ if (!res.writableEnded) {
24
+ res.end();
25
+ }
26
+ });
27
+ }
28
+ }
29
+ });
30
+ return function proxyMiddleware(req, res, next) {
31
+ if (localPluginIds.has(req.params.pluginId)) {
32
+ next();
33
+ return;
34
+ }
35
+ const rpcMetadata = core.getRPCMetadata(api.context.active());
36
+ if (rpcMetadata) {
37
+ rpcMetadata.route = req.baseUrl;
38
+ }
39
+ proxy(req, res, next);
40
+ };
41
+ }
42
+
43
+ exports.createRouter = createRouter;
44
+ //# sourceMappingURL=router.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.cjs.js","sources":["../src/router.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { DiscoveryService, LoggerService } from '@backstage/backend-plugin-api';\nimport { InstanceMetadataService } from '@backstage/backend-plugin-api/alpha';\nimport { Request, Response, NextFunction } from 'express';\nimport { createProxyMiddleware } from 'http-proxy-middleware';\nimport { context } from '@opentelemetry/api';\nimport { getRPCMetadata } from '@opentelemetry/core';\n\nexport function createRouter({\n discovery,\n instanceMeta,\n}: {\n discovery: DiscoveryService;\n instanceMeta: InstanceMetadataService;\n logger: LoggerService;\n}) {\n const localPluginIds = new Set(\n instanceMeta\n .getInstalledFeatures()\n .filter(f => f.type === 'plugin')\n .map(f => f.pluginId),\n );\n\n const proxy = createProxyMiddleware({\n changeOrigin: true,\n router: async (req: Request<{ pluginId: string }>) => {\n const pluginId = req.params.pluginId;\n return discovery.getBaseUrl(pluginId);\n },\n on: {\n proxyRes(proxyRes, _req, res) {\n // https://github.com/chimurai/http-proxy-middleware/discussions/765\n proxyRes.on('close', () => {\n if (!res.writableEnded) {\n res.end();\n }\n });\n },\n },\n });\n\n return function proxyMiddleware(\n req: Request<{ pluginId: string }>,\n res: Response,\n next: NextFunction,\n ) {\n if (localPluginIds.has(req.params.pluginId)) {\n next();\n return;\n }\n\n const rpcMetadata = getRPCMetadata(context.active());\n if (rpcMetadata) {\n rpcMetadata.route = req.baseUrl;\n }\n\n proxy(req, res, next);\n };\n}\n"],"names":["createProxyMiddleware","getRPCMetadata","context"],"mappings":";;;;;;AAsBO,SAAS,YAAa,CAAA;AAAA,EAC3B,SAAA;AAAA,EACA;AACF,CAIG,EAAA;AACD,EAAA,MAAM,iBAAiB,IAAI,GAAA;AAAA,IACzB,YACG,CAAA,oBAAA,EACA,CAAA,MAAA,CAAO,CAAK,CAAA,KAAA,CAAA,CAAE,IAAS,KAAA,QAAQ,CAC/B,CAAA,GAAA,CAAI,CAAK,CAAA,KAAA,CAAA,CAAE,QAAQ;AAAA,GACxB;AAEA,EAAA,MAAM,QAAQA,yCAAsB,CAAA;AAAA,IAClC,YAAc,EAAA,IAAA;AAAA,IACd,MAAA,EAAQ,OAAO,GAAuC,KAAA;AACpD,MAAM,MAAA,QAAA,GAAW,IAAI,MAAO,CAAA,QAAA;AAC5B,MAAO,OAAA,SAAA,CAAU,WAAW,QAAQ,CAAA;AAAA,KACtC;AAAA,IACA,EAAI,EAAA;AAAA,MACF,QAAA,CAAS,QAAU,EAAA,IAAA,EAAM,GAAK,EAAA;AAE5B,QAAS,QAAA,CAAA,EAAA,CAAG,SAAS,MAAM;AACzB,UAAI,IAAA,CAAC,IAAI,aAAe,EAAA;AACtB,YAAA,GAAA,CAAI,GAAI,EAAA;AAAA;AACV,SACD,CAAA;AAAA;AACH;AACF,GACD,CAAA;AAED,EAAA,OAAO,SAAS,eAAA,CACd,GACA,EAAA,GAAA,EACA,IACA,EAAA;AACA,IAAA,IAAI,cAAe,CAAA,GAAA,CAAI,GAAI,CAAA,MAAA,CAAO,QAAQ,CAAG,EAAA;AAC3C,MAAK,IAAA,EAAA;AACL,MAAA;AAAA;AAGF,IAAA,MAAM,WAAc,GAAAC,mBAAA,CAAeC,WAAQ,CAAA,MAAA,EAAQ,CAAA;AACnD,IAAA,IAAI,WAAa,EAAA;AACf,MAAA,WAAA,CAAY,QAAQ,GAAI,CAAA,OAAA;AAAA;AAG1B,IAAM,KAAA,CAAA,GAAA,EAAK,KAAK,IAAI,CAAA;AAAA,GACtB;AACF;;;;"}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@backstage/plugin-gateway-backend",
3
+ "version": "0.0.0-nightly-20250422023845",
4
+ "backstage": {
5
+ "role": "backend-plugin",
6
+ "pluginId": "gateway",
7
+ "pluginPackages": [
8
+ "@backstage/plugin-gateway-backend"
9
+ ],
10
+ "features": {
11
+ ".": "@backstage/BackendFeature"
12
+ }
13
+ },
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "main": "dist/index.cjs.js",
17
+ "types": "dist/index.d.ts"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/backstage/backstage",
22
+ "directory": "plugins/gateway-backend"
23
+ },
24
+ "license": "Apache-2.0",
25
+ "main": "dist/index.cjs.js",
26
+ "types": "dist/index.d.ts",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "scripts": {
31
+ "build": "backstage-cli package build",
32
+ "clean": "backstage-cli package clean",
33
+ "lint": "backstage-cli package lint",
34
+ "prepack": "backstage-cli package prepack",
35
+ "postpack": "backstage-cli package postpack",
36
+ "start": "backstage-cli package start",
37
+ "test": "backstage-cli package test"
38
+ },
39
+ "dependencies": {
40
+ "@backstage/backend-plugin-api": "0.0.0-nightly-20250422023845",
41
+ "@opentelemetry/api": "^1.9.0",
42
+ "@opentelemetry/core": "^1.29.0",
43
+ "express": "^4.17.1",
44
+ "http-proxy-middleware": "^3.0.3"
45
+ },
46
+ "devDependencies": {
47
+ "@backstage/backend-app-api": "0.0.0-nightly-20250422023845",
48
+ "@backstage/backend-defaults": "0.0.0-nightly-20250422023845",
49
+ "@backstage/backend-test-utils": "0.0.0-nightly-20250422023845",
50
+ "@backstage/cli": "0.0.0-nightly-20250422023845",
51
+ "@types/express": "^4.17.6",
52
+ "eventsource": "^3.0.6",
53
+ "wait-for-expect": "^3.0.2"
54
+ },
55
+ "typesVersions": {
56
+ "*": {
57
+ "package.json": [
58
+ "package.json"
59
+ ]
60
+ }
61
+ }
62
+ }