Documentation
¶
Index ¶
- func Chain(next http.Handler, middlewares ...Middleware) http.Handler
- func Coalesce[T any](vals ...*T) (*T, bool)
- func ConditionalRender(w http.ResponseWriter, r *http.Request, status int, ...) error
- func Default[T comparable](vals ...T) (T, bool)
- func GenerateNonce(size uint) (string, error)
- func Gzip(next http.Handler) http.Handler
- func IIF[T any](condition bool, v1, v2 T) T
- func IsHTMX(r *http.Request) bool
- func Logger(next http.Handler) http.Handler
- func NoBrowse(next http.Handler) http.Handler
- func Redirect(w http.ResponseWriter, r *http.Request, status int, route string)
- func Render(w http.ResponseWriter, r *http.Request, status int, tmpls ...templ.Component) error
- func ServeFiles(prefix, root string, browsable bool) http.Handler
- func TrimQuotes(s string) string
- type Cache
- type File
- type Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
func Chain(next http.Handler, middlewares ...Middleware) http.Handler
Chain applies the given middlewares to the next handler in the given order and returns it.
func Coalesce ¶
Coalesce returns the first non nil value and true if it exists, otherwise nil and false.
func ConditionalRender ¶
func ConditionalRender(w http.ResponseWriter, r *http.Request, status int, page, partial templ.Component) error
ConditionalRender checks if the request is HTMX and conditionally renders the partial templ component, otherwise renders the page templ component.
This function can be helpful in the event that a page is navigated to via the browser address bar instead of a HTMX boosted anchor element.
func Default ¶
func Default[T comparable](vals ...T) (T, bool)
Default returns the first non-zero value and true if it exists, otherwise a zero value and false.
func GenerateNonce ¶
GenerateNonce generates a random nonce of the given size in bytes and returns it as a hex encoded string.
func IIF ¶
IIF is an inline if statement. if the condition is true, v1 is returned, otherwise v2 is returned.
func Redirect ¶
Redirect checks if the request is HTMX and adds the Hx-Redirect header and reassigns the status to 200 OK. Otherwise, a standard HTTP redirect is performed with the given status and route.
The status is changed to 200 OK if the request is HTMX due to the way that HTMX handles redirects. HTMX does not see 3xx status redirects and so requires a 2xx status. More info: https://github.com/bigskysoftware/htmx/issues/2052#issuecomment-1979805051
func Render ¶
Render writes any number of templ components to the response writer with the given status.
Rendering multiple components can be especially helpful when using HTMX out of band swaps: https://htmx.org/attributes/hx-swap-oob/
func ServeFiles ¶
ServeFiles serves files from the given root. The prefix should match the route used in your handler.
func TrimQuotes ¶
TrimQuotes trims single, double, or backtick quotes from a string and returns it.
Types ¶
type Cache ¶ added in v0.2.0
type Cache struct {
// contains filtered or unexported fields
}
func NewCache ¶ added in v0.2.0
NewCache returns a new in memory only cache that self cleans expired data at the given cleanInterval. The expiry of an item in the cache is determined by the ttl.
func (*Cache) Close ¶ added in v0.2.0
func (c *Cache) Close()
Close signals for the cache to gracefully stop the goroutine that periodically cleans expired data.
func (*Cache) Delete ¶ added in v0.2.0
Delete removes the key value pair mapped to by k if it exists.
type File ¶
type File struct {
Header multipart.FileHeader
Path string
Directory string
Filename string
}
func ExtractFullPath ¶
func ExtractFullPath(fileheader *multipart.FileHeader) (*File, error)
ExtractFullPath extracts the full relative path of an individual file submitted by a multipart form containing a file input with the directory attributes enabled.
E.g., <input type="file" webkitdirectory directory/>
The output is sanitised to prevent path traversal and invalid characters. Note: Some browsers may sanitise the filename to include only the base filename (e.g., "file.txt"). The Directory field may be empty or "." in such cases. Test with target browsers to confirm behaviour.
type Middleware ¶
func WithNonce ¶
func WithNonce(size uint) Middleware
WithNonce returns a middleware closure that generates random nonces of the given size in bytes and sets the Content-Security-Policy response header in addition to setting it in the context for automatice usage by templ.