Documentation
¶
Index ¶
- Variables
- func HandleJSON[Req RequestObject, Resp any](w http.ResponseWriter, r *http.Request, req Req, h JSONHandler[Req, Resp])
- func HandleStream[Req RequestObject, Resp *Event[T], T any](w http.ResponseWriter, r *http.Request, req Req, h StreamHandler[Req, Resp])
- func ReadRequest(r *http.Request, i RequestObject) error
- func WithRequestContext(ctx context.Context, c RequestContext) context.Context
- type Event
- func (e *Event[T]) Data(data T) *Event[T]
- func (e *Event[T]) Event(event string) *Event[T]
- func (e *Event[T]) GetData() any
- func (e *Event[T]) GetEvent() string
- func (e *Event[T]) GetID() string
- func (e *Event[T]) GetRetry() int
- func (e *Event[T]) HasEvent() bool
- func (e *Event[T]) HasID() bool
- func (e *Event[T]) HasRetry() bool
- func (e *Event[T]) ID(id string) *Event[T]
- func (e *Event[T]) Retry(retry int) *Event[T]
- type JSONHandler
- type NewRequestContext
- type RequestContext
- type RequestObject
- type Router
- type Server
- type SimpleContext
- type SimpleServer
- 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. Users should implement their own error handling logic.
var ReadBody = func(r *http.Request) ([]byte, error) { const maxBodySize = int64(10 << 20) // 10 MB is a lot of text defer func() { _ = r.Body.Close() }() reader := io.LimitReader(r.Body, maxBodySize+1) b, err := io.ReadAll(reader) if err != nil { return nil, errutil.Explain(err, "read body error") } if int64(len(b)) > maxBodySize { return nil, errutil.Explain(nil, "body too large") } return b, nil }
ReadBody reads the request body into a byte slice. Users can customize the ReadBody function.
Functions ¶
func HandleJSON ¶
func HandleJSON[Req RequestObject, Resp any](w http.ResponseWriter, r *http.Request, req Req, h JSONHandler[Req, Resp])
HandleJSON wraps a JSONHandler into an http.HandlerFunc to handle JSON requests.
func HandleStream ¶
func HandleStream[Req RequestObject, Resp *Event[T], T any](w http.ResponseWriter, r *http.Request, req Req, h StreamHandler[Req, Resp])
HandleStream wraps a StreamHandler into an http.HandlerFunc to handle streaming requests using Server-Sent Events (SSE).
func ReadRequest ¶
func ReadRequest(r *http.Request, i RequestObject) error
ReadRequest parses the request body based on Content-Type and decodes it into the given RequestObject.
func WithRequestContext ¶ added in v0.0.3
func WithRequestContext(ctx context.Context, c RequestContext) context.Context
WithRequestContext returns a new context derived from ctx that carries the given RequestContext.
Types ¶
type Event ¶
type Event[T any] struct { // contains filtered or unexported fields }
Event represents an SSE (Server-Sent Event) with optional ID, event type, data, and retry interval.
type NewRequestContext ¶ added in v0.0.3
type NewRequestContext func(r *http.Request, w http.ResponseWriter) RequestContext
NewRequestContext defines a factory function used to create a RequestContext from an HTTP request and response writer.
type RequestContext ¶ added in v0.0.3
type RequestContext interface {
// Request returns the underlying *http.Request.
Request() *http.Request
// ResponseWriter returns the underlying http.ResponseWriter.
ResponseWriter() http.ResponseWriter
// PathValue returns the value of the named path parameter.
PathValue(name string) string
}
RequestContext abstracts an HTTP request lifecycle context. It provides unified access to the request, response writer.
func GetRequestContext ¶ added in v0.0.3
func GetRequestContext(ctx context.Context) RequestContext
GetRequestContext retrieves the RequestContext stored in ctx. It returns nil if no RequestContext is present.
func NewSimpleContext ¶ added in v0.0.3
func NewSimpleContext(r *http.Request, w http.ResponseWriter) RequestContext
NewSimpleContext creates a SimpleContext instance and returns it as a RequestContext.
type RequestObject ¶
type RequestObject interface {
// Bind binds query/path parameters to the struct.
Bind(*http.Request) error
// Validate validates the parameters.
Validate() error
}
RequestObject defines the interface that all request types must implement.
type Router ¶
type Router struct {
Method string
Pattern string
Handler http.HandlerFunc
}
Router describes a single HTTP route definition, including method, URL pattern, and handler.
type Server ¶
type Server interface {
Route(r Router)
}
Server defines the minimal contract for an HTTP server capable of registering routes.
type SimpleContext ¶ added in v0.0.3
type SimpleContext struct {
// contains filtered or unexported fields
}
SimpleContext is a minimal RequestContext implementation backed directly by http.Request and http.ResponseWriter.
func (*SimpleContext) PathValue ¶ added in v0.0.3
func (c *SimpleContext) PathValue(name string) string
PathValue returns the value of the named path parameter.
func (*SimpleContext) Request ¶ added in v0.0.3
func (c *SimpleContext) Request() *http.Request
Request returns the underlying *http.Request.
func (*SimpleContext) ResponseWriter ¶ added in v0.0.3
func (c *SimpleContext) ResponseWriter() http.ResponseWriter
ResponseWriter returns the underlying http.ResponseWriter.
type SimpleServer ¶
SimpleServer is a lightweight HTTP server implementation based on http.Server and http.ServeMux.
func NewSimpleServer ¶
func NewSimpleServer(addr string) *SimpleServer
NewSimpleServer creates a SimpleServer bound to the given address.
func (*SimpleServer) Route ¶
func (s *SimpleServer) Route(r Router)
Route registers a route with method-based pattern matching. The pattern format follows Go 1.22+ ServeMux conventions, for example: "GET /users/{id}".