Documentation ¶
Overview ¶
Package static provides a static file HTTP handler that sends content-based cache headers.
Example ¶
package main import ( "net/http" "os" "zombiezen.com/go/bass/static" ) func main() { // Create a static handler with the desired file system. // In this case, we use a directory on the local filesystem. staticHandler := static.NewHandler(os.DirFS("path/to/dir")) // Serve the handler under the prefix "/dir/". // We use the http.StripPrefix function to avoid "/dir/foo.txt" requesting // "path/to/dir/dir/foo.txt". http.Handle("/dir/", http.StripPrefix("/dir", staticHandler)) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is an HTTP handler for a file system.
func NewHandler ¶
NewHandler returns a new Handler that serves the given file system.
func (*Handler) ServeFile ¶
ServeFile serves the given file from the Handler's file system. It primarily uses net/http.ServeContent, but sets a content-based ETag first.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the file named by the request's path from the Handler's file system.
func (*Handler) SetErrorFunc ¶
SetErrorFunc sets the error callback for the Handler. The function is responsible for logging the error and returns the error string that should be sent back in response. The default error callback returns the error's message.
SetErrorFunc must not be called concurrently with ServeHTTP.