nethttp

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package nethttp adapts api/rest route handles to net/http handlers.

Each [RouteHandle] from api/rest becomes an http.Handler via Handler. Register wires it directly onto an http.ServeMux using the Go 1.22+ method-prefixed pattern ("POST /users", "GET /users/{id}", etc.).

Typical usage:

b := rest.NewBuilder(rest.Info{Title: "User API", Version: "1.0.0"})
createUser := rest.AddRoute[CreateReq, User](b, "POST", "/users", ...)

mux := http.NewServeMux()
nethttp.Register(mux, createUser, func(ctx context.Context, req CreateReq) (User, error) {
    // Access path params via the embedded request:
    r, _ := nethttp.RequestFromContext(ctx)
    id := r.PathValue("id")
    return svc.CreateUser(ctx, req)
})
http.ListenAndServe(":8080", mux)

Error responses use the JSON body {"error":"<message>"} by default: 400 for decode/validation failures, 500 for handler or encode errors. Override by supplying a custom Options.ErrorHandler via HandlerWithOptions.

For body-less methods (GET, HEAD, DELETE) the handler function is called with the zero value of Req. Access path and query parameters through RequestFromContext.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler[Req, Resp any](handle *rest.RouteHandle[Req, Resp], fn HandlerFunc[Req, Resp]) http.Handler

Handler wraps a rest.RouteHandle and a HandlerFunc into an http.Handler using default options (JSON error envelope, 1 MiB body limit).

For body-bearing methods (POST, PUT, PATCH) the request body is read, decoded, and validated using the route's codec before fn is called. For other methods (GET, HEAD, DELETE) fn is called with the zero value of Req.

On success the response is JSON-encoded and written with the HTTP status from the route descriptor's primary response (the first entry in Responses).

func HandlerWithOptions

func HandlerWithOptions[Req, Resp any](handle *rest.RouteHandle[Req, Resp], fn HandlerFunc[Req, Resp], opts Options) http.Handler

HandlerWithOptions is like Handler but accepts Options to customise error handling.

func Register

func Register[Req, Resp any](mux *http.ServeMux, handle *rest.RouteHandle[Req, Resp], fn HandlerFunc[Req, Resp])

Register registers the route on mux using its method and path from the route descriptor. It uses the Go 1.22+ enhanced ServeMux pattern "METHOD /path" so each registration is scoped to a single method.

func RegisterWithOptions

func RegisterWithOptions[Req, Resp any](mux *http.ServeMux, handle *rest.RouteHandle[Req, Resp], fn HandlerFunc[Req, Resp], opts Options)

RegisterWithOptions is like Register but accepts Options.

func RequestFromContext

func RequestFromContext(ctx context.Context) (*http.Request, bool)

RequestFromContext retrieves the *http.Request stored in ctx by Handler. Returns false if the context was not created by this package.

Types

type HandlerFunc

type HandlerFunc[Req, Resp any] func(ctx context.Context, req Req) (Resp, error)

HandlerFunc is the typed application handler called by Handler. ctx is the request context. req is the decoded request value; for body-less methods it is the zero value of Req. Use RequestFromContext to access the underlying *http.Request for path parameters, headers, or other request metadata.

type Options

type Options struct {
	// ErrorHandler, when non-nil, is called instead of the default JSON error
	// envelope when a request fails. status is the suggested HTTP status code
	// (400 or 500). Implementations must write the response header and body.
	ErrorHandler func(w http.ResponseWriter, r *http.Request, status int, err error)
}

Options configures the behaviour of HandlerWithOptions.

Jump to

Keyboard shortcuts

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