Skip to content

Commit 78577d5

Browse files
docs: create README.md and crate docs in lib.rs
1 parent 57f5404 commit 78577d5

File tree

4 files changed

+176
-3
lines changed

4 files changed

+176
-3
lines changed

README.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,89 @@
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.

static-serve/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
- **Automatic compression** with `gzip` and `zstd`
9+
- **ETag support** for conditional requests and caching
10+
- **Seamless Axum integration** with request extraction for encoding and caching headers
11+
12+
## Installation
13+
14+
Add the following to your `Cargo.toml`:
15+
16+
```toml
17+
[dependencies]
18+
static-serve = "0.1"
19+
axum = "0.8"
20+
```
21+
22+
## Usage
23+
24+
### Embedding Static Assets
25+
26+
Use the `embed_assets!` macro to create a `static_router()` function in scope which will include your static files, embedding them into your binary:
27+
28+
```rust,ignore
29+
use static_serve::embed_assets;
30+
31+
embed_assets!("assets", compress = true);
32+
let router = static_router();
33+
```
34+
35+
This will:
36+
37+
- Include all files from the `assets` directory
38+
- Compress them using `gzip` and `zstd` (if beneficial)
39+
- Generate a `static_router()` function to serve these assets
40+
41+
### Conditional Requests & Caching
42+
43+
The crate automatically handles:
44+
- `Accept-Encoding` header to serve compressed versions if available
45+
- `If-None-Match` header for ETag validation, returning `304 Not Modified` if unchanged
46+
47+
### Required parameter
48+
49+
- `path_to_dir` - a valid Rust identifier or `&str` of the path to the static files to be included
50+
51+
### Optional parameters
52+
53+
- `compress = false` - compress static files with zstd and gzip, true or false (defaults to false)
54+
- `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)
55+
56+
## Example
57+
58+
```rust,ignore
59+
use axum::{Router, Server};
60+
use static_serve::embed_assets;
61+
62+
embed_assets!("public", compress = true);
63+
64+
#[tokio::main]
65+
async fn main() {
66+
let router = static_router();
67+
Server::bind(&"0.0.0.0:3000".parse().unwrap())
68+
.serve(router.into_make_service())
69+
.await
70+
.unwrap();
71+
}
72+
```
73+
74+
## License
75+
76+
Licensed under either of
77+
- Apache License, Version 2.0, (LICENSE-APACHE or [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0))
78+
- MIT license (LICENSE-MIT or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT))
79+
80+
at your option.
81+
82+
### Contribution
83+
84+
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.

static-serve/clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doc-valid-idents = ["ETag", ".."]

static-serve/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//! Crate for compressing and embedding static assets
2-
//! in a web server
1+
#![doc = include_str!("../README.md")]
2+
33
use std::convert::Infallible;
44

55
use axum::{

0 commit comments

Comments
 (0)