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 21, 2021
1 parent bc0090f commit 6ae7c73
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 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 @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 6ae7c73

Please sign in to comment.