Documentation
¶
Index ¶
- Variables
- func Binding(req *http.Request, params []BindingField) error
- func GetHeader(ctx context.Context, key string) string
- func GetReq(ctx context.Context) *http.Request
- func HandleJSON[Req, Resp Object](h JSONHandler[Req, Resp]) http.HandlerFunc
- func HandleStream[Req, Resp Object](h StreamHandler[Req, Resp]) http.HandlerFunc
- func ReadRequest(r *http.Request, i Object) error
- func SetCookie(ctx context.Context, cookie *http.Cookie)
- func SetHTTPCode(ctx context.Context, httpCode int)
- func SetHeader(ctx context.Context, key, value string)
- type BindingField
- type JSONHandler
- type Object
- type ObjectBase
- type StreamHandler
Constants ¶
This section is empty.
Variables ¶
var ErrorHandler = func(r *http.Request, w http.ResponseWriter, err error) { http.Error(w, err.Error(), http.StatusInternalServerError) }
ErrorHandler is the default handler for reporting errors back to the client. By default, it responds with an HTTP 500 status and the error message.
Functions ¶
func Binding ¶
func Binding(req *http.Request, params []BindingField) error
Binding applies multiple BindingField rules to extract values from a request and bind them into a target object.
func HandleJSON ¶
func HandleJSON[Req, Resp Object](h JSONHandler[Req, Resp]) http.HandlerFunc
HandleJSON wraps a JSONHandler into an http.HandlerFunc.
func HandleStream ¶
func HandleStream[Req, Resp Object](h StreamHandler[Req, Resp]) http.HandlerFunc
HandleStream wraps a StreamHandler into an http.HandlerFunc. It supports server-sent event style streaming (SSE).
func ReadRequest ¶
ReadRequest parses the request body based on Content-Type and decodes it into the given Object.
func SetHTTPCode ¶
SetHTTPCode sets the HTTP response status code.
Types ¶
type BindingField ¶
type BindingField struct {
Field string // Struct field name
From string // "header", "path", or "query"
Name string // Parameter name
Target any // Target struct field pointer
}
BindingField describes a mapping rule for extracting values from request headers, path parameters, or query parameters.
type JSONHandler ¶
type Object ¶
type Object interface {
New() any // Returns a new instance of the object
Binding(req *http.Request) error // Extracts additional values from the request
Validate() error // Validates object fields
}
Object defines the interface that all request/response types must implement.
type ObjectBase ¶ added in v0.0.3
type ObjectBase struct{}
ObjectBase is a base struct for implementing Object.
func (*ObjectBase) Binding ¶ added in v0.0.3
func (x *ObjectBase) Binding(r *http.Request) error
Binding extracts non-body values (header, path, query) from *http.Request.
func (*ObjectBase) New ¶ added in v0.0.3
func (x *ObjectBase) New() any
New implements the Object interface.
func (*ObjectBase) Validate ¶ added in v0.0.3
func (x *ObjectBase) Validate() error
Validate checks field values using generated validation expressions.