🛠 Try without lib for proxy
* Try without lib for proxy * Tested and approved and using streams * Lint fix
This commit is contained in:
@@ -1,21 +1,11 @@
|
||||
import axios from "axios";
|
||||
import config from "config";
|
||||
import httpProxy from "http-proxy";
|
||||
import _ from "lodash";
|
||||
import { Prefix, TdriveService } from "../../core/platform/framework";
|
||||
import WebServerAPI from "../../core/platform/services/webserver/provider";
|
||||
import Application from "../applications/entities/application";
|
||||
import web from "./web/index";
|
||||
|
||||
const proxy = httpProxy.createProxyServer({});
|
||||
// https://github.com/http-party/node-http-proxy/issues/1471
|
||||
proxy.on("proxyReq", (proxyReq, req: any) => {
|
||||
if (req.body && ["POST", "PATCH", "PUT"].includes(req.method)) {
|
||||
const bodyData = JSON.stringify(req.body);
|
||||
proxyReq.setHeader("content-type", "application/json");
|
||||
proxyReq.setHeader("content-length", Buffer.byteLength(bodyData));
|
||||
proxyReq.write(bodyData);
|
||||
}
|
||||
});
|
||||
|
||||
@Prefix("/api")
|
||||
export default class ApplicationsApiService extends TdriveService<undefined> {
|
||||
version = "1";
|
||||
@@ -35,10 +25,25 @@ export default class ApplicationsApiService extends TdriveService<undefined> {
|
||||
const prefix = app.external_prefix.replace(/(\/$|^\/)/gm, "");
|
||||
if (domain && prefix) {
|
||||
try {
|
||||
fastify.all("/" + prefix + "/*", (req, rep) => {
|
||||
proxy.web(req.raw, rep.raw, {
|
||||
target: domain,
|
||||
});
|
||||
fastify.all("/" + prefix + "/*", async (req, rep) => {
|
||||
console.log("Proxying", req.method, req.url, "to", domain);
|
||||
try {
|
||||
const response = await axios.request({
|
||||
url: domain + req.url,
|
||||
method: req.method as any,
|
||||
headers: _.omit(req.headers, "host", "content-length") as {
|
||||
[key: string]: string;
|
||||
},
|
||||
data: req.body as any,
|
||||
responseType: "stream",
|
||||
});
|
||||
rep.statusCode = response.status;
|
||||
rep.send(response.data);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
rep.raw.statusCode = 500;
|
||||
rep.raw.end(JSON.stringify({ error: err.message }));
|
||||
}
|
||||
});
|
||||
console.log("Listening at ", "/" + prefix + "/*");
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user