~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.


451568cd Peter John

3 years ago
Merge pull request #175 from pyrossh/update/actix-4.0
Files changed (2) hide show
  1. Cargo.toml +1 -1
  2. examples/actix.rs +10 -22
Cargo.toml CHANGED
@@ -52,7 +52,7 @@ rust-embed-impl = { version = "6.2.0", path = "impl"}
52
52
  rust-embed-utils = { version = "7.2.0", path = "utils"}
53
53
 
54
54
  include-flate = { version = "0.1", optional = true, features = ["stable"] }
55
- actix-web = { version = "3", default-features = false, optional = true }
55
+ actix-web = { version = "4", optional = true }
56
56
  mime_guess = { version = "2", optional = true }
57
57
  hex = { version = "0.4.3", optional = true }
58
58
  tokio = { version = "1.0", optional = true, features = ["macros", "rt-multi-thread"] }
examples/actix.rs CHANGED
@@ -1,43 +1,31 @@
1
- use actix_web::body::Body;
2
- use actix_web::{web, App, HttpResponse, HttpServer};
1
+ use actix_web::{web, App, HttpResponse, HttpServer, Responder};
3
2
  use mime_guess::from_path;
4
3
  use rust_embed::RustEmbed;
5
4
 
6
- use std::borrow::Cow;
7
-
8
5
  #[derive(RustEmbed)]
9
6
  #[folder = "examples/public/"]
10
7
  struct Asset;
11
8
 
12
9
  fn handle_embedded_file(path: &str) -> HttpResponse {
13
10
  match Asset::get(path) {
14
- Some(content) => {
11
+ Some(content) => HttpResponse::Ok()
15
- let body: Body = match content.data {
16
- Cow::Borrowed(bytes) => bytes.into(),
17
- Cow::Owned(bytes) => bytes.into(),
18
- };
19
- HttpResponse::Ok().content_type(from_path(path).first_or_octet_stream().as_ref()).body(body)
12
+ .content_type(from_path(path).first_or_octet_stream().as_ref())
20
- }
13
+ .body(content.data.into_owned()),
21
14
  None => HttpResponse::NotFound().body("404 Not Found"),
22
15
  }
23
16
  }
24
17
 
18
+ #[actix_web::get("/")]
25
- fn index() -> HttpResponse {
19
+ async fn index() -> impl Responder {
26
20
  handle_embedded_file("index.html")
27
21
  }
28
22
 
23
+ #[actix_web::get("/dist/{_:.*}")]
29
- fn dist(path: web::Path<String>) -> HttpResponse {
24
+ async fn dist(path: web::Path<String>) -> impl Responder {
30
- handle_embedded_file(&path.0)
25
+ handle_embedded_file(path.as_str())
31
26
  }
32
27
 
33
28
  #[actix_web::main]
34
29
  async fn main() -> std::io::Result<()> {
35
- HttpServer::new(|| {
36
- App::new()
37
- .service(web::resource("/").route(web::get().to(index)))
38
- .service(web::resource("/dist/{_:.*}").route(web::get().to(dist)))
30
+ HttpServer::new(|| App::new().service(index).service(dist)).bind("127.0.0.1:8000")?.run().await
39
- })
40
- .bind("127.0.0.1:8000")?
41
- .run()
42
- .await
43
31
  }