Documentation
¶
Overview ¶
Package assets assists serving embedded assets via HTTP
Index ¶
- Constants
- func BLAKE3Sum(data []byte) string
- func BLAKE3SumFile(file io.Reader) (string, error)
- func CleanETags(tags ...string) []string
- func ContentType(v any) string
- func DefaultRequestResolver(req *http.Request) (string, error)
- func ETags(v any) []string
- func Match(path string, globs []fs.Matcher) bool
- func MustJSONAssetHandler[T any](d time.Duration, v T) http.Handler
- func NewJSONAssetHandler[T any](d time.Duration, v T) (http.Handler, error)
- func ServeFile(rw http.ResponseWriter, req *http.Request, file File)
- func TypeByFilename(fileName string) string
- func TypeBySniffing(file io.ReadSeeker) (string, error)
- type Asset
- type AssetHandler
- type ContentTypeSetter
- type ContentTypeSetterFS
- type ContentTyped
- type ContentTypedFS
- type ETaged
- type ETagedFS
- type ETagsSetter
- type ETagsSetterFS
- type EmbedFS
- func (o *EmbedFS) ContentType(name string) (string, error)
- func (o *EmbedFS) ETags(name string) ([]string, error)
- func (o *EmbedFS) Glob(pattern string) ([]string, error)
- func (o *EmbedFS) Middleware() func(http.Handler) http.Handler
- func (o *EmbedFS) Open(name string) (fs.File, error)
- func (o *EmbedFS) ReadFile(name string) ([]byte, error)
- func (o *EmbedFS) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (o *EmbedFS) SetContentType(name, contentType string) (string, error)
- func (o *EmbedFS) SetETags(name string, tags ...string) ([]string, error)
- func (o *EmbedFS) SetResolver(r func(*http.Request) (string, error))
- func (o *EmbedFS) Stat(name string) (fs.FileInfo, error)
- func (o *EmbedFS) Sub(dir string) (fs.FS, error)
- type EmbedFile
- func (fd *EmbedFile) Close() error
- func (fd *EmbedFile) ContentType() string
- func (fd *EmbedFile) ETags() []string
- func (fd *EmbedFile) Read(b []byte) (int, error)
- func (fd *EmbedFile) Seek(offset int64, whence int) (int64, error)
- func (fd *EmbedFile) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (fd *EmbedFile) SetContentType(contentType string) string
- func (fd *EmbedFile) SetETags(tags ...string) []string
- func (fd *EmbedFile) Stat() (fs.FileInfo, error)
- type EmbedMeta
- func (fm *EmbedMeta) ContentType() string
- func (fm *EmbedMeta) ETags() []string
- func (fm *EmbedMeta) Info() (fs.FileInfo, error)
- func (fm *EmbedMeta) Name() string
- func (fm *EmbedMeta) Path() string
- func (fm *EmbedMeta) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (fm EmbedMeta) SetContentType(contentType string) string
- func (fm EmbedMeta) SetETags(tags ...string) []string
- func (fm *EmbedMeta) Type() fs.FileMode
- type FS
- type File
Constants ¶
const DefaultJSONIndent = "\t"
DefaultJSONIndent is the indentation used by NewJSONAssetHandler when serializing the data.
Variables ¶
This section is empty.
Functions ¶
func BLAKE3Sum ¶
BLAKE3Sum calculates a BLAKE3-256 check from the given content and returns its base64 representation.
func BLAKE3SumFile ¶
BLAKE3SumFile calculates a BLAKE3-256 checksum for the file. If io.Seeker is implemented, the file will be rewind.
func CleanETags ¶ added in v0.4.1
CleanETags removes empty and duplicate tags. Whitespace before and after the content of each tag is also removed.
func ContentType ¶
ContentType checks if the [fs.File] or its [fs.FileInfo] effectively provides the Content-Type, and returns it if so.
func DefaultRequestResolver ¶ added in v0.4.2
DefaultRequestResolver returns the requested path
func ETags ¶
ETags checks the [fs.File] and [fs.FileInfo] to see if they implement the ETaged interface, and return their output.
func Match ¶ added in v0.4.1
Match tests if the given path matches any of the given globs. if no matchers are provided, it will be understood as unconditional.
func MustJSONAssetHandler ¶ added in v0.7.0
MustJSONAssetHandler creates an AssetHandler serving a constant JSON resource, and panics if marshalling fails.
func NewJSONAssetHandler ¶ added in v0.7.0
NewJSONAssetHandler creates an AssetHandler serving a constant JSON resource.
func ServeFile ¶
func ServeFile(rw http.ResponseWriter, req *http.Request, file File)
ServeFile serves the contents of the given File.
func TypeByFilename ¶
TypeByFilename attempts to infer the ContentType from the filename. It will return "" if unknown.
func TypeBySniffing ¶
func TypeBySniffing(file io.ReadSeeker) (string, error)
TypeBySniffing looks at the first 512 bytes of data to attempt to determine ContentType.
Types ¶
type Asset ¶ added in v0.7.0
type Asset interface {
Content() io.ReadSeeker
}
Asset represents an in-memory object that doesn't change
type AssetHandler ¶ added in v0.7.0
type AssetHandler struct { Asset Asset // contains filtered or unexported fields }
AssetHandler serves an Asset. Only Content() is required, but the object is also tested for:
* ContentType() string * ETags() []string * Stat() (fs.FileInfo, error) * Info() (fs.FileInfo, error) * ModTime() time.Time * Header() http.Header
AssetHandler supports: * 405 * 406 * ETag * Last-Modified * Ranges
func (*AssetHandler) ServeHTTP ¶ added in v0.7.0
func (h *AssetHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
type ContentTypeSetter ¶
ContentTypeSetter is the interface that allows a [fs.File] or its [fs.FileInfo] to assign a MIME Content-Type to it. If empty it will be ignored. On success the new value is returned.
type ContentTypeSetterFS ¶
type ContentTypeSetterFS interface { fs.FS SetContentType(path, contentType string) (string, error) }
ContentTypeSetterFS is the interface implemented by a file system to set a file's MIME Content-Type. On success the new value is returned.
type ContentTyped ¶
type ContentTyped interface {
ContentType() string
}
ContentTyped allows files to declare their Content-Type.
type ContentTypedFS ¶
ContentTypedFS is the interface implemented by a file system to get a file's MIME Content-Type
type ETaged ¶
type ETaged interface {
ETags() []string
}
ETaged allows files to declare their ETag to enable proper caching.
type ETagsSetter ¶
ETagsSetter is the interface that allows a [fs.File] or its [fs.FileInfo] to assign a ETags to it. Any previous value will be replaced, unless none is provided. The effective ETags set is returned.
type ETagsSetterFS ¶
ETagsSetterFS is the interface implemented by a file system to set a file's ETags. On success the new tags is returned.
type EmbedFS ¶ added in v0.4.2
type EmbedFS struct {
// contains filtered or unexported fields
}
EmbedFS extends embed.FS for serving static assets.
func NewEmbedFS ¶ added in v0.4.2
NewEmbedFS creates an assets FS from the given embed.FS, optional root, and an optional list of patterns. github.com/gobwas/glob patterns supported.
func (*EmbedFS) ContentType ¶ added in v0.4.2
ContentType returns the MIME Content-Type of the named file.
func (*EmbedFS) ETags ¶ added in v0.4.2
ETags returns the the ETags associated to the named file. BLAKE3-256 of the content will be calculated if none has been set already
func (*EmbedFS) Glob ¶ added in v0.4.2
Glob returns a list of files matching the given pattern. github.com/gobwas/glob patterns supported.
func (*EmbedFS) Middleware ¶ added in v0.4.2
Middleware returns a middleware handler to be used with this EmbedFS, allowing us to proceed when the requested file wasn't found.
func (*EmbedFS) ServeHTTP ¶ added in v0.4.2
func (o *EmbedFS) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP directly implements the http.Handler interface
func (*EmbedFS) SetContentType ¶ added in v0.4.2
SetContentType sets the MIME Content-Type of the named file. If none is provided, it will just return the current value.
func (*EmbedFS) SetETags ¶ added in v0.4.2
SetETags sets the ETags for the named file. Previous values will be discarded. If none is provided, it will just return the current value.
func (*EmbedFS) SetResolver ¶ added in v0.4.2
SetResolver provides an optional function to identify the requested resource name.
type EmbedFile ¶ added in v0.4.2
type EmbedFile struct {
// contains filtered or unexported fields
}
EmbedFile is a readable instance of an embedded static file.
func (*EmbedFile) Close ¶ added in v0.4.2
Close implements the [fs.File] interface, but doesn't really do anything.
func (*EmbedFile) ContentType ¶ added in v0.4.2
ContentType returns the MIME Content-Type of the file.
func (*EmbedFile) ETags ¶ added in v0.4.2
ETags returns the the ETags associated to the file. BLAKE3-256 of the content will be calculated if none has been set already
func (*EmbedFile) Read ¶ added in v0.4.2
Read reads up to len(b) bytes from the File and stores them in b. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.
func (*EmbedFile) Seek ¶ added in v0.4.2
Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.
func (*EmbedFile) ServeHTTP ¶ added in v0.4.2
func (fd *EmbedFile) ServeHTTP(rw http.ResponseWriter, req *http.Request)
func (*EmbedFile) SetContentType ¶ added in v0.4.2
SetContentType sets the MIME Content-Type of the file. If none is provided, it will just return the current value.
type EmbedMeta ¶ added in v0.4.2
type EmbedMeta struct {
// contains filtered or unexported fields
}
EmbedMeta contains all information we know about the embedded assets
func (*EmbedMeta) ContentType ¶ added in v0.4.2
ContentType returns the MIME Content-Type of the file
func (*EmbedMeta) ETags ¶ added in v0.4.2
ETags returns the the ETags associated to the file. BLAKE3-256 of the content will be calculated if none has been set already
func (*EmbedMeta) Info ¶ added in v0.4.2
Info implements the [fs.DirEntry] interface returning itself as [fs.FileInfo]
func (*EmbedMeta) ServeHTTP ¶ added in v0.4.2
func (fm *EmbedMeta) ServeHTTP(rw http.ResponseWriter, req *http.Request)
func (EmbedMeta) SetContentType ¶ added in v0.4.2
SetContentType sets the MIME Content-Type of the file. If none is provided, it will just return the current value.
type FS ¶ added in v0.4.2
type FS interface { // FS is a standard Go file system fs.FS // ServeHTTP handles requests as normal and fails with a standard 404 http.Handler // Middleware handles requests for known files and pass over the rest Middleware() func(http.Handler) http.Handler // SetResolver sets a helper that will extract the request path from // the [http.Request]. by default req.URL.Path will be used. SetResolver(func(*http.Request) (string, error)) }
FS represents an HTTP capable file system.