Skip to content

Auto-registered Layers & Services

Sword can automatically register common Tower layers and services through configuration alone — no .layer(...) or .nest_service(...) calls needed.

Compression

Enable response compression via the [compression] config section:

toml
[compression]
algorithms = ["gzip", "br"]

Supported Algorithms

AlgorithmNote
gzipWidely supported
brBrotli, best compression ratio
deflateLegacy algorithm
zstdFast, modern algorithm

CORS

Configure Cross-Origin Resource Sharing:

toml
[cors]
allow-origins = ["*"]
allow-methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]

Available Options

  • allow-origins — list of allowed origins or ["*"] for all
  • allow-methods — HTTP methods to allow
  • allow-headers — custom allowed headers
  • allow-credentials — boolean, enable credentials sharing
  • max-age — seconds to cache preflight response

Static File Serving

Serve a directory of static files with [serve-dir]:

toml
[serve-dir]
static-dir = "public"
router-path = "/static"

Options

KeyDescriptionDefault
static-dirDirectory to serve"public"
router-pathURL path prefix"/static"
compressionPre-compressed file supporttrue
chunk-sizeStreaming chunk size in bytes65536
not-foundCustom 404 fallback path

How It Works

When Sword loads the configuration, it checks for these sections and automatically registers the corresponding Tower layers or services in the correct order before the application starts. No manual setup required.

See Also