~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 +8 -0
  2. examples/poem.rs +63 -0
  3. readme.md +4 -0
Cargo.toml CHANGED
@@ -31,6 +31,11 @@ name = "axum"
31
31
  path = "examples/axum.rs"
32
32
  required-features = ["axum-ex"]
33
33
 
34
+ [[example]]
35
+ name = "poem"
36
+ path = "examples/poem.rs"
37
+ required-features = ["poem-ex"]
38
+
34
39
  [[test]]
35
40
  name = "interpolated_path"
36
41
  path = "tests/interpolated_path.rs"
@@ -49,10 +54,12 @@ rust-embed-utils = { version = "7.0.0", path = "utils"}
49
54
  include-flate = { version = "0.1", optional = true, features = ["stable"] }
50
55
  actix-web = { version = "3", default-features = false, optional = true }
51
56
  mime_guess = { version = "2", optional = true }
57
+ hex = { version = "0.4.3", optional = true }
52
58
  tokio = { version = "1.0", optional = true, features = ["macros", "rt-multi-thread"] }
53
59
  warp = { version = "0.3", default-features = false, optional = true }
54
60
  rocket = { version = "0.4.5", default-features = false, optional = true }
55
61
  axum = { version = "0.2.3", default-features = false, optional = true }
62
+ poem = { version = "1.0.8", default-features = false, optional = true }
56
63
 
57
64
  [dev-dependencies]
58
65
  sha2 = "0.9"
@@ -66,6 +73,7 @@ nightly = ["rocket"]
66
73
  actix = ["actix-web", "mime_guess"]
67
74
  warp-ex = ["warp", "tokio", "mime_guess"]
68
75
  axum-ex = ["axum", "tokio", "mime_guess"]
76
+ poem-ex = ["poem", "tokio", "mime_guess", "hex"]
69
77
 
70
78
 
71
79
  [badges]
examples/poem.rs ADDED
@@ -0,0 +1,63 @@
1
+ use poem::{
2
+ async_trait,
3
+ http::{header, Method, StatusCode},
4
+ listener::TcpListener,
5
+ Endpoint, Request, Response, Route, Server,
6
+ };
7
+ #[tokio::main]
8
+ async fn main() -> Result<(), std::io::Error> {
9
+ let app = Route::new().at("/", StaticEmbed).at("/index.html", StaticEmbed).nest("/dist", StaticEmbed);
10
+
11
+ let listener = TcpListener::bind("127.0.0.1:3000");
12
+ let server = Server::new(listener).await?;
13
+ server.run(app).await?;
14
+ Ok(())
15
+ }
16
+
17
+ #[derive(rust_embed::RustEmbed)]
18
+ #[folder = "examples/public/"]
19
+ struct Asset;
20
+ pub(crate) struct StaticEmbed;
21
+
22
+ #[async_trait]
23
+ impl Endpoint for StaticEmbed {
24
+ type Output = Response;
25
+
26
+ async fn call(&self, req: Request) -> Self::Output {
27
+ if req.method() != Method::GET {
28
+ return StatusCode::METHOD_NOT_ALLOWED.into();
29
+ }
30
+
31
+ let mut path = req.uri().path().trim_start_matches('/').trim_end_matches('/').to_string();
32
+ if path.starts_with("dist/") {
33
+ path = path.replace("dist/", "");
34
+ } else if path.is_empty() {
35
+ path = "index.html".to_string();
36
+ }
37
+ let path = path.as_ref();
38
+
39
+ match Asset::get(path) {
40
+ Some(content) => {
41
+ let hash = hex::encode(content.metadata.sha256_hash());
42
+ // if etag is matched, return 304
43
+ if req
44
+ .headers()
45
+ .get(header::IF_NONE_MATCH)
46
+ .map(|etag| etag.to_str().unwrap_or("000000").eq(&hash))
47
+ .unwrap_or(false)
48
+ {
49
+ return StatusCode::NOT_MODIFIED.into();
50
+ }
51
+
52
+ // otherwise, return 200 with etag hash
53
+ let body: Vec<u8> = content.data.into();
54
+ let mime = mime_guess::from_path(path).first_or_octet_stream();
55
+ Response::builder()
56
+ .header(header::CONTENT_TYPE, mime.as_ref())
57
+ .header(header::ETAG, hash)
58
+ .body(body)
59
+ }
60
+ None => Response::builder().status(StatusCode::NOT_FOUND).finish(),
61
+ }
62
+ }
63
+ }
readme.md CHANGED
@@ -163,6 +163,10 @@ Note: To run the `axum` example:
163
163
 
164
164
  `cargo run --example axum --features axum-ex`
165
165
 
166
+ Note: To run the [poem](https://github.com/poem-web/poem) example:
167
+
168
+ `cargo run --example poem --features poem-ex`
169
+
166
170
  ## Testing
167
171
 
168
172
  debug: `cargo test --test lib`