~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/actix.rs +9 -3
Cargo.toml CHANGED
@@ -18,7 +18,8 @@ syn = "0.11"
18
18
  quote = "0.3"
19
19
  walkdir = "2.1.4"
20
20
 
21
- actix-web = { version = "0.6", optional = true }
21
+ actix-web = { version = "0.7", optional = true }
22
+ mime_guess = { version = "2.0.0-alpha.6", optional = true }
22
23
 
23
24
  rocket = { version = "0.3.6", optional = true }
24
25
  rocket_codegen = { version = "0.3.6", optional = true }
@@ -26,7 +27,7 @@ rocket_contrib = { version = "0.3.6", optional = true }
26
27
 
27
28
  [features]
28
29
  nightly = ["rocket", "rocket_codegen", "rocket_contrib"]
29
- actix = ["actix-web"]
30
+ actix = ["actix-web", "mime_guess"]
30
31
 
31
32
  [badges]
32
33
  appveyor = { repository = "pyros2097/rust-embed" }
examples/actix.rs CHANGED
@@ -1,9 +1,11 @@
1
1
  extern crate actix_web;
2
2
  #[macro_use]
3
3
  extern crate rust_embed;
4
+ extern crate mime_guess;
4
5
 
5
6
  use actix_web::{App, HttpRequest, HttpResponse, server};
6
7
  use actix_web::http::Method;
8
+ use mime_guess::guess_mime_type;
7
9
 
8
10
  #[derive(RustEmbed)]
9
11
  #[folder = "examples/public/"]
@@ -11,7 +13,11 @@ struct Asset;
11
13
 
12
14
  fn handle_embedded_file(path: &str) -> HttpResponse {
13
15
  match Asset::get(path) {
16
+ Some(content) => {
17
+ HttpResponse::Ok()
14
- Some(content) => HttpResponse::Ok().body(content),
18
+ .content_type(guess_mime_type(path).as_ref())
19
+ .body(content)
20
+ }
15
21
  None => HttpResponse::NotFound().body("404 Not Found"),
16
22
  }
17
23
  }
@@ -21,14 +27,14 @@ fn index(_req: HttpRequest) -> HttpResponse {
21
27
  }
22
28
 
23
29
  fn dist(req: HttpRequest) -> HttpResponse {
24
- let path = &req.path()["/dist/".len()..];
30
+ let path = &req.path()["/dist/".len()..]; // trim the preceding `/dist/` in path
25
31
  handle_embedded_file(path)
26
32
  }
27
33
 
28
34
  fn main() {
29
35
  server::new(|| {
30
36
  App::new().route("/", Method::GET, index).route(
31
- "/dist{_:.*}",
37
+ "/dist/{_:.*}",
32
38
  Method::GET,
33
39
  dist,
34
40
  )