PACKAGE_NAME

package
v0.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HandleError = func(w http.ResponseWriter, err error) {
	http.Error(w, err.Error(), http.StatusInternalServerError)
}

HandleError is the default error handler for failed requests.

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 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