staticserve

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 12 Imported by: 5

README

staticserve

staticserve is a cache-busting HTTP handler for static files. It supports GET operations requesting the file with no encoding or gzip encoding.

Example

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/linkdata/staticserve"
)

func main() {
	mux := http.NewServeMux()
	uri, err := staticserve.Handle("app.js", []byte("console.log('hello');"), mux.Handle)
	if err != nil {
		log.Fatal(err)
	}

	// Use this URI in your HTML templates, e.g. <script src="{{.ScriptURI}}"></script>.
	fmt.Printf("script URI: %s\n", uri)

	log.Fatal(http.ListenAndServe(":8080", mux))
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var HeaderCacheControl = []string{"public, max-age=31536000, s-maxage=31536000, immutable"}
View Source
var HeaderVary = []string{"Accept-Encoding"}

Functions

func EnsurePrefixSlash

func EnsurePrefixSlash(s string) string

EnsurePrefixSlash returns s with a leading slash.

func Handle

func Handle(fpath string, data []byte, handleFn HandleFunc) (uri string, err error)

Handle creates a new StaticServe for the fpath that returns the data given. Returns the URI of the resource.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/linkdata/staticserve"
)

func main() {
	mux := http.NewServeMux()
	uri, err := staticserve.Handle("app.js", []byte("console.log('hello');"), mux.Handle)
	if err != nil {
		panic(err)
	}

	req := httptest.NewRequest(http.MethodGet, uri, nil)
	req.Header.Set("Accept-Encoding", "gzip")
	rr := httptest.NewRecorder()
	mux.ServeHTTP(rr, req)
	res := rr.Result()

	fmt.Println(res.StatusCode == http.StatusOK)
	fmt.Println(res.Header.Get("Content-Encoding"))
	fmt.Println(strings.HasPrefix(uri, "/app."))
	fmt.Println(strings.HasSuffix(uri, ".js"))
}
Output:
true
gzip
true
true

func HandleFS

func HandleFS(fsys fs.FS, handleFn HandleFunc, root string, filepaths ...string) (uris []string, err error)

HandleFS creates StaticServe handlers for the filepaths given. Returns the URI(s) of the resources. If an error occurs, the URI of the failed resource will be the empty string.

func MaybePanic

func MaybePanic(err error)

func NormalizeGET

func NormalizeGET(pattern string) string

NormalizeGET returns a method-aware ServeMux pattern.

If pattern already has a method prefix, it is returned unchanged. Otherwise GET is prepended and the path is made absolute.

func WalkDir

func WalkDir(fsys fs.FS, root string, fn func(filename string, ss *StaticServe) (err error)) (err error)

WalkDir walks the file tree rooted at root, calling fn for each file in the tree with the filename having root trimmed (e.g. "root/dir/file.ext" becomes "dir/file.ext").

Types

type HandleFunc

type HandleFunc = func(uri string, handler http.Handler)

HandleFunc matches the signature of http.ServeMux.Handle().

Handle and HandleFS pass method-aware patterns. Bare path patterns are normalized to GET.

type StaticServe

type StaticServe struct {
	Name        string // the cache-busting file name, e.g. "static/filename.1234567.js"
	ContentType string // Content-Type of the file, e.g. "application/javascript"
	Gz          []byte // gzipped data, will be unpacked as needed
}

func Must

func Must(filename string, data []byte) (ss *StaticServe)

Must calls New and panics on error.

func MustNewFS

func MustNewFS(fsys fs.FS, root string, fpaths ...string) (ssl []*StaticServe)

func New

func New(filename string, data []byte) (ss *StaticServe, err error)

New returns a StaticServe that serves the given data with a filename like 'filename.12345678.ext'. The filename must have the suffix ".gz" if the data is GZip compressed. The ".gz" suffix will not be part of the filename presented in this case.

func NewFS

func NewFS(fsys fs.FS, root, fpath string) (ss *StaticServe, err error)

NewFS reads the file at fpath from fsys and then calls New.

func (*StaticServe) ServeHTTP

func (ss *StaticServe) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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