weblib

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 14 Imported by: 0

README

Weblib

A collection of helpful functions for Go web developers that use HTMX and/or Templ.

License

MIT licensed. See the LICENSE file for details.

Documentation

Index

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

func Coalesce[T any](vals ...*T) (*T, bool)

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

func GenerateNonce(size uint) (string, error)

GenerateNonce generates a random nonce of the given size in bytes and returns it as a hex encoded string.

func Gzip

func Gzip(next http.Handler) http.Handler

Gzip applies gzip compression to a response if it is an accepted encoding.

func IIF

func IIF[T any](condition bool, v1, v2 T) T

IIF is an inline if statement. if the condition is true, v1 is returned, otherwise v2 is returned.

func IsHTMX

func IsHTMX(r *http.Request) bool

IsHTMX returns true if the request is HTMX, otherwise false.

func Logger

func Logger(next http.Handler) http.Handler

Logger logs the method, path, and time taken for the given handler.

func NoBrowse

func NoBrowse(next http.Handler) http.Handler

NoBrowse prevents a browser from being able to browse a file server.

func Redirect

func Redirect(w http.ResponseWriter, r *http.Request, status int, route string)

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

func Render(w http.ResponseWriter, r *http.Request, status int, tmpls ...templ.Component) error

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

func ServeFiles(prefix, root string, browsable bool) http.Handler

ServeFiles serves files from the given root. The prefix should match the route used in your handler.

func TrimQuotes

func TrimQuotes(s string) string

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

func NewCache(ttl, cleanInterval time.Duration) *Cache

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

func (c *Cache) Delete(k string)

Delete removes the key value pair mapped to by k if it exists.

func (*Cache) Get added in v0.2.0

func (c *Cache) Get(k string) (v any)

Get retrieves the value v from the cache with key k if it exists and is not expired.

func (*Cache) Put added in v0.2.0

func (c *Cache) Put(k string, v any)

Put adds the value v to the cache with key k.

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

type Middleware func(http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

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