static

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package static implements Credo's static file serving: sanitization of the router-decoded path, index and SPA fallbacks, directory listings, Range-aware file delivery, and status-aware Cache-Control. The root package owns the public StaticConfig/StaticRoute surface and the route registration; it converts its config into this package's Config and adapts the request Context to the ResponseWriter/http.Request pair the server consumes.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound reports a missing file or directory; the root package maps
	// it to its 404 error.
	ErrNotFound = errors.New("static: not found")

	// ErrBadRequest reports a malformed or unsafe path; the root package maps
	// it to its 400 error.
	ErrBadRequest = errors.New("static: bad request")
)

Functions

This section is empty.

Types

type CacheContext

type CacheContext struct {
	RequestPath string
	FilePath    string
	FileName    string
	IsHTML      bool
}

CacheContext mirrors credo.StaticCacheContext field-for-field.

type Config

type Config struct {
	Index        string
	Browse       bool
	SPA          bool
	Download     bool
	CacheControl func(CacheContext) string
}

Config mirrors credo.StaticConfig. See that type for field semantics.

type FileServer

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

FileServer serves one named file from an fs.FS.

func NewFileServer

func NewFileServer(fsys fs.FS, name string, cfg Config) *FileServer

NewFileServer returns a FileServer for name inside fsys. The name is normalized once at construction.

func (*FileServer) Serve

func (f *FileServer) Serve(w ResponseWriter, r *http.Request, requestPath string) error

Serve writes the file. Directories and missing files report ErrNotFound.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	Committed() bool
}

ResponseWriter is the subset of the root Response the server writes to.

type Server

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

Server serves files from an fs.FS under a route prefix.

func NewServer

func NewServer(fsys fs.FS, cfg Config) *Server

NewServer returns a Server for fsys with cfg.

func (*Server) Serve

func (s *Server) Serve(w ResponseWriter, r *http.Request, requestPath, filePath string) error

Serve resolves and writes filePath, the route-captured remainder ("" for the prefix itself). The router has already decoded it once, so it is a file path, not URL text: it is sanitized but never decoded again, and "%" is an ordinary character ("100%.txt"). requestPath is the client path before internal rewrites.

Jump to

Keyboard shortcuts

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