Documentation
¶
Overview ¶
Package static provides a static file server middleware for Hanzo Ingress.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compress ¶ added in v0.5.6
Compress wraps a handler so text responses go out gzip-encoded when the client says it can read them.
It is deliberately OUTSIDE the file server rather than inside it: the decision is about the response, not about the file, and every handler mounted here — the directory listing, the 404 page, a future one — gets it for free.
Three requests opt out entirely and are handed the raw writer:
- No `Accept-Encoding: gzip`. Nothing to negotiate.
- A `Range` request. http.ServeContent answers those from byte offsets into the identity body; those offsets do not address a compressed stream, and re-deriving them would mean compressing the whole file to serve a slice of it. Ranges stay identity, which is what every CDN and browser expects.
- A HEAD. It has no body, so there is nothing to compress, and its headers describe the identity representation. Wrapping it was measurably wrong: net/http discards a HEAD body but still counts the bytes offered, so the gzip envelope closing over an empty stream made the server announce `Content-Length: 23` for a 146 KB stylesheet — a number no GET would ever produce and one a health check or a CDN would believe.
Types ¶
type Config ¶
type Config struct {
// Root directory to serve files from (local filesystem).
Root string `json:"root,omitempty"`
// S3 backend configuration (if set, takes precedence over Root).
S3 *S3Config `json:"s3,omitempty"`
// EnableDirectoryListing enables directory listing.
EnableDirectoryListing bool `json:"enableDirectoryListing,omitempty"`
// IndexFiles is a list of filenames to try when a directory is requested.
IndexFiles []string `json:"indexFiles,omitempty"`
// SPAMode redirects all not-found requests to a single page.
SPAMode bool `json:"spaMode,omitempty"`
// SPAIndex is the file to serve in SPA mode.
SPAIndex string `json:"spaIndex,omitempty"`
// ErrorPage404 is the path to a custom 404 error page.
ErrorPage404 string `json:"errorPage404,omitempty"`
// CacheControl sets cache control headers for static files.
CacheControl map[string]string `json:"cacheControl,omitempty"`
// Key is the site's publishable key (pk-…). Every document this server sends
// goes out carrying the Hanzo tag stamped with it; without one, no tag is
// injected. See tag.go.
Key string `json:"key,omitempty"`
}
Config holds the static file server middleware configuration.
func CreateConfig ¶
func CreateConfig() *Config
CreateConfig creates the default plugin configuration.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a static file server handler.
func (*Handler) GetTracingInformation ¶
GetTracingInformation returns the middleware name and type for observability.
type S3Config ¶ added in v0.2.0
type S3Config struct {
// IndexFiles tried for a directory request; defaults to index.html/index.htm.
IndexFiles []string `json:"indexFiles,omitempty"`
Endpoint string `json:"endpoint,omitempty"` // S3 endpoint (e.g. s3:9000)
Bucket string `json:"bucket,omitempty"` // Bucket name
Region string `json:"region,omitempty"` // Region (default us-east-1)
AccessKey string `json:"accessKey,omitempty"` // Access key ID
SecretKey string `json:"secretKey,omitempty"` // Secret access key
Prefix string `json:"prefix,omitempty"` // Key prefix
UseSSL bool `json:"useSSL,omitempty"` // Use HTTPS
}
S3Config holds S3 backend configuration.
type S3FS ¶ added in v0.2.0
type S3FS struct {
// contains filtered or unexported fields
}
S3FS implements http.FileSystem backed by S3-compatible s3.
func (*S3FS) Open ¶ added in v0.2.0
Open implements http.FileSystem.
A DIRECTORY REQUEST IS TRANSLATED, NOT REFUSED. On a filesystem `Open("/app/")` yields a directory and the handler then looks inside it for an index; S3 has no directories, so the same intent has to be expressed by asking for the index key itself. Refusing outright — which is what this did — made every static SITE 404 at its own root and at every sub-route, while every ASSET served perfectly. That is exactly why it went unnoticed for so long: the CDN only ever asks for files, and a file is the one case that worked.