Created
March 27, 2025 16:27
-
-
Save ZeusMode/1a077a88edbf78aa7e31ca5dc66db63a to your computer and use it in GitHub Desktop.
Gin Proxy
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 ( | |
"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") | |
} |
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 ( | |
"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