import querystring from "querystring"; import express from "express"; import bodyParser from "body-parser"; import axios, { ResponseType } from "axios"; import { bid, ogury } from "../../src/protos/protos"; const HOSTS = { local: "http://127.0.0.1:8081", devc: "http://ms-auction-euw1a.devc.cloud.ogury.io", staging: "http://ms-auction-euw1a.staging.cloud.ogury.io", prod: "http://ms-auction-euw1a.prod.cloud.ogury.io", }; type Transformer = { encode: (x) => Uint8Array; decode: (x) => any; reqType?: string; resType?: ResponseType; }; const bodyTransformers: { [k: string]: Transformer } = { default: { encode: (x) => x, decode: (x) => x, }, "/v1/hv": { encode: (x) => bid.BidAgentRequest.encode(x).finish(), decode: (x) => ogury.VastCacheResponse.decode(x), reqType: "application/protobuf", resType: "arraybuffer", }, }; const app = express(); app.set("etag",false); app.use(bodyParser.json() as any); app.use(async (req, res, next) => { try { const host = HOSTS[process.env.ENV || "devc"]; let url = `${host}${req.path}`; const qs = querystring.stringify(req.query as any); if (qs) { url += `?${qs}`; } const transformer = bodyTransformers[req.path] || bodyTransformers.default; const reqData = transformer.encode(req.body); const response = await axios({ method: req.method as any, url, data: reqData, headers: { "Content-Type": transformer.reqType || "application/json", }, responseType: (transformer.resType as any) || "json", }); const resData = transformer.decode(response.data); res.set("Content-Type", "application/json"); res.status(response.status); res.send(resData); } catch (err: any) { if (!err.response) { next(err); return; } res.set("Content-Type", "application/json"); res.status(err.response.status); res.send(err.response.data); } }); app.listen(process.env.PORT || 8080); console.log("Listening...");