import (
"context"
"errors"
"net/http"
"net/http/httputil"
"github.com/gin-gonic/gin"
"github.com/hhk7734/zapx.go"
"go.uber.org/zap"
)
func RouteWithHTTP(scheme string, host string, path string) gin.HandlerFunc {
return func(c *gin.Context) {
rp := &httputil.ReverseProxy{
Director: func(r *http.Request) {
r.URL.Scheme = scheme
r.URL.Host = host
r.Host = host
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
logger := zapx.Ctx(r.Context())
switch {
case errors.Is(err, context.Canceled):
logger.Warn("request canceled")
default:
logger.Error("proxy error", zap.Error(err))
c.Status(http.StatusBadGateway)
}
},
}
rp.ServeHTTP(c.Writer, c.Request)
}
}