Browse Source

Enhance router pipeline route matching

feature/update-route-registration
Roman Zipp 2 years ago
committed by Fabian Vowie
parent
commit
ed716435ac
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 16
      main.go
  2. 4
      main_test.go

16
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() {

4
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)

Loading…
Cancel
Save