From 81abd30c9067c7d5cb61134dd773d09c3146de4c Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Wed, 26 Jan 2022 17:38:15 +0100 Subject: [PATCH] Use subrouter for different route sections --- main.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index b05c644..86f4ea3 100644 --- a/main.go +++ b/main.go @@ -131,11 +131,16 @@ func UploadHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPi } func RegisterRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) { - r.HandleFunc("/", IndexHandler).Methods("GET") - r.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { + index := r.Methods(http.MethodGet).Subrouter() + index.HandleFunc("/", IndexHandler) + + upload := r.Methods(http.MethodPost).Subrouter() + upload.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { UploadHandler(w, r, pipelines, storageProvider) - }).Methods("POST") - r.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) { + }) + + pipeline := r.Methods(http.MethodGet).Subrouter() + pipeline.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) @@ -144,7 +149,7 @@ func RegisterRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvi } w.WriteHeader(404) - }).Methods("GET") + }) } func main() {