web

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 5 Imported by: 0

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

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

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

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

func DecodeJSON(w http.ResponseWriter, r *http.Request, dst any) error

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 Error

func Error(w http.ResponseWriter, status int, message string)

func Int64Param

func Int64Param(r *http.Request, name string) (int64, error)

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{})

func ParsePage

func ParsePage(r *http.Request) (limit, offset int, err error)

ParsePage reads ?limit and ?offset. Absent values fall back to the default page; an out-of-range limit is clamped rather than rejected, because a caller asking for too much still wants rows back.

Types

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

Jump to

Keyboard shortcuts

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