Documentation ¶
Overview ¶
Package statigz serves pre-compressed embedded files with http.
Index ¶
- Variables
- func EncodeOnInit(server *Server)
- func FSPrefix(prefix string) func(server *Server)
- func OnError(onErr func(rw http.ResponseWriter, r *http.Request, err error)) func(server *Server)
- func OnNotFound(onErr func(rw http.ResponseWriter, r *http.Request)) func(server *Server)
- type Encoding
- type Server
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var SkipCompressionExt = []string{".gz", ".br", ".gif", ".jpg", ".png", ".webp"}
SkipCompressionExt lists file extensions of data that is already compressed.
Functions ¶
func EncodeOnInit ¶ added in v1.1.0
func EncodeOnInit(server *Server)
EncodeOnInit enables runtime encoding for unencoded files to allow compression for uncompressed embedded files.
Enabling this option can degrade startup performance and memory usage in case of large embeddings, use with caution.
func OnNotFound ¶ added in v1.3.0
func OnNotFound(onErr func(rw http.ResponseWriter, r *http.Request)) func(server *Server)
OnNotFound is an option to customize not found (404) handling in Server.
Types ¶
type Encoding ¶
type Encoding struct { // FileExt is an extension of file with compressed content, for example ".gz". FileExt string // ContentEncoding is encoding name that is used in Accept-Encoding and Content-Encoding // headers, for example "gzip". ContentEncoding string // Decoder is a function that can decode data for an agent that does not accept encoding, // can be nil to disable dynamic decompression. Decoder func(r io.Reader) (io.Reader, error) // Encoder is a function that can encode data Encoder func(r io.Reader) ([]byte, error) }
Encoding describes content encoding.
type Server ¶
type Server struct { // OnError controls error handling during Serve. OnError func(rw http.ResponseWriter, r *http.Request, err error) // OnNotFound controls handling of not found files. OnNotFound func(rw http.ResponseWriter, r *http.Request) // Encodings contains supported encodings, default GzipEncoding. Encodings []Encoding // EncodeOnInit encodes files that does not have encoded version on Server init. // This allows embedding uncompressed files and still leverage one time compression // for multiple requests. // Enabling this option can degrade startup performance and memory usage in case // of large embeddings, use with caution. EncodeOnInit bool // FSPrefix is a path prefix shat should be ignored. // It is prepended to the incoming HTTP path. // This can help to keep static assets in a subdirectory, e.g. // //go:embed static/* // But access files from HTTP without "/static/" prefix in the path. FSPrefix string // contains filtered or unexported fields }
Server is a http.Handler that directly serves compressed files from file system to capable agents.
Please use FileServer to create an instance of Server.
If agent does not accept encoding and uncompressed file is not available in file system, it would decompress the file before serving.
Compressed files should have an additional extension to indicate their encoding, for example "style.css.gz" or "bundle.js.br".
Caching is implemented with ETag and If-None-Match headers. Range requests are supported with help of http.ServeContent.
Behavior is similar to http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html and https://github.com/lpar/gzipped, except compressed data can be decompressed for an incapable agent.
func FileServer ¶
FileServer creates an instance of Server from file system.
This function indexes provided file system to optimize further serving, so it is not recommended running it in the loop (for example for each request).
Typically, file system would be an embed.FS.
//go:embed *.png *.br var FS embed.FS
Brotli support is optionally available with brotli.AddEncoding.
Example ¶
package main import ( "embed" "io/fs" "log" "net/http" "github.com/vearutop/statigz" "github.com/vearutop/statigz/brotli" ) // Declare your embedded assets. //go:embed testdata/* var st embed.FS func main() { s, err := fs.Sub(st, "testdata") if err != nil { log.Fatal(err) } // Plug static assets handler to your server or router. err = http.ListenAndServe(":80", statigz.FileServer(s.(fs.ReadDirFS), brotli.AddEncoding)) if err != nil { log.Fatal(err) } }
Output:
func (*Server) Found ¶ added in v1.2.0
Found returns true if http.Request would be fulfilled by Server.
This can be useful for custom handling of requests to non-existent resources.
Example ¶
fileServer := statigz.FileServer(st) customHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Serve existing static resource. if fileServer.Found(r) { fileServer.ServeHTTP(w, r) return } // Do something custom for non-existing resource, for example serve index page. // (This is an example, serving index instead of 404 might not be the best idea in real life 😅). r.URL.Path = "/" fileServer.ServeHTTP(w, r) }) // Plug static assets handler to your server or router. if err := http.ListenAndServe("localhost:80", customHandler); err != nil { log.Fatal(err) }
Output:
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP serves static files.
For compatibility with std http.FileServer: if request path ends with /index.html, it is redirected to base directory; if request path points to a directory without trailing "/", it is redirected to a path with trailing "/".