Skip to content

Commit

Permalink
added statistics handler and put handlers under statisticsHandler to …
Browse files Browse the repository at this point in the history
…be able to request most provided request params
  • Loading branch information
Michel Levieux committed Dec 29, 2020
1 parent e211d20 commit 722c4be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 21 additions & 2 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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
}
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 722c4be

Please sign in to comment.