This commit is contained in:
Zeev Diukman 2025-03-07 07:30:44 +00:00
parent 9882ad3edb
commit e554570004

View file

@ -9,15 +9,18 @@ import (
) )
type ReverseProxy struct { type ReverseProxy struct {
context.Context
*httputil.ReverseProxy *httputil.ReverseProxy
} }
type CtxKey string type CtxKey string
func New(ctx context.Context, host string) *ReverseProxy { func New(ctx context.Context, host string) *ReverseProxy {
ctxKey := CtxKey("host") ctxKey := CtxKey("host")
ctx = context.WithValue(ctx, ctxKey, host) ctx = context.WithValue(ctx, ctxKey, host)
reverseproxy := &httputil.ReverseProxy{
reverseProxySTDLIB := &httputil.ReverseProxy{
Director: func(r *http.Request) { Director: func(r *http.Request) {
r = r.WithContext(ctx) r = r.WithContext(ctx)
hostFromCtx := ctx.Value(ctxKey).(string) hostFromCtx := ctx.Value(ctxKey).(string)
@ -33,7 +36,17 @@ func New(ctx context.Context, host string) *ReverseProxy {
} }
}, },
} }
return &ReverseProxy{reverseproxy} reverseProxy := &ReverseProxy{}
reverseProxy.Context = ctx
reverseProxy.ReverseProxy = reverseProxySTDLIB
return reverseProxy
}
func (revereProxy *ReverseProxy) SetContext(ctx context.Context) {
revereProxy.Context = ctx
}
func (revereProxy *ReverseProxy) DirectorFunc(df func(jup JoinURLPathFunc) DirectorFunc) {
d := df(joinURLPath)
revereProxy.Director = d
} }
func singleJoiningSlash(a, b string) string { func singleJoiningSlash(a, b string) string {
@ -68,3 +81,8 @@ func joinURLPath(a, b *url.URL) (path, rawpath string) {
} }
return a.Path + b.Path, apath + bpath return a.Path + b.Path, apath + bpath
} }
type JoinURLPathFunc func(*url.URL, *url.URL) (string, string)
// type SingleJoiningSlashFunc func(string, string) string
type DirectorFunc func(*http.Request)