Static files

server.StaticConfig controls the public directory, streaming, compression, headers and partial requests.

gox
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

FieldZero valueBehavior
Path""Use public.
StreamMinFileSize0It uses 512 KiB.
Compressionempty structureDon't compress files.
HeadersnilIt doesn't add custom rules.
RangenilIt does not enable partial responses by rules.

Path is resolved within the project and cannot escape by ...


Headers and ranges

gox
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.

gox
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.

MatchScope
/assets/logo.svgAn exact file.
/assets/:name.svgA file .svg in one level.
/assets/*Files with or without extension, in any depth.
/assets/*.svgFiles .svg, also within subdirectories.
'/ (photosimages)/*.(jpg

server.DefaultStaticCompressionConfig() returns the expanded compression object of the first example. It is an auxiliary function, not a special server mode:

gox
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.

See also

Next step

Continue with Routes to relate public routes to internal destinations.

Routes