mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:48:49 +02:00
fix: middleware bypass
This commit is contained in:
74
internal/route/routes/context.go
Normal file
74
internal/route/routes/context.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type RouteContext struct{}
|
||||
|
||||
var routeContextKey = RouteContext{}
|
||||
|
||||
func WithRouteContext(r *http.Request, route HTTPRoute) *http.Request {
|
||||
return r.WithContext(context.WithValue(r.Context(), routeContextKey, route))
|
||||
}
|
||||
|
||||
func TryGetRoute(r *http.Request) HTTPRoute {
|
||||
if route, ok := r.Context().Value(routeContextKey).(HTTPRoute); ok {
|
||||
return route
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tryGetURL(r *http.Request) *url.URL {
|
||||
if route := TryGetRoute(r); route != nil {
|
||||
u := route.TargetURL()
|
||||
if u != nil {
|
||||
return &u.URL
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TryGetUpstreamName(r *http.Request) string {
|
||||
if route := TryGetRoute(r); route != nil {
|
||||
return route.Name()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TryGetUpstreamScheme(r *http.Request) string {
|
||||
if u := tryGetURL(r); u != nil {
|
||||
return u.Scheme
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TryGetUpstreamHost(r *http.Request) string {
|
||||
if u := tryGetURL(r); u != nil {
|
||||
return u.Hostname()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TryGetUpstreamPort(r *http.Request) string {
|
||||
if u := tryGetURL(r); u != nil {
|
||||
return u.Port()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TryGetUpstreamAddr(r *http.Request) string {
|
||||
if u := tryGetURL(r); u != nil {
|
||||
return u.Host
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TryGetUpstreamURL(r *http.Request) string {
|
||||
if u := tryGetURL(r); u != nil {
|
||||
return u.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user