Documentation
¶
Overview ¶
Package httputil provides utilities for writing http services.
Index ¶
- Constants
- func AllowCaching(headers http.Header)
- func AllowContentTypeMiddleware(next http.Handler, contentTypes ...string) http.Handler
- func InstanceFromContext(ctx context.Context) uuid.UUID
- func RecovererMiddleware(next http.Handler) http.Handler
- func Serve(ctx context.Context, srv *http.Server, timeout time.Duration) error
- func TimeoutMiddleware(next http.Handler, timeout time.Duration) http.Handler
- func TracingMiddleware(next http.Handler) http.Handler
- func WriteJSON(w http.ResponseWriter, obj any) error
- type Client
- type ClientError
- type ClientFunc
- type ClientMiddlewareFunc
- type HTTPError
- type MiddlewareFunc
- type RequestEditorFunc
- type RootHandler
- type RouteMiddlewareFunc
- type Router
Constants ¶
const ( // HeaderUsername is the header set by the auth system (reverse proxy) to denote the username HeaderUsername = "X-Auth-Username" // HeaderInstance is a header used for identify this unique request/response (primitive tracing) HeaderInstance = "X-Instance" // HeaderCreationDate denotes the date at which this item was first uploaded to the telemetry server (used for replication purposes) HeaderCreationDate = "X-Creation-Date" )
const (
// MediaTypeProblem is the content type for errors produced by the server
MediaTypeProblem = "application/problem+json; charset=utf-8"
)
Variables ¶
This section is empty.
Functions ¶
func AllowContentTypeMiddleware ¶
AllowContentTypeMiddleware enforces a allowlist of request Content-Types
func InstanceFromContext ¶
InstanceFromContext returns the instance for this request to uniquely identify the request
func RecovererMiddleware ¶
RecovererMiddleware is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.
KMT - I am not sure we need this middleware since the golang server already recovers from panics. It just does not use our logger or return a 500.
func TimeoutMiddleware ¶
TimeoutMiddleware cancels the request context after a given timeout duration
func TracingMiddleware ¶
TracingMiddleware injects a tracing ID into the context
Types ¶
type Client ¶
Client is an interface for HTTP clients.
func WithRequestEditors ¶
WithRequestEditors wraps a Client with request editor functions (RequestEditorFunc).
Each given RequestEditorFunc will be called to modify the request before it is handled by [Client.Do].
If a RequestEditorFunc returns an error, the error will be returned and the underlying [Client.Do] will not be called.
func WrapClient ¶
func WrapClient(client Client, middlewares ...ClientMiddlewareFunc) Client
WrapClient wraps a Client with client middlewares.
type ClientError ¶
type ClientError interface { Error() string // Extra KeyValue pairs associated with the error ErrorArgs() []any // ResponseBody returns response body. ResponseBody() ([]byte, error) // ResponseHeaders returns http status code and headers. ResponseHeaders() (int, map[string]string) }
ClientError is an error whose details to be shared with client.
type ClientFunc ¶
ClientFunc promotes a function to a Client.
type ClientMiddlewareFunc ¶
ClientMiddlewareFunc is an alias for client middleware functions.
type HTTPError ¶
type HTTPError struct { Cause error `json:"-"` CauseArgs []any `json:"-"` Detail string `json:"detail"` StatusCode int `json:"-"` Status string `json:"status"` }
HTTPError implements ClientError interface.
func NewHTTPError ¶
NewHTTPError returns a new error
func (*HTTPError) LogValue ¶
LogValue implements log/slog.LogValuer.
func (*HTTPError) ResponseBody ¶
ResponseBody returns JSON response body.
type MiddlewareFunc ¶
MiddlewareFunc is an alias for middleware functions.
func LoggingMiddleware ¶
func LoggingMiddleware(log *slog.Logger) MiddlewareFunc
LoggingMiddleware injects a logger into the context.
A previous implementation contained a memory leak because the tracing attributes were always appended to the given logger.
func ServerHeaderMiddleware ¶
func ServerHeaderMiddleware(server string) MiddlewareFunc
ServerHeaderMiddleware injects the Server into the response headers
type RequestEditorFunc ¶
RequestEditorFunc edits HTTP requests.
func WithBasicAuth ¶
func WithBasicAuth(username, password string) RequestEditorFunc
WithBasicAuth produces a RequestEditorFunc that sets basic auth with http.Request.SetBasicAuth for all requests.
type RootHandler ¶
type RootHandler func(http.ResponseWriter, *http.Request) error
RootHandler a wrapper around the handler functions to allow uniform error handling
func (RootHandler) ServeHTTP ¶
func (fn RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
RootHandler implements http.Handler interface.
type RouteMiddlewareFunc ¶
RouteMiddlewareFunc modifies a handler as it is registered with a router.
func WrapMiddleware ¶
func WrapMiddleware(middleware MiddlewareFunc) RouteMiddlewareFunc
WrapMiddleware adapts a MiddlewareFunc to a RouteMiddlewareFunc by ignoring the pattern argument
type Router ¶
Router represents an HTTP request server like http.ServeMux.
func WrapHandler ¶
func WrapHandler(mux Router, middlewares ...MiddlewareFunc) Router
WrapHandler wraps a [ServeMuxer] with middlewares.
func WrapRouter ¶
func WrapRouter(mux Router, middlewares ...RouteMiddlewareFunc) Router
WrapRouter wraps a Router with RouteMiddleware functions.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package promhttputil contains a prometheus metrics middleware, relocated from httputil.
|
Package promhttputil contains a prometheus metrics middleware, relocated from httputil. |