assets

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package assets assists serving embedded assets via HTTP

Index

Constants

View Source
const DefaultJSONIndent = "\t"

DefaultJSONIndent is the indentation used by NewJSONAssetHandler when serializing the data.

Variables

This section is empty.

Functions

func BLAKE3Sum

func BLAKE3Sum(data []byte) string

BLAKE3Sum calculates a BLAKE3-256 check from the given content and returns its base64 representation.

func BLAKE3SumFile

func BLAKE3SumFile(file io.Reader) (string, error)

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

func CleanETags(tags ...string) []string

CleanETags removes empty and duplicate tags. Whitespace before and after the content of each tag is also removed.

func ContentType

func ContentType(v any) string

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

func DefaultRequestResolver(req *http.Request) (string, error)

DefaultRequestResolver returns the requested path

func ETags

func ETags(v any) []string

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

func Match(path string, globs []fs.Matcher) bool

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

func MustJSONAssetHandler[T any](d time.Duration, v T) http.Handler

MustJSONAssetHandler creates an AssetHandler serving a constant JSON resource, and panics if marshalling fails.

func NewJSONAssetHandler added in v0.7.0

func NewJSONAssetHandler[T any](d time.Duration, v T) (http.Handler, error)

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

func TypeByFilename(fileName string) string

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

type ContentTypeSetter interface {
	SetContentType(string) string
}

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

type ContentTypedFS interface {
	fs.FS

	ContentType(path string) (string, error)
}

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 ETagedFS

type ETagedFS interface {
	fs.FS

	ETags(path string) ([]string, error)
}

ETagedFS is the interface implemented by a file system

type ETagsSetter

type ETagsSetter interface {
	SetETags(...string) []string
}

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

type ETagsSetterFS interface {
	fs.FS

	SetETags(string, ...string) ([]string, error)
}

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

func NewEmbedFS(base *embed.FS, root string, patterns ...string) (*EmbedFS, error)

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

func (o *EmbedFS) ContentType(name string) (string, error)

ContentType returns the MIME Content-Type of the named file.

func (*EmbedFS) ETags added in v0.4.2

func (o *EmbedFS) ETags(name string) ([]string, error)

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

func (o *EmbedFS) Glob(pattern string) ([]string, error)

Glob returns a list of files matching the given pattern. github.com/gobwas/glob patterns supported.

func (*EmbedFS) Middleware added in v0.4.2

func (o *EmbedFS) Middleware() func(http.Handler) http.Handler

Middleware returns a middleware handler to be used with this EmbedFS, allowing us to proceed when the requested file wasn't found.

func (*EmbedFS) Open added in v0.4.2

func (o *EmbedFS) Open(name string) (fs.File, error)

Open opens the named file from the file system.

func (*EmbedFS) ReadFile added in v0.4.2

func (o *EmbedFS) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns its contents.

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

func (o *EmbedFS) SetContentType(name, contentType string) (string, error)

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

func (o *EmbedFS) SetETags(name string, tags ...string) ([]string, error)

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

func (o *EmbedFS) SetResolver(r func(*http.Request) (string, error))

SetResolver provides an optional function to identify the requested resource name.

func (*EmbedFS) Stat added in v0.4.2

func (o *EmbedFS) Stat(name string) (fs.FileInfo, error)

Stat returns a [fs.FileInfo] describing the named file from the file system.

func (*EmbedFS) Sub added in v0.4.2

func (o *EmbedFS) Sub(dir string) (fs.FS, error)

Sub creates a view of the file system restricted to a particular sub-directory. It will fail with [fs.ErrNotExist] if there are no files in it.

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

func (fd *EmbedFile) Close() error

Close implements the [fs.File] interface, but doesn't really do anything.

func (*EmbedFile) ContentType added in v0.4.2

func (fd *EmbedFile) ContentType() string

ContentType returns the MIME Content-Type of the file.

func (*EmbedFile) ETags added in v0.4.2

func (fd *EmbedFile) ETags() []string

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

func (fd *EmbedFile) Read(b []byte) (int, error)

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

func (fd *EmbedFile) Seek(offset int64, whence int) (int64, error)

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

func (fd *EmbedFile) SetContentType(contentType string) string

SetContentType sets the MIME Content-Type of the file. If none is provided, it will just return the current value.

func (*EmbedFile) SetETags added in v0.4.2

func (fd *EmbedFile) SetETags(tags ...string) []string

SetETags sets the ETags for the file. Previous values will be discarded. If none is provided, it will just return the current value.

func (*EmbedFile) Stat added in v0.4.2

func (fd *EmbedFile) Stat() (fs.FileInfo, error)

Stat implements the [fs.File] interface returning the EmbedMeta associated with this descriptor.

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

func (fm *EmbedMeta) ContentType() string

ContentType returns the MIME Content-Type of the file

func (*EmbedMeta) ETags added in v0.4.2

func (fm *EmbedMeta) ETags() []string

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

func (fm *EmbedMeta) Info() (fs.FileInfo, error)

Info implements the [fs.DirEntry] interface returning itself as [fs.FileInfo]

func (*EmbedMeta) Name added in v0.4.2

func (fm *EmbedMeta) Name() string

Name returns the name of the File in the directory.

func (*EmbedMeta) Path added in v0.4.2

func (fm *EmbedMeta) Path() string

Path returns the full path to the File in the base [fs.FS]

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

func (fm EmbedMeta) SetContentType(contentType string) string

SetContentType sets the MIME Content-Type of the file. If none is provided, it will just return the current value.

func (EmbedMeta) SetETags added in v0.4.2

func (fm EmbedMeta) SetETags(tags ...string) []string

SetETags sets the ETags of the file. Previous values will be discarded. If none is provided, it will just return the current values.

func (*EmbedMeta) Type added in v0.4.2

func (fm *EmbedMeta) Type() fs.FileMode

Type implements the [fs.DirEntry] interface as an alias of [EmbedMeta.Mode].

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.

func MustFS added in v0.4.2

func MustFS(base fs.FS, root string, patterns ...string) FS

MustFS is equivalent to NewFS but panics on error.

func NewFS added in v0.4.2

func NewFS(base fs.FS, root string, patterns ...string) (FS, error)

NewFS creates a new assets FS from the given [fs.FS], optional root, and an list of patterns. github.com/gobwas/glob patterns supported.

type File

type File interface {
	fs.File
	io.ReadSeekCloser
}

File represents seek-able [fs.File]

Jump to

Keyboard shortcuts

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