Documentation
¶
Overview ¶
Package web holds the HTTP request and response plumbing shared by every domain handler: JSON encoding with a uniform error envelope, and decoding that is bounded before it is parsed.
The bounds are the reason it is one package rather than a helper per domain. DecodeJSON caps the request body and ParsePage clamps ?limit, so a handler cannot forget either — an unbounded Decode or an unclamped page size is a memory-exhaustion path, and there is no second place to fix it.
Index ¶
- Constants
- Variables
- func DecodeJSON(w http.ResponseWriter, r *http.Request, dst any) error
- func Error(w http.ResponseWriter, status int, message string)
- func Int64Param(r *http.Request, name string) (int64, error)
- func JSON(w http.ResponseWriter, status int, data interface{})
- func ParsePage(r *http.Request) (limit, offset int, err error)
- type ErrorResponse
Constants ¶
const ( DefaultListLimit = 100 MaxListLimit = 500 )
DefaultListLimit / MaxListLimit bound the list endpoints, so a caller that omits ?limit does not walk the whole table and one that asks for everything still cannot.
const MaxRequestBody = 1 << 20 // 1 MiB
MaxRequestBody bounds a write request body. Appsettings trees and translation bundles are the largest payloads the API accepts and they are kilobytes, so a megabyte is generous — the point is that an unbounded Decode cannot be used to exhaust memory.
Variables ¶
var ErrInvalidQueryParam = errors.New("invalid query parameter")
ErrInvalidQueryParam is returned when a numeric query parameter is not a non-negative integer. Callers map it to 400.
Functions ¶
func DecodeJSON ¶
DecodeJSON reads a size-limited JSON body into dst. Everything that decodes a request body goes through here so the limit cannot be forgotten.
func Int64Param ¶
Int64Param reads an optional numeric filter (an id). Absent yields 0, which every filter treats as "no filter".
func JSON ¶
func JSON(w http.ResponseWriter, status int, data interface{})
Types ¶
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
}