Move to http-proxy

This commit is contained in:
Romaric Mourgues
2023-05-11 12:18:49 +02:00
parent 56cc84c34e
commit 6a1582a6d4
4 changed files with 42 additions and 77 deletions
@@ -1,9 +1,11 @@
import config from "config";
import httpProxy from "http-proxy";
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";
import httpProxy from "@fastify/http-proxy";
var proxy = httpProxy.createProxyServer({});
@Prefix("/api")
export default class ApplicationsApiService extends TdriveService<undefined> {
@@ -23,11 +25,17 @@ export default class ApplicationsApiService extends TdriveService<undefined> {
const domain = app.internal_domain.replace(/(\/$|^\/)/gm, "");
const prefix = app.external_prefix.replace(/(\/$|^\/)/gm, "");
if (domain && prefix) {
console.log("Listening at ", "/" + prefix + "/*");
fastify.register(httpProxy, {
upstream: domain + "/" + prefix,
prefix: "/" + prefix,
});
try {
fastify.all("/" + prefix + "/*", (req, rep) => {
proxy.web(req.raw, rep.raw, {
target: domain,
});
});
console.log("Listening at ", "/" + prefix + "/*");
} catch (e) {
console.log(e);
console.log("Can't listen to ", "/" + prefix + "/*");
}
}
}