~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 (3) hide show
  1. Cargo.toml +2 -0
  2. examples/salvo.rs +48 -0
  3. readme.md +4 -0
Cargo.toml CHANGED
@@ -60,6 +60,7 @@ warp = { version = "0.3", default-features = false, optional = true }
60
60
  rocket = { version = "0.5.0-rc.1", default-features = false, optional = true }
61
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
+ salvo = { version = "0.16", default-features = false, optional = true }
63
64
 
64
65
  [dev-dependencies]
65
66
  sha2 = "0.9"
@@ -73,6 +74,7 @@ actix = ["actix-web", "mime_guess"]
73
74
  warp-ex = ["warp", "tokio", "mime_guess"]
74
75
  axum-ex = ["axum", "tokio", "mime_guess"]
75
76
  poem-ex = ["poem", "tokio", "mime_guess", "hex"]
77
+ salvo-ex = ["salvo", "tokio", "mime_guess", "hex"]
76
78
 
77
79
 
78
80
  [badges]
examples/salvo.rs ADDED
@@ -0,0 +1,48 @@
1
+ use salvo::http::{header, StatusCode};
2
+ use salvo::prelude::*;
3
+
4
+ #[tokio::main]
5
+ async fn main() -> Result<(), std::io::Error> {
6
+ let router = Router::new()
7
+ .push(Router::with_path("dist/<**>").get(static_embed))
8
+ .push(Router::with_path("/<**>").get(static_embed));
9
+
10
+ let listener = TcpListener::bind("127.0.0.1:3000");
11
+ Server::new(listener).serve(router).await;
12
+ Ok(())
13
+ }
14
+
15
+ #[derive(rust_embed::RustEmbed)]
16
+ #[folder = "examples/public/"]
17
+ struct Asset;
18
+
19
+ #[fn_handler]
20
+ async fn static_embed(req: &mut Request, res: &mut Response) {
21
+ let mut path: String = req.get_param("**").unwrap_or_default();
22
+ if path.is_empty() {
23
+ path = "index.html".into();
24
+ }
25
+
26
+ match Asset::get(&path) {
27
+ Some(content) => {
28
+ let hash = hex::encode(content.metadata.sha256_hash());
29
+ // if etag is matched, return 304
30
+ if req
31
+ .headers()
32
+ .get(header::IF_NONE_MATCH)
33
+ .map(|etag| etag.to_str().unwrap_or("000000").eq(&hash))
34
+ .unwrap_or(false)
35
+ {
36
+ res.set_status_code(StatusCode::NOT_MODIFIED);
37
+ return;
38
+ }
39
+
40
+ // otherwise, return 200 with etag hash
41
+ let body: Vec<u8> = content.data.into();
42
+ let mime = mime_guess::from_path(path).first_or_octet_stream();
43
+ res.headers_mut().insert(header::ETAG, hash.parse().unwrap());
44
+ res.render_binary(mime.as_ref().parse().unwrap(), &body);
45
+ }
46
+ None => res.set_status_code(StatusCode::NOT_FOUND),
47
+ }
48
+ }
readme.md CHANGED
@@ -167,6 +167,10 @@ Note: To run the [poem](https://github.com/poem-web/poem) example:
167
167
 
168
168
  `cargo run --example poem --features poem-ex`
169
169
 
170
+ Note: To run the [salvo](https://github.com/salvo-rs/salvo) example:
171
+
172
+ `cargo run --example salvo --features salvo-ex`
173
+
170
174
  ## Testing
171
175
 
172
176
  debug: `cargo test --test lib`