Tower Layers
Tower layers process requests and responses in the web engine. Sword includes layers for compression, CORS, static files, security, request limits, and logging. Some are enabled with web-* features, and others are configured from TOML.
Compression
Compression reduces the size of responses before they are sent. It is enabled with the web-compression feature and configured in the [compression] section.
[compression]
algorithms = ["gzip", "br"]The algorithms key accepts a list of these algorithms.
| Algorithm | Note |
|---|---|
gzip | Widely supported |
br | Brotli, best compression ratio |
deflate | Legacy algorithm |
zstd | Fast, modern algorithm |
Compression wraps CORS, so it also compresses preflight responses.
CORS
CORS allows applications from other origins to consume the API. It is enabled with the web-cors feature and configured in the [cors] section.
[cors]
allow-origins = ["*"]
allow-methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]The available options are the following.
| Option | Description |
|---|---|
allow-origins | List of allowed origins or ["*"] for all |
allow-methods | HTTP methods to allow |
allow-headers | Custom allowed headers |
allow-credentials | Enables credentials sharing |
max-age | Seconds to cache the preflight response |
Static file serving
The static file server publishes a directory under a route. It is enabled with the web-servedir feature and configured in the [serve-dir] section.
[serve-dir]
static-dir = "public"
router-path = "/static"| Key | Description | Default |
|---|---|---|
static-dir | Directory to serve | "public" |
router-path | URL path prefix | "/static" |
compression-algorithm | Pre-compressed file support (br, gzip, deflate, zstd) | — |
chunk-size | Streaming chunk size (e.g. "64KB") | — |
not-found-file | Custom 404 fallback file | — |
Helmet
Helmet adds security headers to responses. It is enabled with the web-helmet feature and applied manually to the router.
[dependencies]
sword = { version = "0.7", features = ["web", "web-helmet"] }use sword::prelude::*;
use sword::web::helmet::*;
let helmet = Helmet::builder()
.with_header(XContentTypeOptions::nosniff())
.with_header(XXSSProtection::on())
.build()
.expect("failed to build helmet layer");
let app = Application::builder()?
.with_module::<SomeModule>()
.build()?
.with_router(|router| router.layer(helmet));Limits, cookies, and logging
The body limit rejects requests that exceed the maximum size. The timeout cancels requests that take longer than configured. Both are set in [web].
[web]
body-limit = "2MB"
request-timeout = { enabled = true, timeout = "30s" }Cookies are read on every request. The access logger records every request, and the request ID assigns an identifier to correlate logs.
The logger is configured in [web.logger]. See access logger
Custom layers
Application::with_router receives the router before the state is attached and returns the modified router. Use it to add a Tower layer, a service, or a fallback.
use tower_http::cors::*;
let cors_layer = CorsLayer::new()
.allow_origin(Any)
.allow_methods(vec!["GET", "POST"]);
let app = Application::builder()?
.with_module::<SomeModule>()
.build()?
.with_router(|router| router.layer(cors_layer));These layers wrap the rest of the engine, so they can modify any request or response.

