axum_embed_files/
lib.rs

1//! Serve static embedded files.
2//!
3//! This crate provides utilities to serve static files that are
4//! embedded in the Rust binary at compile time.  This is most useful
5//! for including small asset files directly in a web server.  The files
6//! are served with `Content-Type`, `ETag`, and `Last-Modified` headers,
7//! and conditional requests are handled correctly (well, just
8//! `If-None-Match` so far).
9//!
10//! For development, you may wish to serve the files directly from the local
11//! file system.  Compile with the feature `serve-from-fs` and get coding!
12//!
13//! # Example
14//!
15//! ```
16//! use axum::Router;
17//! use axum_embed_files::embed_files;
18//!
19//! fn router<S: Clone + Sync + Send + 'static>() -> Router<S> {
20//!     embed_files!("assets", [
21//!         "img/logo.svg",
22//!         "style.css",
23//!     ])
24//! }
25//! ```
26
27pub use axum_embed_files_macros::*;
28
29pub mod service;
30
31/// Build an axum Router to serve embedded files.
32///
33/// ```nobuild
34/// embed_files!(dir: &str, files: [&str]) -> axum::Router;
35/// ```
36///
37/// This macro creates an `axum::Router` that routes each file by the path
38/// specified.  The files are loaded from the `dir` directory, relative to
39/// the `CARGO_MANIFEST_PATH`.
40///
41/// # Example
42///
43/// ```
44/// use axum::Router;
45/// use axum_embed_files::embed_files;
46///
47/// fn router<S: Clone + Sync + Send + 'static>() -> Router<S> {
48///     embed_files!("assets", [
49///         "img/logo.svg",
50///         "style.css",
51///     ])
52/// }
53/// ```
54#[macro_export]
55macro_rules! embed_files {
56    (
57        $dir:literal, [
58        $(
59            $file:literal
60        ),* $(,)?
61        ]
62    ) => {
63        ::axum::Router::new()
64        $(
65            .route(
66                ::std::concat!("/", $file),
67                ::axum::routing::get_service($crate::embed_file!($dir, $file)),
68            )
69        )*
70    };
71}