Documentation
¶
Overview ¶
Package fileserver provides an HTTP handler for serving static files from a filesystem directory with support for pre-compressed files (gzip and Brotli).
This is particularly useful for serving Unity WebGL builds and other large assets that benefit from pre-compression, where on-the-fly compression is not viable due to file size.
When a request comes in, the handler checks if the client supports Brotli or gzip (via Accept-Encoding header) and looks for pre-compressed versions of the file (.br or .gz suffix). If found, it serves the compressed file with the appropriate Content-Encoding header, allowing the browser to decompress natively during download.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
Handler returns an http.Handler that serves static files from the given root directory with support for pre-compressed files.
The urlPrefix is stripped from the request URL before looking up files. For example, if urlPrefix is "/static" and the request is for "/static/js/app.js", the handler looks for "js/app.js" in the root directory.
Pre-compressed file lookup:
- If Accept-Encoding includes "br", checks for file.br
- If Accept-Encoding includes "gzip", checks for file.gz
- Falls back to the uncompressed file if no compressed version exists
Example usage:
r.Handle("/static/*", fileserver.Handler("/static", "public"))
r.Handle("/games/*", fileserver.Handler("/games", "/var/www/games"))
Types ¶
type Options ¶
type Options struct {
// CacheControl sets the Cache-Control header for all responses.
// Example: "public, max-age=31536000, immutable" for content-hashed files.
CacheControl string
// DisablePrecompressed disables checking for .br and .gz variants.
DisablePrecompressed bool
}
Options configures the static file handler behavior.