server

package
v1.14.5 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 20 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Coder

type Coder interface {
	HTTPCode() string
}

type Config

type Config struct {
	ReadHeaderTimeout time.Duration
	ReadTimeout       time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
	MaxHeaderBytes    int
	ShutdownTimeout   time.Duration
}

A zero is no timeout, ShutdownTimeout included: a hung shutdown is better seen than silently truncated.

type Detailer

type Detailer interface {
	HTTPDetails() any
}

type Handler

type Handler func(src Source) (Renderer, error)

Handler: nil Response + nil error → 204.

type Layer

type Layer = func(next Handler) Handler

Layer is a type alias so named function-typed middlewares (CustomerAuth, a per-route guard) are assignable to Layer without explicit conversion.

func Bind

func Bind[Req any, PT scan.Parser[Req], Res any](fn func(context.Context, PT) (*Response[Res], error)) Layer

The first error short-circuits with a nil response, so a handler never runs on a half-parsed request.

func Handle

func Handle(fn Handler) Layer

type Middleware added in v1.12.2

type Middleware func(next Handler) Handler

Middleware is the element of a global chain. Named rather than aliased: a generated container names its fields after types, and an alias has no name.

type Mux

type Mux interface {
	http.Handler

	Use(layers ...Layer)

	// Group snapshots layers and prefix; parent and child diverge after.
	Group(prefix string, layers ...Layer) Mux

	Get(path string, layers ...Layer)
	Post(path string, layers ...Layer)
	Put(path string, layers ...Layer)
	Patch(path string, layers ...Layer)
	Delete(path string, layers ...Layer)
	Handle(method, path string, layers ...Layer)

	Routes() []RouteMeta
}

chi rather than stdlib ServeMux: radix-tree routing tolerates overlapping patterns (`/x/{id}` beside `/x/foo/{id}`) that ServeMux rejects.

func New

func New() Mux

type Option

type Option func(*server)

func WithIdleTimeout

func WithIdleTimeout(d time.Duration) Option

func WithMaxHeaderBytes

func WithMaxHeaderBytes(n int) Option

func WithReadHeaderTimeout

func WithReadHeaderTimeout(d time.Duration) Option

func WithReadTimeout

func WithReadTimeout(d time.Duration) Option

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) Option

type Renderer

type Renderer interface {
	Render(w http.ResponseWriter) error
	WriteBody(w io.Writer) error

	Status() int
	Headers() http.Header
	Body() any
	Stream() func(w io.Writer) error
}

func File

func File(req *http.Request, name string, modTime time.Time, content io.ReadSeeker) Renderer

func Redirect

func Redirect(status int, url string) Renderer

Redirect issues a 3xx redirect to url. Extra headers may be added via Headers() before the response is rendered.

type Response

type Response[Res any] struct {
	// contains filtered or unexported fields
}

Response[Res]: set one of body or stream. status 0 → 200.

func Accepted

func Accepted[Res any](body *Res) *Response[Res]

func Created

func Created[Res any](body *Res) *Response[Res]

func HTML

func HTML[Res any](status int, body *Res) *Response[Res]

func JSON

func JSON[Res any](status int, body *Res) *Response[Res]

func NewJsonResponse

func NewJsonResponse[Res any](status int, body *Res) *Response[Res]

func NewResponse

func NewResponse[Res any](status int, body *Res) *Response[Res]

func NoContent

func NoContent() *Response[any]

func OK

func OK[Res any](body *Res) *Response[Res]

func StreamWith

func StreamWith(status int, contentType string, fn func(w io.Writer) error) *Response[any]

func Text

func Text[Res any](status int, body *Res) *Response[Res]

func XML

func XML[Res any](status int, body *Res) *Response[Res]

func (*Response[Res]) Body

func (r *Response[Res]) Body() any

func (*Response[Res]) Headers

func (r *Response[Res]) Headers() http.Header

Allocates on first access and hands out the live map, so asking is itself a write: nothing downstream can tell "never touched" from "emptied".

func (*Response[Res]) Render

func (r *Response[Res]) Render(w http.ResponseWriter) error

func (*Response[Res]) Status

func (r *Response[Res]) Status() int

func (*Response[Res]) Stream

func (r *Response[Res]) Stream() func(w io.Writer) error

func (*Response[Res]) WriteBody

func (r *Response[Res]) WriteBody(w io.Writer) error

The body alone, for a layer that wraps the writer and adds its own framing.

type RouteMeta

type RouteMeta struct {
	Method  string
	Path    string
	Req     reflect.Type
	Res     reflect.Type
	Handler uintptr
}

Req and Res are nil for a route registered through Handle: the specification gets the route without schemas or an operationId.

type Router

type Router interface {
	Setup(mux Mux)
}

type Server

type Server interface {
	Serve(ctx context.Context) error
	Close() error
}

Server: shutdown via ctx cancellation or Close — both idempotent.

func NewServer

func NewServer(
	mux Mux,
	router Router,
	addr net.Addr,
	logger pry.Logger,
	opts ...Option,
) Server

type Source

type Source interface {
	scan.Source

	Path(key string) string
	Query(key string) string
	QueryAll(key string) []string
	Header(key string) string
	HeaderAll(key string) []string
	Cookie(key string) string
	Form(key string) string
	FormAll(key string) []string

	Peek(n int) []byte

	Request() *http.Request
	SetRequest(*http.Request)
}

The typed helpers are for what has no generated Parse: middlewares, and handlers written by hand.

func From

func From(r *http.Request) Source

Query, form and body reads are cached: a handler and a middleware asking for the body must not race for the reader.

type StatusError

type StatusError interface {
	error
	HTTPStatus() int
}

StatusError sets HTTP status. Errors without it default to 500.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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