Added:
- Config management - path rewrite Signed-off-by: Karan Shah <karan.shah@simplybusiness.co.uk>
This commit is contained in:
@@ -25,12 +25,14 @@
|
||||
"@types/express": "*",
|
||||
"express": "^4.17.1",
|
||||
"express-promise-router": "^4.1.0",
|
||||
"http-proxy-middleware": "^2.0.0",
|
||||
"winston": "^3.2.1",
|
||||
"cross-fetch": "^3.0.6",
|
||||
"yn": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "^0.13.1-next.0",
|
||||
"@types/http-proxy-middleware": "^0.19.3",
|
||||
"@types/supertest": "^2.0.8",
|
||||
"supertest": "^4.0.2",
|
||||
"msw": "^0.35.0"
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2022 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { Config } from '@backstage/config';
|
||||
|
||||
export type AirbrakeConfig = {
|
||||
apiKey: string;
|
||||
};
|
||||
|
||||
export function extractAirbrakeConfig(config: Config): AirbrakeConfig {
|
||||
return {
|
||||
apiKey: config.getString('integrations.airbrake.apiKey'),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2022 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { extractAirbrakeConfig } from './ExtractAirbrakeConfig';
|
||||
export type { AirbrakeConfig } from './ExtractAirbrakeConfig';
|
||||
@@ -17,15 +17,26 @@
|
||||
import { getVoidLogger } from '@backstage/backend-common';
|
||||
import express from 'express';
|
||||
import request from 'supertest';
|
||||
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { createRouter } from './router';
|
||||
import { extractAirbrakeConfig } from '../config';
|
||||
|
||||
describe('createRouter', () => {
|
||||
let app: express.Express;
|
||||
|
||||
beforeAll(async () => {
|
||||
const config = new ConfigReader({
|
||||
integrations: {
|
||||
airbrake: {
|
||||
apiKey: 'fakeApiKey',
|
||||
},
|
||||
},
|
||||
});
|
||||
const airbrakeConfig = extractAirbrakeConfig(config);
|
||||
|
||||
const router = await createRouter({
|
||||
logger: getVoidLogger(),
|
||||
airbrakeConfig,
|
||||
});
|
||||
app = express().use(router);
|
||||
});
|
||||
|
||||
@@ -18,9 +18,12 @@ import { errorHandler } from '@backstage/backend-common';
|
||||
import express from 'express';
|
||||
import Router from 'express-promise-router';
|
||||
import { Logger } from 'winston';
|
||||
import { createProxyMiddleware } from 'http-proxy-middleware';
|
||||
import { AirbrakeConfig } from '../config';
|
||||
|
||||
export interface RouterOptions {
|
||||
logger: Logger;
|
||||
airbrakeConfig: AirbrakeConfig;
|
||||
}
|
||||
|
||||
export async function createRouter(
|
||||
@@ -35,6 +38,26 @@ export async function createRouter(
|
||||
logger.info('PONG!');
|
||||
response.send({ status: 'ok' });
|
||||
});
|
||||
|
||||
router.use(
|
||||
'/api',
|
||||
createProxyMiddleware({
|
||||
target: 'https://api.airbrake.io/api',
|
||||
changeOrigin: true,
|
||||
pathRewrite: path => {
|
||||
const apiKey = options.airbrakeConfig.apiKey;
|
||||
|
||||
let newPath = path.replace(/.+?(\/api)/, '');
|
||||
if (newPath.includes('?')) {
|
||||
newPath += `&key=${apiKey}`;
|
||||
} else {
|
||||
newPath += `?key=${apiKey}`;
|
||||
}
|
||||
return newPath;
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
router.use(errorHandler());
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -14,10 +14,14 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { createServiceBuilder } from '@backstage/backend-common';
|
||||
import {
|
||||
createServiceBuilder,
|
||||
loadBackendConfig,
|
||||
} from '@backstage/backend-common';
|
||||
import { Server } from 'http';
|
||||
import { Logger } from 'winston';
|
||||
import { createRouter } from './router';
|
||||
import { extractAirbrakeConfig } from '../config';
|
||||
|
||||
export interface ServerOptions {
|
||||
port: number;
|
||||
@@ -29,9 +33,12 @@ export async function startStandaloneServer(
|
||||
options: ServerOptions,
|
||||
): Promise<Server> {
|
||||
const logger = options.logger.child({ service: 'airbrake-backend-backend' });
|
||||
const config = await loadBackendConfig({ logger, argv: process.argv });
|
||||
const airbrakeConfig = extractAirbrakeConfig(config);
|
||||
logger.debug('Starting application server...');
|
||||
const router = await createRouter({
|
||||
logger,
|
||||
airbrakeConfig,
|
||||
});
|
||||
|
||||
let service = createServiceBuilder(module)
|
||||
|
||||
Reference in New Issue
Block a user