From 4223f8e8e165840d203c2f47ef8cb4c05a89d2a9 Mon Sep 17 00:00:00 2001 From: Nash Gadre Date: Thu, 7 Nov 2024 19:20:54 -0500 Subject: [PATCH] fix routing logic --- fast-server/handlers/proxy_handler.go | 29 ++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/fast-server/handlers/proxy_handler.go b/fast-server/handlers/proxy_handler.go index 575191e..ec23f13 100644 --- a/fast-server/handlers/proxy_handler.go +++ b/fast-server/handlers/proxy_handler.go @@ -33,6 +33,7 @@ func getMatchingLocation(path string, locations []config.Location) *config.Locat func HandleProxy(c echo.Context, domain config.Domain) error { requestPath := c.Request().URL.Path + c.Logger().Infof("Original request path: %s", requestPath) // Get matching location location := getMatchingLocation(requestPath, domain.Locations) @@ -41,6 +42,8 @@ func HandleProxy(c echo.Context, domain config.Domain) error { return echo.ErrNotFound } + c.Logger().Infof("Matched location path: %s", location.Path) + // Use location's proxy config proxyConfig := location.Proxy targetScheme := proxyConfig.Protocol @@ -55,8 +58,22 @@ func HandleProxy(c echo.Context, domain config.Domain) error { } originalHost := c.Request().Host - c.Logger().Infof("Proxying %s request from %s to %s (location: %s)", - c.Request().Method, originalHost, target.String(), location.Path) + + // Set up the rewrite rules + var rewriteMap map[string]string + if location.Path == "/" { + rewriteMap = map[string]string{ + "/*": "/$1", + } + } else { + rewriteMap = map[string]string{ + location.Path + "/*": location.Path + "/$1", + } + } + + c.Logger().Infof("Using rewrite map: %v", rewriteMap) + c.Logger().Infof("Proxying %s request from %s to %s%s", + c.Request().Method, originalHost, target.String(), requestPath) transport := &http.Transport{ Proxy: http.ProxyFromEnvironment, @@ -78,6 +95,7 @@ func HandleProxy(c echo.Context, domain config.Domain) error { IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, + ResponseHeaderTimeout: 30 * time.Second, DisableCompression: false, } @@ -86,11 +104,6 @@ func HandleProxy(c echo.Context, domain config.Domain) error { sourceScheme = "https" } - // Strip the location path prefix for the upstream request - rewriteMap := map[string]string{ - location.Path + "*": "/$1", - } - proxyMiddleware := middleware.ProxyWithConfig(middleware.ProxyConfig{ Balancer: middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{ { @@ -100,6 +113,7 @@ func HandleProxy(c echo.Context, domain config.Domain) error { Rewrite: rewriteMap, Transport: transport, ModifyResponse: func(res *http.Response) error { + c.Logger().Infof("Proxy response status: %d for path: %s", res.StatusCode, requestPath) return nil }, }) @@ -109,6 +123,7 @@ func HandleProxy(c echo.Context, domain config.Domain) error { c.Request().Header.Set("X-Real-IP", c.RealIP()) c.Request().Header.Set("X-Forwarded-For", c.RealIP()) c.Request().Header.Set("X-Forwarded-Proto", sourceScheme) + c.Request().Header.Set("X-Original-URI", requestPath) c.Request().Host = originalHost return proxyMiddleware(func(c echo.Context) error {