Documentation
¶
Index ¶
- Constants
- func ClampLimit(p *int, defaultVal, maxVal int) int
- func ClampPage(p *int) int
- func ClampPerPage(p *int, defaultVal, maxVal int) int
- func DecodeAndValidate[T any](w http.ResponseWriter, r *http.Request, v Validator, log *slog.Logger, ...) (T, bool)
- func DecodeAndValidateE[T any](r *http.Request, v Validator) (T, error)
- func DecodeJSON[T any](r *http.Request, v *T) error
- func EscapeILIKE(s string, maxLen int) string
- func GetClientIP(r *http.Request, trustedProxyCIDRs []string) string
- func GetUserID(ctx context.Context) string
- func HandleError(w http.ResponseWriter, r *http.Request, err error)
- func ParseAuthUserID(w http.ResponseWriter, r *http.Request) (uuid.UUID, bool)
- func ParseIntQuery(r *http.Request, key string) *int
- func ParseMultipartFormLimit(w http.ResponseWriter, r *http.Request, maxMemory int64) bool
- func ParseUUID(w http.ResponseWriter, r *http.Request, id string) (uuid.UUID, bool)
- func ParseUUIDField(w http.ResponseWriter, r *http.Request, value, field string) (uuid.UUID, bool)
- func Ptr[T any](v T) *T
- func RenderBytes(w http.ResponseWriter, contentType, filename string, data []byte) error
- func RenderCreated[T any](w http.ResponseWriter, r *http.Request, data T)
- func RenderError(w http.ResponseWriter, r *http.Request, status int, message string)
- func RenderErrorWithCode(w http.ResponseWriter, r *http.Request, status int, message, code string)
- func RenderInvalidID(w http.ResponseWriter, r *http.Request)
- func RenderJSON[T any](w http.ResponseWriter, r *http.Request, status int, data T)
- func RenderJSONAttachment[T any](w http.ResponseWriter, data T, filename string) error
- func RenderNoContent(w http.ResponseWriter, _ *http.Request)
- func RenderOK[T any](w http.ResponseWriter, r *http.Request, data T)
- func RenderStream(w http.ResponseWriter, contentType, filename string, rc io.Reader) error
- func RenderText(w http.ResponseWriter, _ *http.Request, status int, contentType, body string)
- func SanitizeSearchQ(q string, maxLen int) string
- func ValidateSearchQ(q string) bool
- type ErrorResponse
- type ValidationErrorItem
- type ValidationErrorResponse
- type Validator
Constants ¶
const DefaultSearchMaxLen = 100
const MaxPage = 10000
const MaxRequestBodySize = 1 << 20
const UserIDKey contextKey = "user_id"
Variables ¶
This section is empty.
Functions ¶
func ClampLimit ¶
ClampLimit returns limit clamped to [defaultVal, maxVal], or defaultVal if nil/<=0.
func ClampPerPage ¶
ClampPerPage returns perPage clamped to [defaultVal, maxVal], or defaultVal if nil/<=0.
func DecodeAndValidate ¶
func DecodeAndValidate[T any]( w http.ResponseWriter, r *http.Request, v Validator, log *slog.Logger, operation string, ) (T, bool)
DecodeAndValidate reads and validates JSON from the request body. On error writes response and returns false.
func DecodeAndValidateE ¶
DecodeAndValidateE reads and validates JSON from the request body and returns an error without writing response.
func DecodeJSON ¶
DecodeJSON decodes JSON from the request body with size limit and no trailing data.
func EscapeILIKE ¶
EscapeILIKE escapes %, _, \ for safe use in PostgreSQL ILIKE.
func GetClientIP ¶
GetClientIP returns the client IP, considering X-Real-IP and X-Forwarded-For when remote is in trustedProxyCIDRs.
func HandleError ¶
func HandleError(w http.ResponseWriter, r *http.Request, err error)
HandleError writes a JSON error response. If err is *httperr.HTTPError, uses its status and code; otherwise 500.
func ParseAuthUserID ¶
ParseAuthUserID returns the authenticated user's UUID from context or writes error.
func ParseIntQuery ¶
ParseIntQuery parses the first query parameter key as positive int; returns nil if missing or invalid.
func ParseMultipartFormLimit ¶
ParseMultipartFormLimit parses multipart form with maxMemory limit. On error writes response and returns false.
func ParseUUIDField ¶
ParseUUIDField parses value as UUID for a given field name (validation error).
func RenderBytes ¶
func RenderBytes(w http.ResponseWriter, contentType, filename string, data []byte) error
RenderBytes writes raw bytes with Content-Disposition attachment.
func RenderCreated ¶
func RenderCreated[T any](w http.ResponseWriter, r *http.Request, data T)
RenderCreated sends 201 Created with JSON body.
func RenderError ¶
RenderError sends JSON error with status and message; code is derived from status.
func RenderErrorWithCode ¶
RenderErrorWithCode sends JSON error with explicit code.
func RenderInvalidID ¶
func RenderInvalidID(w http.ResponseWriter, r *http.Request)
RenderInvalidID sends 400 with INVALID_ID code.
func RenderJSON ¶
RenderJSON writes data as JSON with the given status.
func RenderJSONAttachment ¶
func RenderJSONAttachment[T any](w http.ResponseWriter, data T, filename string) error
RenderJSONAttachment encodes data as JSON and writes it as a downloadable attachment.
func RenderNoContent ¶
func RenderNoContent(w http.ResponseWriter, _ *http.Request)
RenderNoContent sends 204 No Content.
func RenderOK ¶
func RenderOK[T any](w http.ResponseWriter, r *http.Request, data T)
RenderOK sends 200 OK with JSON body.
func RenderStream ¶
RenderStream writes a streaming response with Content-Disposition attachment. Caller must close rc.
func RenderText ¶
RenderText writes a text response with given content type and body.
func SanitizeSearchQ ¶
SanitizeSearchQ is EscapeILIKE with default max length.
func ValidateSearchQ ¶
ValidateSearchQ returns true if q is valid for search (length and no control chars).
Types ¶
type ErrorResponse ¶
ErrorResponse is the JSON shape for error responses.
type ValidationErrorItem ¶
ValidationErrorItem represents a single validation error.
type ValidationErrorResponse ¶
type ValidationErrorResponse struct {
Code string `json:"code"`
Message string `json:"message"`
Errors []ValidationErrorItem `json:"errors,omitempty"`
}
ValidationErrorResponse is the JSON shape for validation errors.