22032025-1
This commit is contained in:
parent
0b5421edc2
commit
5ef25782fe
1 changed files with 48 additions and 16 deletions
|
|
@ -9,22 +9,35 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReverseProxy struct {
|
type ReverseProxy struct {
|
||||||
context.Context
|
CtxKey CtxKey
|
||||||
|
Context context.Context
|
||||||
*httputil.ReverseProxy
|
*httputil.ReverseProxy
|
||||||
}
|
}
|
||||||
|
|
||||||
type CtxKey string
|
type CtxKey string
|
||||||
|
|
||||||
func New(ctx context.Context, host string) *ReverseProxy {
|
func NewSTD(ctx context.Context, host string) *ReverseProxy {
|
||||||
|
|
||||||
ctxKey := CtxKey("host")
|
ctxKey := CtxKey("host")
|
||||||
ctx = context.WithValue(ctx, ctxKey, host)
|
ctx = context.WithValue(ctx, ctxKey, host)
|
||||||
|
|
||||||
reverseProxySTDLIB := &httputil.ReverseProxy{
|
reverseProxySTDLIB := &httputil.ReverseProxy{}
|
||||||
|
reverseProxy := &ReverseProxy{}
|
||||||
|
reverseProxy.Context = ctx
|
||||||
|
reverseProxy.ReverseProxy = reverseProxySTDLIB
|
||||||
|
return reverseProxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(ctx context.Context, host string) *ReverseProxy {
|
||||||
|
rp := &ReverseProxy{}
|
||||||
|
rp.CtxKey = CtxKey("host")
|
||||||
|
rp.Context = context.WithValue(ctx, rp.CtxKey, host)
|
||||||
|
target, _ := url.Parse(host)
|
||||||
|
rp.ReverseProxy = &httputil.ReverseProxy{
|
||||||
Director: func(r *http.Request) {
|
Director: func(r *http.Request) {
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(rp.Context)
|
||||||
hostFromCtx := ctx.Value(ctxKey).(string)
|
// hostFromCtx := ctx.Value(rp.CtxKey).(string)
|
||||||
target, _ := url.Parse(hostFromCtx)
|
|
||||||
targetQuery := target.RawQuery
|
targetQuery := target.RawQuery
|
||||||
r.URL.Scheme = target.Scheme
|
r.URL.Scheme = target.Scheme
|
||||||
r.URL.Host = target.Host
|
r.URL.Host = target.Host
|
||||||
|
|
@ -36,10 +49,8 @@ func New(ctx context.Context, host string) *ReverseProxy {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
reverseProxy := &ReverseProxy{}
|
|
||||||
reverseProxy.Context = ctx
|
return rp
|
||||||
reverseProxy.ReverseProxy = reverseProxySTDLIB
|
|
||||||
return reverseProxy
|
|
||||||
}
|
}
|
||||||
func (revereProxy *ReverseProxy) SetContext(ctx context.Context) {
|
func (revereProxy *ReverseProxy) SetContext(ctx context.Context) {
|
||||||
revereProxy.Context = ctx
|
revereProxy.Context = ctx
|
||||||
|
|
@ -87,11 +98,32 @@ type JoinURLPathFunc func(*url.URL, *url.URL) (string, string)
|
||||||
// type SingleJoiningSlashFunc func(string, string) string
|
// type SingleJoiningSlashFunc func(string, string) string
|
||||||
type DirectorFunc func(*http.Request)
|
type DirectorFunc func(*http.Request)
|
||||||
|
|
||||||
func StripPrefix(r *http.Request, prefix string) *http.Request {
|
// func StripPrefix(r *http.Request, prefixPath string) *http.Request {
|
||||||
newPath := strings.TrimPrefix(r.URL.Path, prefix)
|
// if prefixPath == "/" {
|
||||||
if newPath == "" {
|
// newPath := strings.TrimPrefix(r.URL.Path, prefixPath)
|
||||||
newPath = "/"
|
// if newPath == "" {
|
||||||
|
// newPath = "/"
|
||||||
|
// }
|
||||||
|
// r.URL.Path = newPath
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return r
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (rp *ReverseProxy) DefaultDirectorFunc(ctx context.Context, ctxKey CtxKey, fn func(*http.Request) *http.Request) {
|
||||||
|
rp.Director = func(r *http.Request) {
|
||||||
|
r = fn(r)
|
||||||
|
r = r.WithContext(rp.Context)
|
||||||
|
hostFromCtx := ctx.Value(CtxKey("host")).(string)
|
||||||
|
target, _ := url.Parse(hostFromCtx)
|
||||||
|
targetQuery := target.RawQuery
|
||||||
|
r.URL.Scheme = target.Scheme
|
||||||
|
r.URL.Host = target.Host
|
||||||
|
r.URL.Path, r.URL.RawPath = JoinURLPath(target, r.URL)
|
||||||
|
if targetQuery == "" || r.URL.RawQuery == "" {
|
||||||
|
r.URL.RawQuery = targetQuery + r.URL.RawQuery
|
||||||
|
} else {
|
||||||
|
r.URL.RawQuery = targetQuery + "&" + r.URL.RawQuery
|
||||||
|
}
|
||||||
}
|
}
|
||||||
r.URL.Path = newPath
|
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue