From 791518f3be6a3263684b70acbb8fb5f689d5ff22 Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Fri, 2 Apr 2021 21:01:22 +0200 Subject: [PATCH] possible ignore case for rest-routes --- api/rest-api.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/api/rest-api.go b/api/rest-api.go index 8d68066..53596e9 100644 --- a/api/rest-api.go +++ b/api/rest-api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "weather-data/storage" "weather-data/weathersource" @@ -29,7 +30,8 @@ func NewRestAPI(connection string, weatherStorage storage.WeatherStorage, sensor //Start a new Rest-API instance func (api *weatherRestApi) Start() error { - return http.ListenAndServe(api.connection, api.handleRequests()) + handler := api.handleRequests() + return http.ListenAndServe(api.connection, handler) // http.ListenAndServe(api.connection, handler) } func (api *weatherRestApi) Close() { @@ -37,7 +39,7 @@ func (api *weatherRestApi) Close() { func (api *weatherRestApi) handleRequests() *mux.Router { - router := mux.NewRouter().StrictSlash(true) + router := mux.NewRouter() router.HandleFunc("/", api.homePageHandler) router.HandleFunc("/random", api.randomWeatherHandler) @@ -48,6 +50,13 @@ func (api *weatherRestApi) handleRequests() *mux.Router { return router } +func caselessMatcher(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.URL.Path = strings.ToLower(r.URL.Path) + next.ServeHTTP(w, r) + }) +} + func (api *weatherRestApi) getData(w http.ResponseWriter, r *http.Request) { w.Header().Add("content-type", "application/json")