static

package
v0.96.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 22 Imported by: 1

Documentation

Overview

Package static is part of the GoFastr framework. See https://github.com/DonaldMurillo/gofastr for documentation.

Package static provides a static file server for Go's embed.FS (or any fs.FS), with ETag-based caching, configurable Cache-Control headers, MIME type detection, SPA fallback, and optional directory listing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectFromName

func DetectFromName(name string) string

DetectFromName returns the MIME type for the given filename based on its extension. The built-in canonical table wins so detection is platform-independent; mime.TypeByExtension only covers the long tail of extensions not in the table, and "application/octet-stream" is the final fallback.

func FingerprintURL

func FingerprintURL(filePath, hash string) string

FingerprintURL appends a content hash to the filename portion of the URL path for cache busting. For example:

FingerprintURL("/assets/app.js", "abc123") → "/assets/app.abc123.js"

func Handler

func Handler(config Config) http.Handler

Handler returns an http.Handler that serves static files according to the given Config.

func IfNoneMatch added in v0.96.0

func IfNoneMatch(r *http.Request, etag string) bool

IfNoneMatch reports whether r's If-None-Match names etag, the RFC 9110 way: a comma-separated list, "*", and a weak comparison that ignores W/ on either side. A handler answers 304 when it does.

func Mount

func Mount(r *router.Router, config Config)

Mount registers the static file handler on the given router using the Config's Prefix as the path pattern.

func Refuses added in v0.96.0

func Refuses(p string) bool

Refuses reports whether the handler refuses p, a slash-separated path below the served root: one with a dot segment (.env, .git/config), a well-known server config file (web.config), or a Windows 8.3 short name (WEB~1.CON). Code that serves or publishes a file tree by another route calls it to refuse the same paths.

Types

type Config

type Config struct {
	// FS is the filesystem to serve files from (e.g. an embed.FS).
	// When the backing store is disk-based (os.DirFS), requests are
	// opened through an *os.Root over the resolved root, so a symlink
	// pointing outside the root — including one swapped in mid-request —
	// is refused by the kernel at Open time. embed.FS cannot carry
	// symlinks and is unaffected.
	FS fs.FS
	// Prefix is the URL path prefix to strip when mapping to filesystem paths.
	// For example, with Prefix="/static", a request for "/static/app.js"
	// serves "app.js" from FS.
	Prefix string

	// MaxAge is the default cache duration for Cache-Control headers.
	// Zero means "no-cache".
	MaxAge time.Duration

	// IndexFile is the name of the default file to serve for directory paths.
	// Defaults to "index.html".
	IndexFile string

	// SPA enables single-page application mode. When true, a request
	// that matches no file is a client route (/dashboard/settings,
	// /users/42, /u/jane.doe, /blog/v1.2) and is answered with IndexFile
	// instead of 404, unless its last path segment ends in a well-known
	// asset extension, in any case: .js .mjs .css .map, .png .jpg .jpeg
	// .gif .svg .webp .avif .ico, .woff .woff2 .ttf, .json .wasm .txt
	// .xml. A missing asset (/app.3f2a.js, /x.css,
	// /logo.png) is a 404: the index in its place is HTML a browser
	// would run as a script or a stylesheet, and a cache would store it
	// under the asset's URL.
	SPA bool

	// NotFoundFile, when set, names a file in FS whose body answers a
	// request that matches no file, with status 404 (a static export's
	// "404.html"). Ignored in SPA mode, which answers misses with
	// IndexFile. When the file itself is missing the plain 404 stands.
	NotFoundFile string

	// DirListing is reserved for a future release.
	// When implemented, enabling it will render an HTML directory listing
	// instead of returning 404 for directory paths that lack an index file.
	// Do not set this field. It is currently ignored.
	DirListing bool
	// contains filtered or unexported fields
}

Config holds the configuration for serving static files.

Jump to

Keyboard shortcuts

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