Browse Source

Enhance router pipeline route matching

feature/add-http-image-upload
Roman Zipp 3 years ago
parent
commit
562462fbd7
  1. 10
      main.go
  2. 4
      main_test.go

10
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) { func RegisterRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
r.HandleFunc("/", IndexHandler) r.HandleFunc("/", IndexHandler)
r.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) {
for _, pipeline := range pipelines { 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) PipelineHandler(pipeline, storageProvider, w, r)
})
return
}
} }
w.WriteHeader(404)
})
} }
func main() { func main() {

4
main_test.go

@ -36,7 +36,7 @@ func TestEndpointRoute(t *testing.T) {
RegisterRoutes(router, []pipelines.IPipeline{data}, fs) RegisterRoutes(router, []pipelines.IPipeline{data}, fs)
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder() responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request) router.ServeHTTP(responseRecorder, request)
@ -49,7 +49,7 @@ func TestEndpointRoute(t *testing.T) {
t.Run("Unregistered pipelines return 404", func(t *testing.T) { t.Run("Unregistered pipelines return 404", func(t *testing.T) {
router := mux.NewRouter() router := mux.NewRouter()
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder() responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request) router.ServeHTTP(responseRecorder, request)

Loading…
Cancel
Save