httpsvr

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 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 GetHeader

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

GetHeader retrieves a specific HTTP request header by key from the context.

func GetReq

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

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

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 SetCode

func SetCode(ctx context.Context, httpCode int)

SetCode sets the HTTP response status code in the context.

func SetCookie

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

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

func SetHeader

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

SetHeader sets a response header key/value pair in the context.

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

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

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.

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