~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git

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


0fb2054a Franck Royer

6 years ago
Add warp example
Files changed (3) hide show
  1. .travis.yml +2 -0
  2. Cargo.toml +2 -1
  3. examples/warp.rs +32 -0
.travis.yml CHANGED
@@ -20,3 +20,5 @@ script:
20
20
  - cargo build --example basic --release
21
21
  - cargo build --example actix --features actix
22
22
  - cargo build --example actix --features actix --release
23
+ - cargo build --example warp --features warp-ex
24
+ - cargo build --example warp --features warp-ex --release
Cargo.toml CHANGED
@@ -16,13 +16,14 @@ rust-embed-impl = { version = "4.3.0", path = "impl"}
16
16
 
17
17
  actix-web = { version = "0.7", optional = true }
18
18
  mime_guess = { version = "2.0.0-alpha.6", optional = true }
19
-
19
+ warp = { version = "0.1", optional = true }
20
20
  rocket = { version = "0.4.0", optional = true }
21
21
 
22
22
  [features]
23
23
  debug-embed = ["rust-embed-impl/debug-embed"]
24
24
  nightly = ["rocket"]
25
25
  actix = ["actix-web", "mime_guess"]
26
+ warp-ex = ["warp", "mime_guess"]
26
27
 
27
28
  [badges]
28
29
  appveyor = { repository = "pyros2097/rust-embed" }
examples/warp.rs ADDED
@@ -0,0 +1,32 @@
1
+ #![deny(warnings)]
2
+
3
+ #[macro_use]
4
+ extern crate rust_embed;
5
+ extern crate warp;
6
+
7
+ use std::borrow::Cow;
8
+ use warp::{filters::path::Tail, http::Response, Filter, Rejection, Reply};
9
+
10
+ #[derive(RustEmbed)]
11
+ #[folder = "examples/public/"]
12
+ struct Asset;
13
+
14
+ fn main() {
15
+ let index_hml = warp::get2().and(warp::path::end()).and_then(|| serve("index.html"));
16
+
17
+ let dist = warp::path("dist").and(warp::path::tail()).and_then(|tail: Tail| serve(tail.as_str()));
18
+
19
+ let routes = index_hml.or(dist);
20
+
21
+ warp::serve(routes).run(([127, 0, 0, 1], 8080));
22
+ }
23
+
24
+ fn serve(path: &str) -> Result<impl Reply, Rejection> {
25
+ let mime = mime_guess::guess_mime_type(path);
26
+
27
+ let asset: Option<Cow<'static, [u8]>> = Asset::get(path);
28
+
29
+ let file = asset.ok_or_else(|| warp::reject::not_found())?;
30
+
31
+ Ok(Response::builder().header("content-type", mime.to_string()).body(file))
32
+ }