Skip to content

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.

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

The algorithms key accepts a list of these algorithms.

AlgorithmNote
gzipWidely supported
brBrotli, best compression ratio
deflateLegacy algorithm
zstdFast, 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.

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

The available options are the following.

OptionDescription
allow-originsList of allowed origins or ["*"] for all
allow-methodsHTTP methods to allow
allow-headersCustom allowed headers
allow-credentialsEnables credentials sharing
max-ageSeconds 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.

toml
[serve-dir]
static-dir = "public"
router-path = "/static"
KeyDescriptionDefault
static-dirDirectory to serve"public"
router-pathURL path prefix"/static"
compression-algorithmPre-compressed file support (br, gzip, deflate, zstd)—
chunk-sizeStreaming chunk size (e.g. "64KB")—
not-found-fileCustom 404 fallback file—

Helmet ​

Helmet adds security headers to responses. It is enabled with the web-helmet feature and applied manually to the router.

toml
[dependencies]
sword = { version = "0.7", features = ["web", "web-helmet"] }
rust
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].

toml
[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.

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