~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 +1 -1
  2. examples/axum.rs +9 -16
Cargo.toml CHANGED
@@ -58,7 +58,7 @@ hex = { version = "0.4.3", optional = true }
58
58
  tokio = { version = "1.0", optional = true, features = ["macros", "rt-multi-thread"] }
59
59
  warp = { version = "0.3", default-features = false, optional = true }
60
60
  rocket = { version = "0.4.5", default-features = false, optional = true }
61
- axum = { version = "0.2.3", default-features = false, optional = true }
61
+ axum = { version = "0.4", default-features = false, features = ["http1"], optional = true }
62
62
  poem = { version = "1.0.8", default-features = false, optional = true }
63
63
 
64
64
  [dev-dependencies]
examples/axum.rs CHANGED
@@ -1,14 +1,10 @@
1
- use std::convert::Infallible;
2
-
3
1
  use axum::{
4
- body::{Bytes, Full},
2
+ body::{boxed, Full},
5
- handler::get,
3
+ handler::Handler,
6
- http::{header, Response, StatusCode, Uri},
4
+ http::{header, StatusCode, Uri},
7
- response::{Html, IntoResponse},
5
+ response::{Html, IntoResponse, Response},
8
- routing::Router,
6
+ routing::{get, Router},
9
7
  };
10
-
11
- use axum::handler::Handler;
12
8
  use mime_guess;
13
9
  use rust_embed::RustEmbed;
14
10
  use std::net::SocketAddr;
@@ -22,7 +18,7 @@ async fn main() {
22
18
  .route("/", get(index_handler))
23
19
  .route("/index.html", get(index_handler))
24
20
  .route("/dist/", static_handler.into_service())
25
- .or(static_handler.into_service());
21
+ .fallback(static_handler.into_service());
26
22
 
27
23
  // run it
28
24
  let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
@@ -57,18 +53,15 @@ impl<T> IntoResponse for StaticFile<T>
57
53
  where
58
54
  T: Into<String>,
59
55
  {
60
- type Body = Full<Bytes>;
61
- type BodyError = Infallible;
62
-
63
- fn into_response(self) -> Response<Self::Body> {
56
+ fn into_response(self) -> Response {
64
57
  let path = self.0.into();
65
58
  match Asset::get(path.as_str()) {
66
59
  Some(content) => {
67
- let body = content.data.into();
60
+ let body = boxed(Full::from(content.data));
68
61
  let mime = mime_guess::from_path(path).first_or_octet_stream();
69
62
  Response::builder().header(header::CONTENT_TYPE, mime.as_ref()).body(body).unwrap()
70
63
  }
71
- None => Response::builder().status(StatusCode::NOT_FOUND).body(Full::from("404")).unwrap(),
64
+ None => Response::builder().status(StatusCode::NOT_FOUND).body(boxed(Full::from("404"))).unwrap(),
72
65
  }
73
66
  }
74
67
  }