Documentation
¶
Overview ¶
Package echo implements an HTTP server that reflects every request back to the caller: method, path, headers, body, TLS details and more. It is meant for debugging proxies, load balancers, ingress controllers, service meshes and HTTP clients.
The handler returned by New can be mounted in any server, or used in-process with net/http/httptest:
srv := httptest.NewServer(echo.New(echo.Options{}))
defer srv.Close()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidatePrefix ¶
ValidatePrefix reports whether p can be used as Options.RoutePrefix: a clean absolute path such as "/_echo", without wildcards, query or fragment characters, spaces or control characters. A trailing slash is ignored.
Types ¶
type Body ¶
type Body struct {
ContentType string `json:"content_type,omitempty"`
// Size is the full body size in bytes, even when Data is truncated.
Size int64 `json:"size"`
// Truncated reports that the body exceeded Options.MaxBodyBytes, so Data
// holds only the first MaxBodyBytes bytes and the body wasn't parsed.
Truncated bool `json:"truncated,omitempty"`
// Encoding is "utf-8" when Data is the body verbatim, "base64" for
// binary payloads, or "omitted" for multipart uploads with files, which
// are summarized in Files instead.
Encoding string `json:"encoding"`
Data string `json:"data"`
JSON json.RawMessage `json:"json,omitempty"`
Form map[string][]string `json:"form,omitempty"`
Files []File `json:"files,omitempty"`
}
Body describes the request payload.
type Echo ¶
type Echo struct {
Method string `json:"method"`
URL string `json:"url"`
Path string `json:"path"`
// Query is parsed from the raw query string, splitting only on "&", so
// values containing ";" are kept rather than dropped.
Query map[string][]string `json:"query,omitempty"`
Proto string `json:"protocol"`
Host string `json:"host"`
RemoteAddr string `json:"remote_addr"`
ClientIP string `json:"client_ip"`
Headers map[string][]string `json:"headers"`
// ContentLength is the declared body length, or -1 when unknown (for
// example a chunked body).
ContentLength int64 `json:"content_length"`
// TransferEncoding lists the transfer codings, such as "chunked". Go
// removes the Transfer-Encoding header, so it is reported here instead.
TransferEncoding []string `json:"transfer_encoding,omitempty"`
// Cookies are in the order the client sent them; names may repeat.
Cookies []Cookie `json:"cookies,omitempty"`
Body *Body `json:"body,omitempty"`
// Trailers are trailing headers sent after a chunked body.
Trailers map[string][]string `json:"trailers,omitempty"`
TLS *TLSInfo `json:"tls,omitempty"`
Server ServerInfo `json:"server"`
RequestID string `json:"request_id"`
Time time.Time `json:"time"`
}
Echo is the reflection of a single request, as returned to the client.
type File ¶
type File struct {
Field string `json:"field"`
Filename string `json:"filename"`
ContentType string `json:"content_type,omitempty"`
Size int64 `json:"size"`
}
File summarizes one uploaded multipart file.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is the echo server. Create one with New.
func New ¶
New returns a ready echo handler. It panics if opts.RoutePrefix is invalid; use ValidatePrefix to check user input first.
func (*Handler) CloseStreams ¶
func (h *Handler) CloseStreams()
CloseStreams ends long-lived responses: WebSocket connections are closed with 1001 (going away), SSE streams end, and pending delays finish early. http.Server.Shutdown does not interrupt active handlers, so call this when shutting down, e.g. with srv.RegisterOnShutdown(h.CloseStreams). It is safe to call more than once.
func (*Handler) HealthPath ¶
HealthPath is the path of the liveness probe, including any route prefix.
type Options ¶
type Options struct {
// MaxBodyBytes caps how much of a request body is read and echoed back.
// Bodies larger than this are truncated in the echo. Defaults to 1 MiB.
MaxBodyBytes int64
// MaxDelay caps the artificial delay a client may request. Defaults to
// 30s. The handler doesn't change your server's write deadline, so keep
// http.Server.WriteTimeout (if set) comfortably above MaxDelay.
MaxDelay time.Duration
// RoutePrefix moves the built-in routes (/healthz, /metrics, /status/...,
// etc.) under a prefix such as "/_echo", so every other path, including
// /healthz, is echoed. Empty means the routes live at the root.
RoutePrefix string
// EnableEnv exposes the server's environment variables at /env. Off by
// default because environments often contain secrets.
EnableEnv bool
// Hostname is reported in responses so callers can tell which instance
// answered. Defaults to os.Hostname().
Hostname string
// Version is reported in responses and metrics.
Version string
// Logger receives access logs. Nil disables access logging.
Logger *slog.Logger
}
Options configures the echo handler. The zero value is usable.
type ServerInfo ¶
ServerInfo identifies the instance that answered.
type TLSInfo ¶
type TLSInfo struct {
Version string `json:"version"`
CipherSuite string `json:"cipher_suite"`
ServerName string `json:"server_name,omitempty"`
NegotiatedProtocol string `json:"alpn,omitempty"`
Resumed bool `json:"resumed"`
PeerCertificates []string `json:"peer_certificates,omitempty"`
}
TLSInfo describes the TLS connection, when there is one.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
echo-server
command
Command echo-server runs an HTTP server that echoes requests back.
|
Command echo-server runs an HTTP server that echoes requests back. |