Documentation
¶
Index ¶
- func AllIn[T comparable](values []T, safelist ...T) bool
- func Between[T cmp.Ordered](value, min, max T) bool
- func HTTPError(status int, msg string) error
- func Handle[T any](fn func(*Req, T) error, opts ...handleOption) http.HandlerFunc
- func HotReloadHandler(w http.ResponseWriter, r *http.Request)
- func In[T comparable](value T, safelist ...T) bool
- func IsEmail(value string) bool
- func IsURL(value string) bool
- func Matches(value string, rx *regexp.Regexp) bool
- func MaxRunes(value string, n int) bool
- func MinRunes(value string, n int) bool
- func NewExtractor(tag string, fn func(*http.Request, string) (string, bool)) extractor
- func NewValidator[V any](name, msg string, fn func(V) bool) validator
- func NewValidatorWithArg[V any, A Parseable](name, msg string, fn func(V, A) bool) validator
- func NoDuplicates[T comparable](values []T) bool
- func NonZero[T comparable](value T) bool
- func NotBlank(value string) bool
- func NotIn[T comparable](value T, blocklist ...T) bool
- func StaticHandler(dir, prefix string, files embed.FS) http.Handler
- func WithExtractors(extractors ...extractor) handleOption
- func WithValidators(validators ...validator) handleOption
- type Decoder
- type Parseable
- type Req
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllIn ¶
func AllIn[T comparable](values []T, safelist ...T) bool
AllIn reports whether every element of values is in the safelist.
func HTTPError ¶
HTTPError returns an error that the Handle adapter unwraps into an HTTP error response.
func Handle ¶
func Handle[T any](fn func(*Req, T) error, opts ...handleOption) http.HandlerFunc
Handle is a generic adapter that decodes request input into T, then always calls fn. Per-field decode errors are written into Req.FieldErrors so the handler can inspect them alongside its own Check/CheckField calls.
func HotReloadHandler ¶
func HotReloadHandler(w http.ResponseWriter, r *http.Request)
hotreloadHandler is designed for the use case of a developer with a single browser tab open. That tab should have the following code (assuming this handler is running at /hot-reload):
<div data-init="@get('/hot-reload', {retryMaxCount: 1000, retryInterval: 20, retryMaxWaitMs: 200})" id="hotreload"></div>
When the server is shut down, this will attempt to reconnect until the server is restarted. That first successful reconnection will trigger the new server to send the reload script. After that initial reload, this handler will simply send an empty SSE connection thanks to the use of sync.Once.
Todo: Make a version that will reconnect *all* clients – maybe by
func In ¶
func In[T comparable](value T, safelist ...T) bool
In reports whether value is in the safelist.
func NewExtractor ¶
NewExtractor creates an extractor that reads the given struct tag and calls fn to extract a value from the request.
func NewValidator ¶
NewValidator adapts a boolean helper with no tag argument to a struct tag validator.
func NewValidatorWithArg ¶
NewValidatorWithArg adapts a boolean helper with one parsed tag argument to a struct tag validator. msg may contain one %s verb for the tag argument value.
func NoDuplicates ¶
func NoDuplicates[T comparable](values []T) bool
NoDuplicates reports whether all elements of values are unique.
func NonZero ¶
func NonZero[T comparable](value T) bool
func NotIn ¶
func NotIn[T comparable](value T, blocklist ...T) bool
NotIn reports whether value is not in the blocklist.
func StaticHandler ¶
StaticHandler serves an embedded static directory from within an embed.FS. For example, if you want to embed the `static` subdirectory, create an embed.FS:
//go:embed static/* var staticFiles embed.FS
Then call StaticHandler("static", "/static/", staticFiles)
To serve the files from the "static" subdirectory at the route prefix "/static/". The slashes in the prefix ensure that files are routed to the correct path within the embed.FS.
func WithExtractors ¶
func WithExtractors(extractors ...extractor) handleOption
WithExtractors returns a handle option that appends additional extractors.
func WithValidators ¶
func WithValidators(validators ...validator) handleOption
WithValidators returns a handle option that appends additional validators.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder extracts struct fields from an HTTP request using struct tags.
type Req ¶
type Req struct {
Validator
W http.ResponseWriter
R *http.Request
}
Req wraps a response writer and request for handler convenience.
func (*Req) JSONStatus ¶
JSONStatus writes v as JSON with the given status code.
type Validator ¶
type Validator struct {
Errors []string // non-field errors ("passwords don't match")
FieldErrors map[string]string // field -> error message; first error per field wins
// contains filtered or unexported fields
}
Validator collects non-field errors and per-field errors into a single place. FieldErrors keys are the external tag values (first matching extractor tag), not Go field names. For example, a field `Name string `query:"name"“ uses "name" as the key.
func (*Validator) AddError ¶
AddError appends a non-field error, and is usually called by Validator.Check.
func (*Validator) AddFieldError ¶
AddFieldError records an error for field. The first error per field wins; subsequent errors for the same field are ignored.
func (*Validator) CheckField ¶
CheckField records an error for field if ok is false. The first error per field wins; subsequent errors for the same field are ignored.