fileServer

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: BSD-3-Clause Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultNotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		http.Error(w, "Not Found", http.StatusNotFound)
	})
	DefaultForbiddenHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		http.Error(w, "Forbidden", http.StatusForbidden)
	})
	DefaultInternalServerErrorHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
	})
)

Default file handlers in case of errors.

Functions

This section is empty.

Types

type Hasher

type Hasher interface {
	Hash(io.Reader) (string, error)
	IsHash(string) bool
}

Hasher defines an interface for hashing file paths.

type MD5Hasher

type MD5Hasher struct {
	HashLength int
}

MD5Hasher uses MD5 sum to compute a file hash.

func (MD5Hasher) Hash

func (s MD5Hasher) Hash(reader io.Reader) (string, error)

Hash returns a part of a MD5 sum of a file.

func (MD5Hasher) IsHash

func (s MD5Hasher) IsHash(h string) bool

IsHash checks is provided string a valid hash.

type Options

type Options struct {
	// If Hasher is not nil, it will be used to compute a file hash.
	Hasher Hasher
	// Do not hash file paths if the request contains query parameters.
	// It may be used if other tools construct a request with predefined
	// bust parameter.
	NoHashQueryStrings bool
	// Redirect trailing slash of HTTP requests.
	RedirectTrailingSlash bool
	// IndexPage is a filename that will be used to render an index page
	// for a directory.
	IndexPage string
	// AltDir is a directory path to look for files before the
	// initialized directory.
	AltDir string
	// Filesystem is optional and is used for opening files. In case of
	// nil, os.Open will be used to open files.
	Filesystem http.FileSystem
	// Filenames are used to find corresponding files with hashes in them
	// by just iterating through them instead doing filesystem operations.
	Filenames []string

	// NotFoundHandler is used when no file can be found.
	NotFoundHandler http.Handler
	// ForbiddenHandler is used when permissions to open file are insufficient.
	ForbiddenHandler http.Handler
	// InternalServerErrorHandler is used when an unexpected error occurs.
	InternalServerErrorHandler http.Handler
}

Options contains parameters for file server configuration.

type Server

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

Server is a HTTP Handler tha serves static files from local filesystem.

func New

func New(root, dir string, options *Options) *Server

New initializes a new instance of Server.

func (*Server) HashedPath

func (s *Server) HashedPath(p string) (string, error)

HashedPath returns a URL path with hash injected into the filename.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP writes static files content to HTTP response.

Jump to

Keyboard shortcuts

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