PACKAGE_NAME

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 GetHeader

func GetHeader(ctx context.Context, key string) string

GetHeader retrieves a specific HTTP request header by key.

func GetReq

func GetReq(ctx context.Context) *http.Request

GetReq retrieves the *http.Request from context if available.

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

func ReadRequest(r *http.Request, i Object) error

ReadRequest parses the request body based on Content-Type and decodes it into the given Object.

func SetCookie

func SetCookie(ctx context.Context, cookie *http.Cookie)

SetCookie adds a Set-Cookie header to the HTTP response.

func SetHTTPCode

func SetHTTPCode(ctx context.Context, httpCode int)

SetHTTPCode sets the HTTP response status code.

func SetHeader

func SetHeader(ctx context.Context, key, value string)

SetHeader sets a response header key/value pair.

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 JSONHandler[Req, Resp Object] func(context.Context, Req) Resp

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.

type StreamHandler

type StreamHandler[Req, Resp Object] func(context.Context, Req, chan<- Resp)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL