Documentation
¶
Overview ¶
Package httputil provides HTTP helpers: error handling and JSON responses, request decoding and validation, pagination (ClampPage, ClampPerPage, ParseIntQuery), client IP resolution with proxy headers, search sanitization (EscapeILIKE), multipart limits, file download responses, and chi route pattern extraction (ChiPathFromRequest).
Index ¶
- Constants
- func ChiPathFromRequest(r *http.Request) string
- 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) (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, maxBodySize, 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 RenderAccepted[T any](w http.ResponseWriter, r *http.Request, data 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"
UserIDKey is the context key for the authenticated user ID (string, e.g. UUID). Set by auth middleware.
Variables ¶
This section is empty.
Functions ¶
func ChiPathFromRequest ¶ added in v0.1.1
ChiPathFromRequest returns the route pattern from chi's RouteContext, or empty string if none.
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 ¶
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 implements httpErrorWithStatus, 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 ¶
func ParseMultipartFormLimit(w http.ResponseWriter, r *http.Request, maxBodySize, maxMemory int64) bool
ParseMultipartFormLimit parses multipart form with maxBodySize (MaxBytesReader) and maxMemory (ParseMultipartForm). On error writes response and returns false.
func ParseUUIDField ¶
ParseUUIDField parses value as UUID for a given field name (validation error).
func RenderAccepted ¶ added in v0.1.1
func RenderAccepted[T any](w http.ResponseWriter, r *http.Request, data T)
RenderAccepted sends 202 Accepted with JSON body.
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.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package middleware provides HTTP middleware for go-httpkit.
|
Package middleware provides HTTP middleware for go-httpkit. |