Skip to content

Commit 5148d6f

Browse files
docs: update to add embed_asset! info
1 parent dece540 commit 5148d6f

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

README.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ axum = "0.8"
2424

2525
## Usage
2626

27-
### Embedding Static Assets
27+
### Embedding a directory of static assets
2828

2929
Use the `embed_assets!` macro to create a `static_router()` function in scope which will include your static files, embedding them into your binary:
3030

@@ -41,37 +41,66 @@ This will:
4141
- Compress them using `gzip` and `zstd` (if beneficial)
4242
- Generate a `static_router()` function to serve these assets
4343

44-
### Conditional Requests & Caching
45-
46-
The crate automatically handles:
47-
48-
- `Accept-Encoding` header to serve compressed versions if available
49-
- `If-None-Match` header for ETag validation, returning `304 Not Modified` if unchanged
50-
51-
### Required parameter
44+
#### Required parameter
5245

5346
- `path_to_dir` - a valid `&str` string literal of the path to the static files to be included
5447

55-
### Optional parameters
48+
#### Optional parameters
5649

5750
- `compress = false` - compress static files with zstd and gzip, true or false (defaults to false)
5851

5952
- `ignore_dirs = [my_ignore_dir, other_ignore_dir]` - a bracketed list of `&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)
6053

6154
- `strip_html_ext = false` - strips the `.html` or `.htm` from all HTML files included. If the filename is `index.html` or `index.htm`, the `index` part will also be removed, leaving just the root (defaults to false)
6255

63-
## Example
56+
### Embedding a single static asset file
57+
58+
Use the `embed_asset!` macro to return a function you can use as a GET handler, which will include your static file, embedded into your binary:
6459

6560
```rust
61+
use static_serve::embed_assets;
62+
63+
let router: Router<()> = Router::new();
64+
let handler = embed_asset!("assets/my_file.png", compress = true);
65+
let router = router.route("/my_file.png", handler);
66+
67+
```
68+
69+
This will:
70+
71+
- Include the file `my_file.png` from the `assets` directory
72+
- Compress it using `gzip` and `zstd` (if beneficial)
73+
- Generate a `MethodRouter` "handler" you can add as a route on your router to serve the file
6674

75+
#### Required parameter
76+
77+
- `path_to_file` - a valid `&str` string literal of the path to the static file to be included
78+
79+
#### Optional parameter
80+
81+
- `compress = false` - compress a static file with zstd and gzip, true or false (defaults to false)
82+
83+
## Conditional Requests & Caching
84+
85+
The crate automatically handles:
86+
87+
- `Accept-Encoding` header to serve compressed versions if available
88+
- `If-None-Match` header for ETag validation, returning `304 Not Modified` if unchanged
89+
90+
## Example
91+
92+
```rust
6793
use axum::{Router, Server};
68-
use static_serve::embed_assets;
94+
use static_serve::{embed_assets, embed_asset};
6995

7096
embed_assets!("public", compress = true);
7197

7298
#[tokio::main]
7399
async fn main() {
74100
let router = static_router();
101+
let my_file_handler = embed_asset!("other_files/my_file.txt");
102+
let router = router.route("/other_files/my_file.txt", my_file_handler);
103+
75104
Server::bind(&"0.0.0.0:3000".parse().unwrap())
76105
.serve(router.into_make_service())
77106
.await

0 commit comments

Comments
 (0)