serdir
Rust helpers for serving HTTP GET and HEAD responses with hyper 1.x and tokio.
This crate provides utilities for serving static files in Rust web applications.
Features
- Range requests
- Large file support via chunked streaming
- Live content changes
- ETag header generation and conditional GET requests
- Serving files that have been pre-compressed using gzip, brotli or zstd
- Cached runtime compression of files using brotli
- Content type detection based on filename extensions
- Serving directory paths using
index.htmlpages - Customizing 404 response content
- Support for the Tower
ServiceandLayerAPIs
This crate is derived from http-serve.
Example
Serve files via Hyper:
use http1;
use TokioIo;
use ServedDir;
use BrotliLevel;
use ;
use TcpListener;
async
Serve files via Tower:
use http1;
use TokioIo;
use TowerToHyperService;
use ServedDir;
use ;
use TcpListener;
async
Run the builtin example:
$ cargo run --example hyper --features hyper .
Optional features
This project defines 3 optional build features that can be enabled at compile time:
runtime-compression- enables the cached compression strategy, i.e. runtime compression of served files using Brotlitower- enables integration with Tower-based web frameworks like Axum and Poem by providing APIs to convert aServedDirinto atower::Serviceortower::Layerhyper- enables direct integration with thehyperweb server by providing APIs to convert aServedDirinto ahyper::Service
Authors
See the AUTHORS file for details.
License
Your choice of MIT or Apache; see LICENSE-MIT.txt or LICENSE-APACHE, respectively.