Created
February 14, 2024 06:06
-
-
Save cheikhsimsol/585898cd2ece3a3a355ea6a54b264c94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
"github.com/gin-gonic/gin" | |
) | |
func main() { | |
router := gin.Default() | |
// Define the target URL where the requests will be forwarded | |
target, _ := url.Parse("http://localhost:8081/base/") | |
// Create a reverse proxy | |
proxy := &httputil.ReverseProxy{ | |
Rewrite: func(r *httputil.ProxyRequest) { | |
log.Println("Forwarding:", r.In.URL.String()) | |
r.SetURL(target) | |
}, | |
} | |
// Middleware to modify the request URL before forwarding | |
router.Use(func(c *gin.Context) { | |
// Forward the request to the target server | |
proxy.ServeHTTP(c.Writer, c.Request) | |
}) | |
// start the second server | |
go launchSecondServer() | |
// Start the Gin server | |
router.Run(":8080") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment