Documentation
¶
Index ¶
- Variables
- func GetHeader(ctx context.Context, key string) string
- func GetReq(ctx context.Context) *http.Request
- 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 SetCode(ctx context.Context, httpCode int)
- func SetCookie(ctx context.Context, cookie *http.Cookie)
- func SetHeader(ctx context.Context, key, value string)
- 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 RequestObject
- type Router
- type Server
- 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.
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 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 defines a single route with HTTP method, pattern, and handler.
type Server ¶
type Server interface {
Route(r Router)
}
Server is an interface that defines a method to register routes.
type SimpleServer ¶
SimpleServer defines a basic HTTP server with an internal multiplexer.
func NewSimpleServer ¶
func NewSimpleServer(addr string) *SimpleServer
NewSimpleServer creates a new SimpleServer instance with the specified address.
func (*SimpleServer) Route ¶
func (s *SimpleServer) Route(r Router)
Route registers a new route in the SimpleServer with the provided router.