Skip to content

Instantly share code, notes, and snippets.

@dyaa
Last active April 3, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyaa/e971645b982c54e8116d7d411b0535ef to your computer and use it in GitHub Desktop.
Save dyaa/e971645b982c54e8116d7d411b0535ef to your computer and use it in GitHub Desktop.
Golang - Proxy route to another backend
// 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