asset

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package asset serves a bounded set of application assets from an fs.FS.

Set validates and reads every file at construction time. Requests therefore never touch the source filesystem, and callers can replace Set with any ordinary http.Handler and URL resolver when local embedding no longer fits.

Index

Constants

View Source
const (

	// DefaultMaxFiles is the zero-config inventory limit.
	DefaultMaxFiles = 256
	// DefaultMaxFileBytes is the zero-config per-file limit.
	DefaultMaxFileBytes int64 = 8 << 20
	// DefaultMaxTotalBytes is the zero-config total inventory limit.
	DefaultMaxTotalBytes int64 = 32 << 20
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Root string

	// URLPrefix is an absolute, canonical path without a trailing slash. It
	// defaults to /assets.
	URLPrefix string

	MaxFiles      int
	MaxFileBytes  int64
	MaxTotalBytes int64
}

Config describes one immutable in-memory asset set. Zero limits select conservative defaults: 256 files, 8 MiB per file, and 32 MiB total.

type Set

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

Set is a validated collection of application-owned assets. It is safe for concurrent use after New returns.

func New

func New(files fs.FS, config Config) (*Set, error)

New validates root and reads its complete file inventory into a Set. Directories and filenames must use portable, canonical paths. HTML, SVG, XML, and other unlisted types are rejected instead of being served as same-origin active content or relying on platform MIME databases.

func (*Set) ServeHTTP

func (set *Set) ServeHTTP(response http.ResponseWriter, request *http.Request)

ServeHTTP serves GET and HEAD requests below the configured URL prefix. Successful representations always use strong SHA-256 validators and require cache revalidation. http.ServeContent supplies standard conditional and byte range semantics over the immutable in-memory bytes.

func (*Set) URL

func (set *Set) URL(logical string) (string, error)

URL resolves a logical asset name to its stable same-origin URL. It returns an error for invalid or unknown names so template rendering fails before a response is committed.

Jump to

Keyboard shortcuts

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