~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 +1 -1
  2. examples/poem.rs +11 -9
  3. utils/src/lib.rs +1 -0
Cargo.toml CHANGED
@@ -59,7 +59,7 @@ tokio = { version = "1.0", optional = true, features = ["macros", "rt-multi-thre
59
59
  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
- poem = { version = "1.0.8", default-features = false, optional = true }
62
+ poem = { version = "1.3.18", default-features = false, features = ["server"], optional = true }
63
63
  salvo = { version = "0.16", default-features = false, optional = true }
64
64
 
65
65
  [dev-dependencies]
examples/poem.rs CHANGED
@@ -2,7 +2,7 @@ use poem::{
2
2
  async_trait,
3
3
  http::{header, Method, StatusCode},
4
4
  listener::TcpListener,
5
- Endpoint, Request, Response, Route, Server,
5
+ Endpoint, Request, Response, Result, Route, Server,
6
6
  };
7
7
  #[tokio::main]
8
8
  async fn main() -> Result<(), std::io::Error> {
@@ -23,9 +23,9 @@ pub(crate) struct StaticEmbed;
23
23
  impl Endpoint for StaticEmbed {
24
24
  type Output = Response;
25
25
 
26
- async fn call(&self, req: Request) -> Self::Output {
26
+ async fn call(&self, req: Request) -> Result<Self::Output> {
27
27
  if req.method() != Method::GET {
28
- return StatusCode::METHOD_NOT_ALLOWED.into();
28
+ return Ok(StatusCode::METHOD_NOT_ALLOWED.into());
29
29
  }
30
30
 
31
31
  let mut path = req.uri().path().trim_start_matches('/').trim_end_matches('/').to_string();
@@ -46,18 +46,20 @@ impl Endpoint for StaticEmbed {
46
46
  .map(|etag| etag.to_str().unwrap_or("000000").eq(&hash))
47
47
  .unwrap_or(false)
48
48
  {
49
- return StatusCode::NOT_MODIFIED.into();
49
+ return Ok(StatusCode::NOT_MODIFIED.into());
50
50
  }
51
51
 
52
52
  // otherwise, return 200 with etag hash
53
53
  let body: Vec<u8> = content.data.into();
54
54
  let mime = mime_guess::from_path(path).first_or_octet_stream();
55
+ Ok(
55
- Response::builder()
56
+ Response::builder()
56
- .header(header::CONTENT_TYPE, mime.as_ref())
57
+ .header(header::CONTENT_TYPE, mime.as_ref())
57
- .header(header::ETAG, hash)
58
+ .header(header::ETAG, hash)
58
- .body(body)
59
+ .body(body),
60
+ )
59
61
  }
60
- None => Response::builder().status(StatusCode::NOT_FOUND).finish(),
62
+ None => Ok(Response::builder().status(StatusCode::NOT_FOUND).finish()),
61
63
  }
62
64
  }
63
65
  }
utils/src/lib.rs CHANGED
@@ -55,6 +55,7 @@ pub fn is_path_included(rel_path: &str, includes: &[&str], excludes: &[&str]) ->
55
55
  pub fn get_files<'patterns>(folder_path: String, includes: &'patterns [&str], excludes: &'patterns [&str]) -> impl Iterator<Item = FileEntry> + 'patterns {
56
56
  walkdir::WalkDir::new(&folder_path)
57
57
  .follow_links(true)
58
+ .sort_by_file_name()
58
59
  .into_iter()
59
60
  .filter_map(|e| e.ok())
60
61
  .filter(|e| e.file_type().is_file())