~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git
Discussions: https://groups.google.com/g/rust-embed-devs

rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.


Files changed (2) hide show
  1. Cargo.toml +9 -1
  2. examples/axum.rs +63 -0
Cargo.toml CHANGED
@@ -26,6 +26,11 @@ name = "rocket"
26
26
  path = "examples/rocket.rs"
27
27
  required-features = ["rocket"]
28
28
 
29
+ [[example]]
30
+ name = "axum"
31
+ path = "examples/axum.rs"
32
+ required-features = ["axum-ex"]
33
+
29
34
  [[test]]
30
35
  name = "interpolated_path"
31
36
  path = "tests/interpolated_path.rs"
@@ -44,9 +49,10 @@ rust-embed-utils = { version = "7.0.0", path = "utils"}
44
49
  include-flate = { version = "0.1", optional = true, features = ["stable"] }
45
50
  actix-web = { version = "3", default-features = false, optional = true }
46
51
  mime_guess = { version = "2", optional = true }
47
- tokio = { version = "0.2", optional = true, features = ["macros"] }
52
+ tokio = { version = "1.0", features = ["macros", "rt-multi-thread"], optional = true }
48
53
  warp = { version = "0.2", default-features = false, optional = true }
49
54
  rocket = { version = "0.4.5", default-features = false, optional = true }
55
+ axum = { version = "0.2.3", default-features = false, optional = true }
50
56
 
51
57
  [dev-dependencies]
52
58
  sha2 = "0.9"
@@ -59,6 +65,8 @@ include-exclude = ["rust-embed-impl/include-exclude"]
59
65
  nightly = ["rocket"]
60
66
  actix = ["actix-web", "mime_guess"]
61
67
  warp-ex = ["warp", "tokio", "mime_guess"]
68
+ axum-ex = ["axum", "tokio", "mime_guess"]
69
+
62
70
 
63
71
  [badges]
64
72
  appveyor = { repository = "pyros2097/rust-embed" }
examples/axum.rs ADDED
@@ -0,0 +1,63 @@
1
+ use std::convert::Infallible;
2
+
3
+ use axum::{
4
+ body::{Bytes, Full},
5
+ handler::get,
6
+ http::{header, Response, StatusCode, Uri},
7
+ response::{Html, IntoResponse},
8
+ routing::Router,
9
+ };
10
+
11
+ use axum::handler::Handler;
12
+ use mime_guess;
13
+ use rust_embed::RustEmbed;
14
+ use std::net::SocketAddr;
15
+
16
+ #[tokio::main]
17
+ async fn main() {
18
+ // build our application with a route
19
+ let app = Router::new()
20
+ .route("/hello", get(helloworld))
21
+ // handle static files with rust_embed
22
+ .or(static_handler.into_service());
23
+
24
+ // run it
25
+ let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
26
+ println!("listening on {}", addr);
27
+ axum::Server::bind(&addr).serve(app.into_make_service()).await.unwrap();
28
+ }
29
+
30
+ async fn helloworld() -> Html<&'static str> {
31
+ Html("<h1>Hello, World!</h1>")
32
+ }
33
+
34
+ async fn static_handler(uri: Uri) -> impl IntoResponse {
35
+ let path = uri.path().trim_start_matches('/').to_string();
36
+
37
+ StaticFile(path)
38
+ }
39
+
40
+ #[derive(RustEmbed)]
41
+ #[folder = "examples/public/"]
42
+ struct Asset;
43
+ pub struct StaticFile<T>(pub T);
44
+
45
+ impl<T> IntoResponse for StaticFile<T>
46
+ where
47
+ T: Into<String>,
48
+ {
49
+ type Body = Full<Bytes>;
50
+ type BodyError = Infallible;
51
+
52
+ fn into_response(self) -> Response<Self::Body> {
53
+ let path = self.0.into();
54
+ match Asset::get(path.as_str()) {
55
+ Some(content) => {
56
+ let body = content.data.into();
57
+ let mime = mime_guess::from_path(path).first_or_octet_stream();
58
+ Response::builder().header(header::CONTENT_TYPE, mime.as_ref()).body(body).unwrap()
59
+ }
60
+ None => Response::builder().status(StatusCode::NOT_FOUND).body(Full::from("404")).unwrap(),
61
+ }
62
+ }
63
+ }