Browse Source
Enhance router pipeline route matching
feature/update-route-registration
Roman Zipp
3 years ago
committed by
Fabian Vowie
No known key found for this signature in database
GPG Key ID: C27317C33B27C410
2 changed files with
12 additions and
8 deletions
-
main.go
-
main_test.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) |
|
|
|
|
|
|
|
r.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) { |
|
|
|
for _, pipeline := range pipelines { |
|
|
|
r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { |
|
|
|
if pipeline.GetSlug() == mux.Vars(r)["pipeline"] { |
|
|
|
PipelineHandler(pipeline, storageProvider, w, r) |
|
|
|
}) |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
w.WriteHeader(404) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
func main() { |
|
|
|
|
|
@ -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) |
|
|
|