CLI
{{.Value}}
Endpoints
{{.Path}}
{{.Description}}
{{end}}
package httpapi import ( "html/template" "net/http" "strings" ) var homeTemplate = template.Must(template.New("home").Parse(homePageHTML)) type homePageData struct { Info ClientInfo BaseURL string Commands []commandLink Endpoints []endpointLink } type commandLink struct { Label string Value string } type endpointLink struct { Path string Description string } func (s *Server) handleHome(w http.ResponseWriter, req *http.Request) { baseURL := s.requestBaseURL(req) data := homePageData{ Info: s.clientInfo(req), BaseURL: baseURL, Commands: []commandLink{ {Label: "IP", Value: "curl " + baseURL + "/ip"}, {Label: "JSON", Value: "curl " + baseURL + "/json"}, {Label: "Headers", Value: "curl " + baseURL + "/headers"}, }, Endpoints: []endpointLink{ {Path: "/", Description: "Plain IP for CLI, dashboard for browsers"}, {Path: "/ip", Description: "Plain text client IP"}, {Path: "/json", Description: "Request metadata as JSON"}, {Path: "/headers", Description: "Request headers as text"}, {Path: "/user-agent", Description: "User agent as text"}, {Path: "/healthz", Description: "Health check"}, }, } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) _ = homeTemplate.Execute(w, data) } func (s *Server) requestBaseURL(req *http.Request) string { scheme := "http" if req.TLS != nil { scheme = "https" } host := req.Host if host == "" { host = "localhost" } if s.resolver.TrustsProxyHeaders(req) { if proto := firstCommaSeparatedHeader(req.Header.Get("X-Forwarded-Proto")); proto == "http" || proto == "https" { scheme = proto } if forwardedHost := firstCommaSeparatedHeader(req.Header.Get("X-Forwarded-Host")); forwardedHost != "" { host = forwardedHost } } return scheme + "://" + host } func firstCommaSeparatedHeader(value string) string { value = strings.TrimSpace(value) if value == "" { return "" } value, _, _ = strings.Cut(value, ",") return strings.TrimSpace(value) } const homePageHTML = `
{{.Value}}
{{.Path}}
{{.Description}}
{{end}}