Skip to content

Commit

Permalink
excel-service: redirect test and prod urls to the corret endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenzhonauer committed Sep 22, 2021
1 parent 9d8982d commit 0a97cee
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions excel-export-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -32,11 +33,18 @@ excelService.use((req: express.Request, res: express.Response, next) => {
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);
Expand Down Expand Up @@ -70,7 +78,7 @@ excelService.get("/download", async (req: express.Request, res: express.Response
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);
}
Expand Down

0 comments on commit 0a97cee

Please sign in to comment.