Documentation
¶
Overview ¶
Package http provides shared server wiring and middleware helpers.
Index ¶
- Constants
- func CoarsePathGroup(path string) string
- func RequestIDMiddleware(gen *diag.TraceIDGenerator, next http.Handler) http.Handler
- func RequestIDMiddlewareHeaders(gen *diag.TraceIDGenerator, names []string, next http.Handler) http.Handler
- func SecurityHeadersMiddleware(next http.Handler) http.Handler
- func TraceMiddleware(next http.Handler) http.Handler
- func TraceMiddlewareHeaders(names []string, next http.Handler) http.Handler
- type ResponseStatusRecorder
- func (rr *ResponseStatusRecorder) Flush()
- func (rr *ResponseStatusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (rr *ResponseStatusRecorder) Push(target string, opts *nethttp.PushOptions) error
- func (rr *ResponseStatusRecorder) ReadFrom(r io.Reader) (int64, error)
- func (rr *ResponseStatusRecorder) Write(b []byte) (int, error)
- func (rr *ResponseStatusRecorder) WriteHeader(code int)
Examples ¶
Constants ¶
const HeaderTraceID = lipsdk.HeaderTraceID
HeaderTraceID is the default inbound/outbound trace header.
Variables ¶
This section is empty.
Functions ¶
func CoarsePathGroup ¶
CoarsePathGroup returns the first URL path segment as a bounded route family (e.g. "/v1/foo" -> "/v1", "/" -> "/"). Used for metrics, tracing span names, and access logs.
Example ¶
fmt.Println(CoarsePathGroup("/v1/models/list"))
Output: /v1
func RequestIDMiddleware ¶
RequestIDMiddleware generates and injects a new trace ID when one is not already present in the request context. gen must be non-nil (typically diag.NewTraceIDGenerator from the composition root).
func RequestIDMiddlewareHeaders ¶
func RequestIDMiddlewareHeaders(gen *diag.TraceIDGenerator, names []string, next http.Handler) http.Handler
RequestIDMiddlewareHeaders is RequestIDMiddleware and echoes the trace ID on each configured outbound header name (defaults to HeaderTraceID).
func SecurityHeadersMiddleware ¶
SecurityHeadersMiddleware adds standard security headers to every response.
func TraceMiddleware ¶
TraceMiddleware injects a trace ID into the request context when a configured inbound trace header is present.
func TraceMiddlewareHeaders ¶
TraceMiddlewareHeaders is TraceMiddleware with explicit inbound header names. Empty names use HeaderTraceID.
Types ¶
type ResponseStatusRecorder ¶
type ResponseStatusRecorder struct {
nethttp.ResponseWriter
Status int
}
ResponseStatusRecorder wraps net/http.ResponseWriter and records the first status passed to WriteHeader. It forwards optional net/http.ResponseWriter behaviors (Flusher, Hijacker, Pusher, io.ReaderFrom) so streaming middleware preserves flush and HTTP/2 push support.
func (*ResponseStatusRecorder) Flush ¶
func (rr *ResponseStatusRecorder) Flush()
Flush forwards to the underlying net/http.Flusher when supported; otherwise it is a no-op.
func (*ResponseStatusRecorder) Hijack ¶
func (rr *ResponseStatusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack forwards to the underlying net/http.Hijacker when supported.
func (*ResponseStatusRecorder) Push ¶
func (rr *ResponseStatusRecorder) Push(target string, opts *nethttp.PushOptions) error
Push forwards to the underlying net/http.Pusher when supported.
func (*ResponseStatusRecorder) ReadFrom ¶
func (rr *ResponseStatusRecorder) ReadFrom(r io.Reader) (int64, error)
ReadFrom forwards to the underlying io.ReaderFrom when supported. Like ResponseStatusRecorder.Write, the first body bytes imply status 200 if WriteHeader was not called.
func (*ResponseStatusRecorder) Write ¶
func (rr *ResponseStatusRecorder) Write(b []byte) (int, error)
Write implements net/http.ResponseWriter. If WriteHeader has not been called yet, the first Write implicitly sets status 200 (matching net/http.ResponseWriter rules) so ResponseStatusRecorder.Status reflects a committed response for middleware such as panic recovery that must not write a second body after bytes have been sent.
func (*ResponseStatusRecorder) WriteHeader ¶
func (rr *ResponseStatusRecorder) WriteHeader(code int)