Documentation
¶
Overview ¶
Package respond is what every route shares: reading the request, writing JSON, turning process errors into statuses.
Index ¶
- func Decode(w http.ResponseWriter, r *http.Request, dest any) bool
- func Error(w http.ResponseWriter, err error)
- func ID(w http.ResponseWriter, r *http.Request) (string, bool)
- func JSON(w http.ResponseWriter, status int, data any)
- func List[T any](w http.ResponseWriter, status int, items []T)
- func Paging(w http.ResponseWriter, r *http.Request) (limit, offset int, ok bool)
- func Total(w http.ResponseWriter, total int)
- func ValidIDs(w http.ResponseWriter, ids ...string) bool
- type Failure
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
func Error(w http.ResponseWriter, err error)
Error writes the status of a process error. An unknown error is a bug or an outage: its detail may expose SQL or connection strings, so it only goes to the logs.
func ID ¶
ID reads {id} from the path. Not a uuid means no such item, and the check keeps Postgres from raising an error that not every process translates. On false the response is already written.
func JSON ¶
func JSON(w http.ResponseWriter, status int, data any)
JSON writes data with this status.
func List ¶
func List[T any](w http.ResponseWriter, status int, items []T)
List writes a list: an empty one is [], not null.
func Paging ¶
Paging reads ?limit=&offset= for a process that lists by pages, 0 when left out (no limit: all of them). On false the response is already written.
func Total ¶
func Total(w http.ResponseWriter, total int)
Total counts in X-Total-Count the whole list a page comes from. Before the body.