From 6ae7c73909f1ce3573261c4c102b0b2769747c8a Mon Sep 17 00:00:00 2001 From: "laurenz.honauer" Date: Tue, 21 Sep 2021 16:41:24 +0200 Subject: [PATCH] excel-service: redirect test and prod urls to the corret endpoints --- excel-export-service/src/index.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/excel-export-service/src/index.ts b/excel-export-service/src/index.ts index 0a96e28fb..8389bb4f4 100644 --- a/excel-export-service/src/index.ts +++ b/excel-export-service/src/index.ts @@ -10,7 +10,8 @@ import { config } from "./config"; import { getApiReadiness } from "./api"; const DEFAULT_API_VERSION = "1.0"; -const API_BASE = `http://${config.apiHost}:${config.apiPort}/api`; +const API_BASE_PROD = `http://${config.apiHost}:${config.apiPort}/api`; +const API_BASE_TEST = `http://${config.testApiHost}:${config.testApiPort}/api`; const transformRequest: AxiosTransformer = (data) => { if (typeof data === "object") { @@ -26,16 +27,24 @@ axios.defaults.transformRequest = [transformRequest]; const excelService = express(); excelService.use(express.json()); + excelService.use((req: express.Request, res: express.Response, next) => { res.setHeader("Content-Security-Policy", "default-src 'self'"); next(); }); +// This can be removed once prod and test env option will be removed https://github.com/openkfw/TruBudget/issues/954 +excelService.use((req: express.Request, res: express.Response, next) => { + res.apiBase = req.url.includes("/test") ? API_BASE_TEST : API_BASE_PROD; + req.url = req.url.replace("/test", "").replace("/prod", ""); + next(); +}); + configureJWT(); excelService.get("/readiness", async (req: express.Request, res: express.Response) => { try { - const ready = await getApiReadiness(axios, API_BASE); + const ready = await getApiReadiness(axios, res.apiBase); res.status(200).send(ready); } catch (error) { console.error("API readiness call failed", error); @@ -72,7 +81,7 @@ excelService.get( res.setHeader("Content-Disposition", "attachment; filename=TruBudget_Export.xlsx"); res.setHeader("Transfer-Encoding", "chunked"); - await writeXLSX(axios, req.headers.authorization, res, API_BASE); + await writeXLSX(axios, req.headers.authorization, res, res.apiBase); } catch (error) { console.error(error.message); }