mirror of
https://github.com/nais/wonderwall.git
synced 2026-05-13 03:47:02 +00:00
38 lines
742 B
Go
38 lines
742 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/nais/wonderwall/pkg/ingress"
|
|
)
|
|
|
|
type IngressSource interface {
|
|
GetIngresses() *ingress.Ingresses
|
|
}
|
|
|
|
type IngressMiddleware struct {
|
|
IngressSource
|
|
}
|
|
|
|
func Ingress(source IngressSource) IngressMiddleware {
|
|
return IngressMiddleware{IngressSource: source}
|
|
}
|
|
|
|
func (i *IngressMiddleware) Handler(next http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
ingresses := i.GetIngresses()
|
|
ctx := r.Context()
|
|
|
|
path := ingresses.MatchingPath(r)
|
|
ctx = WithPath(ctx, path)
|
|
|
|
matchingIngress, ok := ingresses.MatchingIngress(r)
|
|
if ok {
|
|
ctx = WithIngress(ctx, matchingIngress)
|
|
}
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
}
|
|
return http.HandlerFunc(fn)
|
|
}
|