~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 +3 -2
  2. examples/warp.rs +18 -14
Cargo.toml CHANGED
@@ -20,7 +20,8 @@ include-flate = { version = "0.1", optional = true, features = ["stable"] }
20
20
  actix-web = { version = "2", optional = true }
21
21
  actix-rt = { version = "1", optional = true }
22
22
  mime_guess = { version = "2", optional = true }
23
+ tokio = { version = "0.2", optional = true, features = ["macros"] }
23
- warp = { version = "0.1", optional = true }
24
+ warp = { version = "0.2", optional = true }
24
25
  rocket = { version = "0.4.2", optional = true }
25
26
 
26
27
  [features]
@@ -29,7 +30,7 @@ interpolate-folder-path = ["rust-embed-impl/interpolate-folder-path"]
29
30
  compression = ["rust-embed-impl/compression", "include-flate"]
30
31
  nightly = ["rocket"]
31
32
  actix = ["actix-web", "actix-rt", "mime_guess"]
32
- warp-ex = ["warp", "mime_guess"]
33
+ warp-ex = ["warp", "tokio", "mime_guess"]
33
34
 
34
35
  [badges]
35
36
  appveyor = { repository = "pyros2097/rust-embed" }
examples/warp.rs CHANGED
@@ -1,28 +1,32 @@
1
1
  use rust_embed::RustEmbed;
2
- use warp::{filters::path::Tail, http::Response, Filter, Rejection, Reply};
2
+ use warp::{http::header::HeaderValue, path::Tail, reply::Response, Filter, Rejection, Reply};
3
-
4
- use std::borrow::Cow;
5
3
 
6
4
  #[derive(RustEmbed)]
7
5
  #[folder = "examples/public/"]
8
6
  struct Asset;
9
7
 
8
+ #[tokio::main]
10
- fn main() {
9
+ async fn main() {
11
- let index_hml = warp::get2().and(warp::path::end()).and_then(|| serve("index.html"));
10
+ let index_html = warp::path::end().and_then(serve_index);
11
+ let dist = warp::path!("dist").and(warp::path::tail()).and_then(serve);
12
12
 
13
+ let routes = index_html.or(dist);
13
- let dist = warp::path("dist").and(warp::path::tail()).and_then(|tail: Tail| serve(tail.as_str()));
14
+ warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;
15
+ }
14
16
 
17
+ async fn serve_index() -> Result<impl Reply, Rejection> {
15
- let routes = index_hml.or(dist);
18
+ serve_impl("index.html")
19
+ }
16
20
 
17
- warp::serve(routes).run(([127, 0, 0, 1], 8080));
21
+ async fn serve(path: Tail) -> Result<impl Reply, Rejection> {
22
+ serve_impl(path.as_str())
18
23
  }
19
24
 
20
- fn serve(path: &str) -> Result<impl Reply, Rejection> {
25
+ fn serve_impl(path: &str) -> Result<impl Reply, Rejection> {
26
+ let asset = Asset::get(path).ok_or_else(warp::reject::not_found)?;
21
27
  let mime = mime_guess::from_path(path).first_or_octet_stream();
22
28
 
23
- let asset: Option<Cow<'static, [u8]>> = Asset::get(path);
24
-
25
- let file = asset.ok_or_else(warp::reject::not_found)?;
29
+ let mut res = Response::new(asset.into());
26
-
27
- Ok(Response::builder().header("content-type", mime.to_string()).body(file))
30
+ res.headers_mut().insert("content-type", HeaderValue::from_str(mime.as_ref()).unwrap());
31
+ Ok(res)
28
32
  }