Documentation
¶
Overview ¶
Package httpx holds tiny echo-boundary helpers shared by both HTTP shells and by handlers. Keep it dependency-light — echo, plus internal/logger and internal/apperror, which import no package from this repository and so cannot close a cycle.
Index ¶
- func AssignRequestID() echo.MiddlewareFunc
- func NewRequestID() string
- func RequestID(c echo.Context) string
- func RequestIDContext(next echo.HandlerFunc) echo.HandlerFunc
- func RequiredParam(c echo.Context, name string) (string, error)
- func RequiredQuery(c echo.Context, name string) (string, error)
- func SafeRequestLogURI(u *url.URL, fallback string) string
- type Binder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssignRequestID ¶ added in v0.22.0
func AssignRequestID() echo.MiddlewareFunc
AssignRequestID is echo's RequestID middleware with NewRequestID as its generator: it adopts an inbound X-Request-ID and assigns one otherwise.
func NewRequestID ¶ added in v0.21.0
func NewRequestID() string
NewRequestID returns a fresh request id. It is a UUID: lowercase, so an id a user copies by hand keeps its case, and hyphenated, so it does not read as a trace id. AssignRequestID uses it for HTTP requests, so the id of a unit of work that did not arrive over HTTP, such as an IM message, has the same form as the id of one that did. Readers treat an id as an opaque string: an inbound X-Request-ID is adopted whatever its form.
func RequestID ¶
RequestID returns the request id assigned by AssignRequestID (response header), falling back to a client-provided header. Empty when neither exists — callers should treat it as optional metadata.
func RequestIDContext ¶ added in v0.21.0
func RequestIDContext(next echo.HandlerFunc) echo.HandlerFunc
RequestIDContext carries the request id into the request's context.Context, so that anything logging during the request reports it.
Without this the id exists only on the echo.Context and the response header. It reaches the client — error responses carry it as request_id — and it reaches the access log line, but nothing a handler or a service logs while serving the request can include it. An id a user quotes back therefore names a request whose actual work cannot be found.
Install it directly after AssignRequestID, which is what assigns the id.
func RequiredParam ¶ added in v0.22.0
RequiredParam returns the path parameter name, trimmed. An empty one is answered as a missing field called name.
func RequiredQuery ¶ added in v0.22.0
RequiredQuery returns the query parameter name, trimmed. An empty one is answered as a missing field called name.
func SafeRequestLogURI ¶ added in v0.21.0
SafeRequestLogURI renders a request URI for an access log line: the path, never the query string.
Some URLs authorise with a token in the query string, so a log line that keeps the query publishes credentials to anything that reads logs. Dropping it for every request rather than for the paths known to carry one means a new such path cannot arrive unnoticed.
This lives here rather than next to one server's middleware because both HTTP shells need it. A sanitizer that only one of them uses is the worst of the three possible states: the tokens still reach a log, and the shell that redacts them suggests otherwise.
Types ¶
type Binder ¶ added in v0.22.0
type Binder struct {
echo.DefaultBinder
}
Binder is echo's DefaultBinder, except that a JSON value of the wrong type is answered as an invalid field named by its key. Both HTTP shells install it, so a handler returns whatever Bind returns. Any other binding failure, such as malformed JSON, stays echo's 400 and is answered without a field.