Skip to content

Instantly share code, notes, and snippets.

@ZeusMode
Created March 27, 2025 16:27
Show Gist options
  • Save ZeusMode/1a077a88edbf78aa7e31ca5dc66db63a to your computer and use it in GitHub Desktop.
Save ZeusMode/1a077a88edbf78aa7e31ca5dc66db63a to your computer and use it in GitHub Desktop.
Gin Proxy
package main
import (
"net/http"
"net/http/httputil"
"net/url"
"github.com/gin-gonic/gin"
)
func proxy(c *gin.Context) {
remote, err := url.Parse("http://localhost:8081")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = c.Param("proxy")
}
proxy.ServeHTTP(c.Writer, c.Request)
}
func formatResponse() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusCreated, gin.H{})
}
}
func main() {
r := gin.Default()
r.Any("/*proxy", proxy, formatResponse())
r.Run(":8080")
}
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.Any("/*proxy", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run(":8081")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment