static

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 13 Imported by: 0

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. It first tries mime.TypeByExtension, then falls back to a built-in mapping, and finally returns "application/octet-stream" if no match is found.

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 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.

Types

type Config

type Config struct {
	// FS is the filesystem to serve files from (e.g. an embed.FS).
	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, requests for
	// paths that don't match any file will serve IndexFile instead of 404.
	SPA bool

	// 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
}

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