mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
74 lines
1.3 KiB
Go
74 lines
1.3 KiB
Go
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 ""
|
|
}
|