static

package
v2.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package static provides a static-file handler for httpsrv.

New returns an httpsrv.Handler that serves a whole file tree when registered on a catch-all route — the "/*" wildcard or a named {*name}. root is a directory path, or — with Config.FS set — a sub-path within that fs.FS:

app.Get("/*", static.New("./public"))
// GET /css/main.css  ->  ./public/css/main.css

app.Get("/static/*", static.New("./assets"))
// GET /static/css/main.css  ->  ./assets/css/main.css

// embedded filesystem
app.Get("/*", static.New(".", static.Config{FS: embedFS}))

The path remainder is read via r.PathValue("*") (the router exposes it under "*" for both /* and {*name}).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(root string, config ...Config) (h httpsrv.Handler)

New returns a handler that serves static files. root is a directory path served via http.Dir (which prevents path escape); when Config.FS is set, root is instead a sub-path within that filesystem.

Any setup failure — an empty root, an invalid Config.FS, or an unexpected panic — is logged via slog and degrades to an empty filesystem that responds 404 to every request, instead of crashing the process.

Register it on a catch-all route to serve a file tree — the unnamed "/*" wildcard or a named {*name} — e.g. app.Get("/*", static.New("./public")) or app.Get("/static/*", static.New("./assets")). The path remainder is read via r.PathValue("*").

Types

type Config

type Config struct {
	// FS is the filesystem (io/fs) to serve files from, e.g. an embed.FS or
	// os.DirFS(root). When nil, root is served from the real filesystem via
	// http.Dir. When set, root is interpreted as a sub-path within FS (so
	// New("dist", Config{FS: embedFS}) serves the "dist" subtree).
	FS fs.FS
}

Config configures a static handler.

Jump to

Keyboard shortcuts

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