Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithBody ¶
func WithBody[T any]( decoding func(w http.ResponseWriter, r *http.Request, dest any) bool, next func(ctx context.Context, w http.ResponseWriter, r *http.Request, body T), ) http.HandlerFunc
WithBody adapts a request body handler function to a standard http.HandlerFunc. It decodes the request body into type T using the provided decoding function. If decoding succeeds (ok=true), it calls the handler with the decoded body.
Example usage:
type UserRequest struct {
Name string `json:"name"`
Age int `json:"age"`
}
func decodeJSON(w http.ResponseWriter, r *http.Request, dest any) bool {
if err := json.NewDecoder(r.Body).Decode(dest); err != nil {
http.Error(w, "invalid request body", http.StatusBadRequest)
return false
}
return true
}
http.HandleFunc("/api/user", WithBody[UserRequest](decodeJSON, func(ctx context.Context, w http.ResponseWriter, r *http.Request, body UserRequest) {
// Use the decoded body
}))
func WithContext ¶
func WithContext(next func(ctx context.Context, w http.ResponseWriter, r *http.Request)) http.HandlerFunc
WithContext adapts a context-aware HTTP handler function to a standard http.HandlerFunc. It provides a more ergonomic way to access request context by making it an explicit parameter rather than having to call r.Context() repeatedly.
Example usage:
func myHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
userID := ctx.Value(UserIDKey).(string)
// ... use context directly ...
}
// Convert to http.HandlerFunc
http.HandleFunc("/api", WithContext(myHandler))
Types ¶
type BodyResponseWriter ¶
type BodyResponseWriter struct {
http.ResponseWriter
// contains filtered or unexported fields
}
BodyResponseWriter wraps an http.ResponseWriter and captures the response body up to a specified limit. It's useful when you need to access the response body after the handler has finished writing.
func NewBodyResponseWriter ¶
func NewBodyResponseWriter(w http.ResponseWriter, limit int64) *BodyResponseWriter
NewBodyResponseWriter returns a new BodyResponseWriter that wraps the given http.ResponseWriter. Limit is the maximum number of bytes to be written to the buffer, < 0 means no limit.
Please always call Close() when you are done to release the buffer.
func (*BodyResponseWriter) Body ¶
func (w *BodyResponseWriter) Body() []byte
Body returns the body bytes.
func (*BodyResponseWriter) Close ¶
func (w *BodyResponseWriter) Close() error
Close releases the buffer. You must call this method when you are done with the BodyResponseWriter. Close always returns nil error.
func (*BodyResponseWriter) Truncated ¶
func (w *BodyResponseWriter) Truncated() bool
Truncated reports whether the body was truncated.
func (*BodyResponseWriter) Unwrap ¶
func (w *BodyResponseWriter) Unwrap() http.ResponseWriter
Unwrap returns the underlying http.ResponseWriter.
func (*BodyResponseWriter) Write ¶
func (w *BodyResponseWriter) Write(b []byte) (int, error)
Write implements the http.ResponseWriter interface. It writes to both the underlying ResponseWriter and an internal buffer. The buffer will stop accepting new writes after reaching the specified limit, if any.
type Route ¶
type Route struct {
Pattern string
Handler http.HandlerFunc
}
Route pairs a URL pattern with its HTTP handler function. Pattern format is "METHOD PATH" (e.g. "GET /z/y/z/{params}"). See https://pkg.go.dev/net/http#ServeMux for pattern format details.
type StatusResponseWriter ¶
type StatusResponseWriter struct {
http.ResponseWriter
// contains filtered or unexported fields
}
StatusResponseWriter wraps an http.ResponseWriter and records the status code written via WriteHeader.
This is useful when you want to write a middleware that needs to know the status code, which only available after the handler has finished.
func NewStatusResponseWriter ¶
func NewStatusResponseWriter(w http.ResponseWriter) *StatusResponseWriter
NewStatusResponseWriter returns a new StatusResponseWriter that wraps the given http.ResponseWriter.
func (*StatusResponseWriter) Status ¶
func (w *StatusResponseWriter) Status() int
Status returns the HTTP status code. It defaults to 200 if WriteHeader was never called.
func (*StatusResponseWriter) Unwrap ¶
func (r *StatusResponseWriter) Unwrap() http.ResponseWriter
Unwrap returns the underlying http.ResponseWriter.
func (*StatusResponseWriter) WriteHeader ¶
func (w *StatusResponseWriter) WriteHeader(status int)
WriteHeader records the status code and calls WriteHeader on the underlying ResponseWriter.
func (*StatusResponseWriter) Written ¶
func (w *StatusResponseWriter) Written() bool
Written reports whether WriteHeader has been called.