Files
flatrender/services/gateway/internal/proxy/proxy.go
T

65 lines
1.8 KiB
Go
Raw Normal View History

package proxy
import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/gin-gonic/gin"
)
// Upstream holds a reverse proxy for one backend service.
type Upstream struct {
Name string
rp *httputil.ReverseProxy
}
// New creates an Upstream reverse proxy pointing at target (e.g. "http://localhost:5010").
func New(name, target string) *Upstream {
u, err := url.Parse(target)
if err != nil {
panic("invalid upstream URL for " + name + ": " + err.Error())
}
rp := httputil.NewSingleHostReverseProxy(u)
rp.Transport = &http.Transport{
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
DisableCompression: false,
}
// Custom error handler so we return JSON rather than Go's default HTML
rp.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte(`{"code":"bad_gateway","message":"upstream unavailable"}`))
}
// Rewrite the request before forwarding
orig := rp.Director
rp.Director = func(req *http.Request) {
orig(req)
req.Header.Set("X-Forwarded-Host", req.Host)
req.Header.Del("X-Forwarded-For") // let httputil re-add it properly
req.Host = u.Host
}
return &Upstream{Name: name, rp: rp}
}
// Handler returns a gin.HandlerFunc that proxies the request.
func (up *Upstream) Handler() gin.HandlerFunc {
return func(c *gin.Context) {
up.rp.ServeHTTP(c.Writer, c.Request)
}
}
// StripPrefixHandler proxies after stripping a URL prefix (e.g. "/api" → "").
func (up *Upstream) StripPrefixHandler(prefix string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, prefix)
if c.Request.URL.Path == "" {
c.Request.URL.Path = "/"
}
up.rp.ServeHTTP(c.Writer, c.Request)
}
}