middleware

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: GPL-3.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultExtensions = [...]string{".txt", ".html", ".css", ".js", ".json", ".webmanifest", ".xml", ".ico"}
	ErrCacheDisabled  = errors.New("cache disabled")
)

Functions

func FileServerHandler

func FileServerHandler(root fs.FS) http.HandlerFunc

func GenerateHashString

func GenerateHashString(data []byte) string

GenerateHashString generate a hash with sha256 from data

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache(logger *log.Logger, dir string, ttl time.Duration, extensions []string, contents ...Content) (*Cache, error)

func (*Cache) DisableFileCache

func (c *Cache) DisableFileCache()

func (*Cache) DumpStatus

func (c *Cache) DumpStatus() string

func (*Cache) EnableFileCache

func (c *Cache) EnableFileCache()

func (*Cache) Handler

func (c *Cache) Handler() http.Handler

func (*Cache) NewContent

func (c *Cache) NewContent(content Content) error

func (*Cache) ServeContent

func (c *Cache) ServeContent(w http.ResponseWriter, r *http.Request, uri string)

func (*Cache) ServeStatic

func (c *Cache) ServeStatic(w http.ResponseWriter, r *http.Request)

func (*Cache) SetFileCacheTTL

func (c *Cache) SetFileCacheTTL(ttl time.Duration)

func (*Cache) UpdateCache

func (c *Cache) UpdateCache() error

func (*Cache) UpdateContent

func (c *Cache) UpdateContent(uri string) error

type Content

type Content interface {
	URI() string
	Name() string
	Info() (ContentInfo, error)
	Reader() (io.ReadSeekCloser, error)
}

func NewCachedFile

func NewCachedFile(uri string, dir string, path string) Content

type ContentInfo

type ContentInfo struct {
	Modtime time.Time
	Size    int
}

type CookieManager

type CookieManager struct {
	// contains filtered or unexported fields
}

func GetCookieManager

func GetCookieManager(r *http.Request) *CookieManager

func NewCookieManager

func NewCookieManager(hashKey []byte, blockKey []byte, sz securecookie.Serializer) (*CookieManager, error)

func (*CookieManager) DeleteCookie

func (cm *CookieManager) DeleteCookie(w http.ResponseWriter, name string)

DeleteCookie instantly removes a cookie with the given name before set with route.SetCookie or route.SetCookiePerm

func (*CookieManager) GetCookie

func (cm *CookieManager) GetCookie(r *http.Request, name string, value any) error

DecodeCookie decodes a previously set cookie with the given name using the method route.SetCookie.

If the cookie was not found, it will return false and the relative error (probably an http.ErrNoCookie), otherwise it will return true and, possibly, the decode error. It happends when:

  • the server was restarted, so the keys used for decoding are different
  • you provided the wrong value type
  • the cookie was not set by the server

The argument value must be a pointer, otherwise the value will not be returned. A workaround might be using the type parametric function server.DecodeCookie

func (*CookieManager) GetCookiePerm

func (cm *CookieManager) GetCookiePerm(r *http.Request, name string, value any) error

DecodeCookiePerm decodes a previously set cookie with the given name using the method route.SetCookiePerm.

If the cookie was not found, it will return false and the relative error (probably an http.ErrNoCookie), otherwise it will return true and, possibly, the decode error. It happends when:

  • you provided the wrong value type
  • the cookie was not set by the server

The argument value must be a pointer, otherwise the value will not be returned. A workaround might be using the type parametric function server.DecodeCookiePerm

func (*CookieManager) SetCookie

func (cm *CookieManager) SetCookie(w http.ResponseWriter, name string, value any, maxAge int, opts ...CookieOption) error

SetCookie creates a new cookie with the given name and value, maxAge can be used to sex the expiration date:

  • maxAge = 0 means no expiration specified
  • maxAge > 0 sets the expiration date from the current date adding the given time in seconds (- maxAge < 0 will remove the cookie instantly, like route.DeleteCookie)

The cookie value is encoded and encrypted using a pair of keys created randomly at server creation, so if the same cookie is later decoded between server restart, it can't be decoded. To have such a behaviour see SetCookiePerm.

The encoding of the value is managed by the package encoding/gob. If you are just encoding and decoding plain structs and each field type is a primary type or a struct (with the same rules), nothing should be done, but if you are dealing with interfaces, you must first register every concrete structure or type implementing that interface before encoding or decoding

func (*CookieManager) SetCookiePerm

func (cm *CookieManager) SetCookiePerm(w http.ResponseWriter, name string, value any, maxAge int, opts ...CookieOption) error

SetCookiePerm creates a new cookie with the given name and value, maxAge can be used to sex the expiration date:

  • maxAge = 0 means no expiration specified
  • maxAge > 0 sets the expiration date from the current date adding the given time in seconds (- maxAge < 0 will remove the cookie instantly, like route.DeleteCookie)

The cookie value is encoded and encrypted using a pair of keys at package level that MUST be set at program startup. This differs for the method route.SetCookie to ensure that even after server restart these cookies can still be decoded.

type CookieOption

type CookieOption func(cookie *http.Cookie)

func CookieDomainOpt

func CookieDomainOpt(value string) CookieOption

func CookieExpiresOpt

func CookieExpiresOpt(value time.Time) CookieOption

func CookieHTTPOnlyOpt

func CookieHTTPOnlyOpt(value bool) CookieOption

func CookiePathOpt

func CookiePathOpt(value string) CookieOption

func CookieSameSiteOpt

func CookieSameSiteOpt(value http.SameSite) CookieOption

func CookieSecureOpt

func CookieSecureOpt(value bool) CookieOption

Jump to

Keyboard shortcuts

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