httpsvr

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 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. Users should implement their own error handling logic.

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

func NewEvent

func NewEvent[T any]() *Event[T]

NewEvent creates a new Event instance.

func (*Event[T]) Data

func (e *Event[T]) Data(data T) *Event[T]

Data sets the data of the SSE event.

func (*Event[T]) Event

func (e *Event[T]) Event(event string) *Event[T]

Event sets the event type of the SSE event.

func (*Event[T]) GetData

func (e *Event[T]) GetData() any

GetData returns the data of the SSE event.

func (*Event[T]) GetEvent

func (e *Event[T]) GetEvent() string

GetEvent returns the event type of the SSE event.

func (*Event[T]) GetID

func (e *Event[T]) GetID() string

GetID returns the ID of the SSE event.

func (*Event[T]) GetRetry

func (e *Event[T]) GetRetry() int

GetRetry returns the retry interval of the SSE event.

func (*Event[T]) HasEvent

func (e *Event[T]) HasEvent() bool

HasEvent returns true if the SSE event has an event type.

func (*Event[T]) HasID

func (e *Event[T]) HasID() bool

HasID returns true if the SSE event has an ID.

func (*Event[T]) HasRetry

func (e *Event[T]) HasRetry() bool

HasRetry returns true if the SSE event has a retry interval.

func (*Event[T]) ID

func (e *Event[T]) ID(id string) *Event[T]

ID sets the ID of the SSE event.

func (*Event[T]) Retry

func (e *Event[T]) Retry(retry int) *Event[T]

Retry sets the retry interval of the SSE event.

type JSONHandler

type JSONHandler[Req any, Resp any] func(context.Context, Req) Resp

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

type SimpleServer struct {
	*http.Server
	// contains filtered or unexported fields
}

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}".

type StreamHandler

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

Jump to

Keyboard shortcuts

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