From d10e8c866bb9b65484b4b3d6f6dd8ded9ce6547e Mon Sep 17 00:00:00 2001 From: Tom Andrade Date: Thu, 26 Jan 2023 11:45:29 +0100 Subject: [PATCH] feat: simplify echo, add handling of json content type, optimize request counter --- main.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index de04404..5a31527 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "io" "log" "net/http" "os" @@ -74,16 +75,26 @@ func serve(l int) bool { // handler generates the echo server response func handler(l int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - jm, err := json.Marshal(r.PostForm) - if err != nil || !serve(l) { + defer C.Add() + if !serve(l) { w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintln(w, "why did you do that?") - C.Add() return } - fmt.Fprintf(w, "%v", string(jm)) - C.Add() + if r.Header.Get("Content-Type") == "application/json" { + v := make(map[string]interface{}) + err := json.NewDecoder(r.Body).Decode(&v) + if err != nil { + http.Error(w, "why did you do that?", http.StatusInternalServerError) + return + } + fmt.Fprintf(w, "%s", v) + return + } + v, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, "Error reading body", http.StatusBadRequest) + } + fmt.Fprintf(w, "%s", v) } }