Last active
April 3, 2020 17:02
-
-
Save dyaa/e971645b982c54e8116d7d411b0535ef to your computer and use it in GitHub Desktop.
Golang - Proxy route to another backend
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
// Original post: https://github.com/gin-gonic/gin/issues/686#issuecomment-240619920 | |
router.POST("/api/v1/endpoint1", ReverseProxy()) | |
func ReverseProxy() gin.HandlerFunc { | |
target := "localhost:3000" | |
return func(c *gin.Context) { | |
director := func(req *http.Request) { | |
r := c.Request | |
req = r | |
req.URL.Scheme = "http" | |
req.URL.Host = target | |
req.Header["my-header"] = []string{r.Header.Get("my-header")} | |
// Golang camelcases headers | |
delete(req.Header, "My-Header") | |
} | |
proxy := &httputil.ReverseProxy{Director: director} | |
proxy.ServeHTTP(c.Writer, c.Request) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment