diff --git a/src/main.go b/src/main.go index 93276f2..50f2cd7 100644 --- a/src/main.go +++ b/src/main.go @@ -2,22 +2,69 @@ package main import ( "fmt" + "html/template" + "io" "log" "net/http" ) -func echoHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "%s %s\n", r.Method, r.URL.String()) +type EchoData struct { + Method string + URL string + Headers map[string][]string + Body string +} - fmt.Fprintln(w, "Headers:") - for name, values := range r.Header { - for _, value := range values { - fmt.Fprintf(w, "%s: %s\n", name, value) - } +var echoTemplate = template.Must(template.New("echo").Parse(` + + +
+ +{{- range, $_, $v := $values}} {{$v}}
+{{- end}}
+
+{{- end}}
+
+
+{{.Body}}
+
+
+
+`))
+
+func echoHandler(w http.ResponseWriter, r *http.Request) {
+ bodyBytes, err := io.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, "Failed to read body: "+err.Error(), http.StatusBadRequest)
+ return
+ }
+ r.Body.Close()
+ data := EchoData{
+ Method: r.Method,
+ URL: r.URL.String(),
+ Headers: r.Header,
+ Body: string(bodyBytes),
}
- fmt.Fprintln(w, "\nBody:")
- r.Body.Close()
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ if err := echoTemplate.Execute(w, data); err != nil {
+ http.Error(w, fmt.Sprintf("Template error: %v", err), http.StatusInternalServerError)
+ }
}
func main() {