From 722c4be4960163c5f8a3d19144819c18ee9ef3a1 Mon Sep 17 00:00:00 2001 From: Michel Levieux Date: Tue, 29 Dec 2020 23:35:06 +0100 Subject: [PATCH] added statistics handler and put handlers under statisticsHandler to be able to request most provided request params --- handle.go | 23 +++++++++++++++++++++-- main.go | 5 ++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/handle.go b/handle.go index 678d12c..8356e19 100644 --- a/handle.go +++ b/handle.go @@ -7,10 +7,11 @@ import ( ) var ( - _ http.HandlerFunc = handleFizzBuzz + _ http.HandlerFunc = (&statisticsHandler{}).handleFizzBuzz + _ http.HandlerFunc = (&statisticsHandler{}).handleStatistics ) -func handleFizzBuzz(w http.ResponseWriter, r *http.Request) { +func (sh *statisticsHandler) handleFizzBuzz(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: @@ -36,6 +37,7 @@ func handleFizzBuzz(w http.ResponseWriter, r *http.Request) { return } + sh.newCall(transformQuery(int(d1), int(d2), int(limit), s1, s2)) _, err = w.Write([]byte(fizzBuzz(int(d1), int(d2), int(limit), s1, s2))) if err != nil { http.Error(w, "internal server error", http.StatusInternalServerError) @@ -46,4 +48,21 @@ func handleFizzBuzz(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("unsupported method: %s", http.MethodPost), http.StatusMethodNotAllowed) return } +} + +func (sh *statisticsHandler) handleStatistics(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + + d1, d2, limit, s1, s2 := getQuery(sh.most()) + + _, err := w.Write([]byte(fmt.Sprintf("int1=%d ; int2=%d ; limit=%d ; s1 = %s ; s2 = %s", d1, d2, limit, s1, s2))) + if err != nil { + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } + default: + http.Error(w, fmt.Sprintf("unsupported method: %s", http.MethodPost), http.StatusMethodNotAllowed) + return + } } \ No newline at end of file diff --git a/main.go b/main.go index f426ff2..cb8d6b5 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,11 @@ import ( func main() { + sh := newStatistics() + r := mux.NewRouter() - r.HandleFunc("/fizzbuzz", handleFizzBuzz) + r.HandleFunc("/fizzbuzz", sh.handleFizzBuzz) + r.HandleFunc("/statistics", sh.handleStatistics) log.Fatalln(http.ListenAndServe(":8080", r)) } \ No newline at end of file