From ed716435aca3145bf926e6876d3bc6bf54f912e0 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sun, 23 Jan 2022 17:14:43 +0100 Subject: [PATCH] Enhance router pipeline route matching --- main.go | 16 ++++++++++------ main_test.go | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 16ed52a..2f7df81 100644 --- a/main.go +++ b/main.go @@ -38,12 +38,16 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { func RegisterRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) { r.HandleFunc("/", IndexHandler) - - for _, pipeline := range pipelines { - r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { - PipelineHandler(pipeline, storageProvider, w, r) - }) - } + r.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) { + for _, pipeline := range pipelines { + if pipeline.GetSlug() == mux.Vars(r)["pipeline"] { + PipelineHandler(pipeline, storageProvider, w, r) + return + } + } + + w.WriteHeader(404) + }) } func main() { diff --git a/main_test.go b/main_test.go index cd2b0c1..e97f4ef 100644 --- a/main_test.go +++ b/main_test.go @@ -36,7 +36,7 @@ func TestEndpointRoute(t *testing.T) { RegisterRoutes(router, []pipelines.IPipeline{data}, fs) - request, _ := http.NewRequest("GET", "/"+data.Slug, nil) + request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil) responseRecorder := httptest.NewRecorder() router.ServeHTTP(responseRecorder, request) @@ -49,7 +49,7 @@ func TestEndpointRoute(t *testing.T) { t.Run("Unregistered pipelines return 404", func(t *testing.T) { router := mux.NewRouter() - request, _ := http.NewRequest("GET", "/"+data.Slug, nil) + request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil) responseRecorder := httptest.NewRecorder() router.ServeHTTP(responseRecorder, request)