From 94a66fac2ab4320a1a0b84e2b06b3a67fdcc55c8 Mon Sep 17 00:00:00 2001 From: Trong Huu Nguyen Date: Tue, 21 Feb 2023 14:45:14 +0100 Subject: [PATCH] refactor(handler): extract path matcher for reuse --- pkg/handler/handler.go | 7 +------ pkg/handler/path.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 pkg/handler/path.go diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index e1c9cbe..911ceae 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -126,12 +126,7 @@ func (s *Standalone) GetIngresses() *ingress.Ingresses { } func (s *Standalone) GetPath(r *http.Request) string { - path, ok := mw.PathFrom(r.Context()) - if !ok { - path = s.Ingresses.MatchingPath(r) - } - - return path + return GetPath(r, s) } func (s *Standalone) GetRedirect() url.Redirect { diff --git a/pkg/handler/path.go b/pkg/handler/path.go new file mode 100644 index 0000000..52b40c8 --- /dev/null +++ b/pkg/handler/path.go @@ -0,0 +1,23 @@ +package handler + +import ( + "net/http" + + "github.com/nais/wonderwall/pkg/ingress" + mw "github.com/nais/wonderwall/pkg/middleware" +) + +type PathSource interface { + GetIngresses() *ingress.Ingresses +} + +// GetPath returns the matching context path from the list of registered ingresses. +// If none match, an empty string is returned. +func GetPath(r *http.Request, src PathSource) string { + path, ok := mw.PathFrom(r.Context()) + if !ok { + path = src.GetIngresses().MatchingPath(r) + } + + return path +}