1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! Embed content-hashed static web assets into your binary and serve them with `tower`.
//!
//! `tower-serve-embedded` is similar to [`rust-embed`](https://docs.rs/rust-embed) but tailored
//! for *serving* web assets from a `tower`/`axum` stack: ordinary files are embedded and exposed at
//! a content-hashed URL that mirrors their location in your crate
//! (`assets/css/style.css` → `/assets/css/style.9f3a1c2b.css`), so they can be served `immutable`
//! with a one-year cache and still update instantly when their content changes.
//!
//! # How it works
//!
//! The heavy lifting (walking the directory, hashing, MIME detection, codegen) happens at build
//! time in [`tower-serve-embedded-build`](https://docs.rs/tower-serve-embedded-build), called
//! from your `build.rs`. There are **no proc macros** — the generated code is plain data plus a
//! tiny `macro_rules!`, so IDE support stays excellent.
//!
//! ```ignore
//! // build.rs
//! fn main() {
//! tower_serve_embedded_build::Builder::new("assets").emit().unwrap();
//! }
//! ```
//!
//! ```ignore
//! // src/main.rs
//! tower_serve_embedded::embed!(); // pulls in `ASSETS` and the `asset!` macro
//!
//! // Reference assets by their path relative to the crate root. Resolved at compile time —
//! // typos are compile errors:
//! // link rel="stylesheet" href=(asset!("assets/css/style.css"))
//! // => "/assets/css/style.9f3a1c2b.css"
//!
//! // Serve them: generated asset URLs are already full paths, so mount as a fallback.
//! // Router::new().fallback_service(ASSETS.service())
//! ```
//!
//! See `examples/` in the repository for complete, runnable setups (`axum`, `actix`, `warp`).
pub use ServeEmbedded;
/// The `Cache-Control` value sent for content-hashed URLs and for assets in an `immutable_dir`:
/// a one-year, `public`, `immutable` cache. The bytes behind such a URL never change.
pub const IMMUTABLE_CACHE_CONTROL: &str = "public, max-age=31536000, immutable";
/// A single embedded asset: its content plus the metadata needed to serve it.
///
/// Values of this type are generated at build time and stored in a `static` slice; you do not
/// construct them by hand.
///
/// Every file records its stable, non-hashed [`url`](Self::url). Files that are content-hashed
/// (the default) are served at their cache-busted [`hashed_url`](Self::hashed_url); files in an
/// `immutable_dir` are not re-hashed, have `hashed_url == None`, and are served at their plain
/// [`url`](Self::url).
/// A served URL mapped to the file that answers it and the `Cache-Control` to send.
///
/// One generated file produces one route: its [`hashed_url`](EmbeddedFile::hashed_url) for
/// ordinary assets, or its plain [`url`](EmbeddedFile::url) for assets in an `immutable_dir`.
/// Generated at build time and sorted by `url` for binary search; you do not construct these by
/// hand outside of tests.
/// The outcome of resolving a request path against an [`Assets`] set: which file to serve and the
/// `Cache-Control` to send with it. Returned by [`Assets::resolve`].
/// A collection of [`EmbeddedFile`]s plus the [`Route`] table that maps served URLs to them.
///
/// You normally get one via the generated `ASSETS` static (see [`embed!`]), not by calling
/// [`Assets::new`] yourself.
/// Pull in the assets generated by `tower-serve-embedded-build`.
///
/// Call this once at module scope (typically at the crate root in `main.rs` or `lib.rs`). It
/// expands to an `include!` of the build script's generated file, bringing into scope:
///
/// - `pub static ASSETS: tower_serve_embedded::Assets` — the embedded asset set, and
/// - `asset!` — a crate-local compile-time macro mapping a crate-root-relative path to its URL.
///
/// The generated `asset!` macro is deliberately not emitted with `#[macro_export]`, because Rust
/// treats `macro_export` macros generated by another macro as future-incompatible when they are
/// referenced by absolute paths. If you invoke `embed!()` at the crate root, you can use
/// `crate::asset!(...)` from other modules.
///
/// ```ignore
/// tower_serve_embedded::embed!();
///
/// fn css() -> &'static str { crate::asset!("assets/css/style.css") } // "/assets/css/style.9f3a1c2b.css"
/// ```