static

package
v0.0.0-...-0399f01 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

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

func NewHandler(fsys fs.FS) *Handler

NewHandler returns a new Handler that serves the given file system.

func (*Handler) ServeFile

func (h *Handler) ServeFile(w http.ResponseWriter, r *http.Request, path string)

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

func (h *Handler) SetErrorFunc(f func(ctx context.Context, path string, err error) string)

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.

Jump to

Keyboard shortcuts

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