|
1 | | -# static-serve |
| 1 | +# Static Serve |
| 2 | + |
| 3 | +A Rust library for compressing and embedding static assets in a web server using [Axum](https://github.com/tokio-rs/axum). This crate provides efficient asset embedding with optional compression (`gzip` and `zstd`) and conditional requests support. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Embed static assets** at compile-time for efficient serving |
| 8 | + |
| 9 | +- **Automatic compression** with `gzip` and `zstd` |
| 10 | + |
| 11 | +- **ETag support** for conditional requests and caching |
| 12 | + |
| 13 | +- **Seamless Axum integration** with request extraction for encoding and caching headers |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Add the following to your `Cargo.toml`: |
| 18 | + |
| 19 | +```toml |
| 20 | +[dependencies] |
| 21 | +static-serve = "0.1" |
| 22 | +axum = "0.8" |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### Embedding Static Assets |
| 28 | + |
| 29 | +Use the `embed_assets!` macro to create a `static_router()` function in scope which will include your static files, embedding them into your binary: |
| 30 | + |
| 31 | +```rust |
| 32 | +use static_serve::embed_assets; |
| 33 | + |
| 34 | +embed_assets!("assets", compress = true); |
| 35 | +let router = static_router(); |
| 36 | +``` |
| 37 | + |
| 38 | +This will: |
| 39 | + |
| 40 | +- Include all files from the `assets` directory |
| 41 | +- Compress them using `gzip` and `zstd` (if beneficial) |
| 42 | +- Generate a `static_router()` function to serve these assets |
| 43 | + |
| 44 | +### Conditional Requests & Caching |
| 45 | + |
| 46 | +The crate automatically handles: |
| 47 | +- `Accept-Encoding` header to serve compressed versions if available |
| 48 | +- `If-None-Match` header for ETag validation, returning `304 Not Modified` if unchanged |
| 49 | + |
| 50 | +### Required parameter |
| 51 | + |
| 52 | +- `path_to_dir` - a valid Rust identifier or `&str` of the path to the static files to be included |
| 53 | + |
| 54 | +### Optional parameters |
| 55 | + |
| 56 | +- `compress = false` - compress static files with zstd and gzip, true or false (defaults to false) |
| 57 | + |
| 58 | +- `ignore_dirs = [my_ignore_dir, other_ignore_dir]` - a bracketed list of valid Rust identifiers or `&str`s of the paths/subdirectories inside the target directory, which should be ignored and not included. (If this parameter is missing, no subdirectories will be ignored) |
| 59 | + |
| 60 | +## Example |
| 61 | + |
| 62 | +```rust |
| 63 | + |
| 64 | +use axum::{Router, Server}; |
| 65 | +use static_serve::embed_assets; |
| 66 | + |
| 67 | +embed_assets!("public", compress = true); |
| 68 | + |
| 69 | +#[tokio::main] |
| 70 | +async fn main() { |
| 71 | + let router = static_router(); |
| 72 | + Server::bind(&"0.0.0.0:3000".parse().unwrap()) |
| 73 | + .serve(router.into_make_service()) |
| 74 | + .await |
| 75 | + .unwrap(); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## License |
| 80 | + |
| 81 | +Licensed under either of |
| 82 | +- Apache License, Version 2.0, (LICENSE-APACHE or [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0)) |
| 83 | +- MIT license (LICENSE-MIT or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)) |
| 84 | + |
| 85 | +at your option. |
| 86 | + |
| 87 | +### Contribution |
| 88 | + |
| 89 | +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |
0 commit comments