Static files
server.StaticConfig controls the public directory, streaming, compression, headers and partial requests.
var Config = server.StaticConfig{
Path: "public",
StreamMinFileSize: 512 * 1024,
Compression: server.CompressionConfig{
Enabled: true,
OrderPriority: []string{
"brotli",
"gzip",
"zstd",
"deflate",
},
Brotli: server.CompressionAlgorithmConfig{
Enabled: true,
Level: 7,
},
Gzip: server.CompressionAlgorithmConfig{
Enabled: true,
Level: 6,
},
Zstd: server.CompressionAlgorithmConfig{
Enabled: true,
Level: 5,
},
Deflate: server.CompressionAlgorithmConfig{
Enabled: true,
Level: 6,
},
},
} Structure
| Field | Zero value | Behavior |
|---|---|---|
Path | "" | Use public. |
StreamMinFileSize | 0 | It uses 512 KiB. |
Compression | empty structure | Don't compress files. |
Headers | nil | It doesn't add custom rules. |
Range | nil | It does not enable partial responses by rules. |
Path is resolved within the project and cannot escape by ...
Headers and ranges
Headers: []server.StaticHeaderRule{
{
Match: "/assets/*",
Headers: server.Headers{
"Cache-Control": "public, max-age=31536000, immutable",
},
},
},
Range: []server.StaticRangeRule{
{
Match: "/video/*",
Enabled: true,
MinFileSize: 4 * 1024 * 1024,
MaxChunkSize: 1024 * 1024,
},
}, The rules use the same compiled grammar that Routes. Admit exact routes, parameters, optional segments, alternatives and wildcard with or without extension.
Headers: []server.StaticHeaderRule{
{
Match: "/(photos|images)/*.(jpg|jpeg|png|webp)",
Headers: server.Headers{
"Cache-Control": "public, max-age=31536000, immutable",
},
},
},
Range: []server.StaticRangeRule{
{
Match: "/media/*.(mp4|webm)",
Enabled: true,
MinFileSize: 4 * 1024 * 1024,
MaxChunkSize: 1024 * 1024,
},
{
Match: "*",
Enabled: false,
},
}, The rules are evaluated in the declared order and the first match is applied. * can be used as final fallback.
Match is compared to the final route of the file that GOX tries to serve. Routes rewrite /photos/avatar.webp to /public/assets/avatar.webp, the static rules compare /public/assets/avatar.webp. client assets are the exception: they compare their reserved public route.
Match | Scope |
|---|---|
/assets/logo.svg | An exact file. |
/assets/:name.svg | A file .svg in one level. |
/assets/* | Files with or without extension, in any depth. |
/assets/*.svg | Files .svg, also within subdirectories. |
| '/ (photos | images)/*.(jpg |
server.DefaultStaticCompressionConfig() returns the expanded compression object of the first example. It is an auxiliary function, not a special server mode:
Compression: server.DefaultStaticCompressionConfig(), Enable Brotli, Gzip, Zstd and Deflate with levels oriented to static files. Its priorities and levels are different from the dynamic compression described in HTTP.
The compiled assets of client They are also served through static policy.
Reloading
Path, StreamMinFileSize, Compression, Headers and Range are rewarded during hot relax and applied to subsequent requests. GOX also reprepares client assets tablets with new configuration.
The files already sent cannot be altered, but the following request uses the updated policy. No need to restart the instance.
Best practices
- Keep the static directory inside the project.
- It uses immutable cache only for versioned names.
- Enable ranges for large files that really need them.
- Variance in static compression of dynamic HTTP compression.