handlers

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildInsertArgs

func BuildInsertArgs(body map[string]any) (cols, placeholders string, args []any)

BuildInsertArgs builds the column list, placeholder list ($1,$2…), and args slice from a JSON-decoded body map. Keys are sorted for deterministic output and each column identifier is quoted with pgx.Identifier.Sanitize so a key can never break out of the identifier position (defence-in-depth alongside ValidateWritableColumns).

func CachedGet

func CachedGet(next http.HandlerFunc) http.HandlerFunc

CachedGet wraps a GET handler to add ETag and Cache-Control headers. If the request includes an If-None-Match header that matches the computed ETag, it returns 304 Not Modified with no body. Only 200 responses are cached.

func FilterFields

func FilterFields(record map[string]any, allowed []string) map[string]any

FilterFields returns a copy of record containing only the keys in allowed. If allowed is empty, record is returned unchanged.

func IsServerError

func IsServerError(err error) bool

IsServerError reports whether err would be answered as HTTP 500 by WriteDBError (i.e. it is not a missing-tenant / bad-input / unavailable error). Used to decide whether to capture a stack trace — client/availability errors are not bugs.

func RowsToMaps

func RowsToMaps(rows pgx.Rows) ([]map[string]any, error)

RowsToMaps converts pgx.Rows to a slice of string-keyed maps. UUID columns ([16]byte) are converted to hyphenated UUID strings.

func WriteDBError

func WriteDBError(w http.ResponseWriter, err error)

WriteDBError maps a database error to an HTTP response without leaking internal details (raw SQL, schema names) to the client:

  • missing tenant schema/relation (42P01/3F000) → 400 "invalid tenant"
  • caller-supplied bad input (22P02, e.g. a non-UUID id) → 400 "invalid request"
  • unknown column on a write (42703) → 422 validation_failed/unknown_field
  • unreachable database (timeout / open breaker) → 503 "service unavailable"
  • anything else → 500 "internal error"

func WriteJSON

func WriteJSON(w http.ResponseWriter, v any) error

WriteJSON encodes v to JSON and writes it to w using a pooled buffer, avoiding per-request buffer allocations for the JSON encoder. Callers must set Content-Type and status code before calling.

Types

This section is empty.

Jump to

Keyboard shortcuts

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