possible ignore case for rest-routes

This commit is contained in:
Joel Schmid 2021-04-02 21:01:22 +02:00
parent 26204b4ed7
commit 791518f3be

View file

@ -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")