Skip to content

Instantly share code, notes, and snippets.

@cheikhsimsol
Created February 14, 2024 06:06
Show Gist options
  • Save cheikhsimsol/585898cd2ece3a3a355ea6a54b264c94 to your computer and use it in GitHub Desktop.
Save cheikhsimsol/585898cd2ece3a3a355ea6a54b264c94 to your computer and use it in GitHub Desktop.
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