diff --git a/src/main.go b/src/main.go index d0599ea..3152a98 100644 --- a/src/main.go +++ b/src/main.go @@ -1,6 +1,7 @@ package main import ( + "embed" "fmt" "html/template" "io" @@ -8,57 +9,44 @@ import ( "net/http" ) +//go:embed templates/*.html +var templates embed.FS +var echoTemplate = template.Must( + template.ParseFS(templates, "templates/echo.html"), +) + type EchoData struct { - Method string - URL string - Headers map[string][]string - Body string + Method string + URL string + Path string + Query string + Host string + Proto string + RemoteAddr string + Headers map[string][]string + Body string + TLS bool } -var echoTemplate = template.Must(template.New("echo").Parse(` - - - - -Echo Server - - - -

{{.Method}} {{.URL}}

- -

Headers:

- -{{- range $name, $values := .Headers}} -
{{$name}}
-
{{- range $_, $v := $values}} {{$v}}
-{{- end}}
- -{{- end}} - -

Body:

-
-{{.Body}}
-
- - -`)) - func echoHandler(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() 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), + Method: r.Method, + URL: r.URL.String(), + Path: r.URL.Path, + Query: r.URL.RawQuery, + Host: r.Host, + RemoteAddr: r.RemoteAddr, + Proto: r.Proto, + Headers: r.Header, + Body: string(bodyBytes), + TLS: r.TLS != nil, } w.Header().Set("Content-Type", "text/html; charset=utf-8") diff --git a/src/templates/echo.html b/src/templates/echo.html new file mode 100644 index 0000000..c7ad38a --- /dev/null +++ b/src/templates/echo.html @@ -0,0 +1,243 @@ + + + + + +Echo Server + + + + + + +
+ +

Echo Server

+
{{.Method}} {{.URL}}
+ +
+ +
+

Last Hop Connection

+ + + + + + + + + + + + + +
Remote Address{{.RemoteAddr}}
TLS{{.TLS}}
Protocol{{.Proto}}
+
+ + +
+

Request

+ + + + + + + + + + + + + + + + + + + + + +
Method{{.Method}}
Host{{.Host}}
Path{{.Path}}
Query{{.Query}}
URL{{.URL}}
+
+ +
+ + +
+

Forwarding Headers

+ + + + + + + + + + + + + + + + + + + + + +
X-Forwarded-For{{index .Headers "X-Forwarded-For"}}
X-Real-IP{{index .Headers "X-Real-IP"}}
X-Forwarded-Proto{{index .Headers "X-Forwarded-Proto"}}
X-Forwarded-Host{{index .Headers "X-Forwarded-Host"}}
Forwarded{{index .Headers "Forwarded"}}
+
+ + +
+

Headers

+ + +{{range $name, $values := .Headers}} + + + + +{{end}} +
{{$name}} + {{range $values}} +
{{.}}
+ {{end}} +
+ +
+ + +
+

Body

+
{{.Body}}
+
+ + +
+ + \ No newline at end of file