~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. examples/axum-spa/main.rs +4 -8
  2. examples/axum.rs +2 -4
examples/axum-spa/main.rs CHANGED
@@ -1,7 +1,6 @@
1
1
  use axum::{
2
- body::{boxed, Full},
3
2
  http::{header, StatusCode, Uri},
4
- response::{IntoResponse, Response},
3
+ response::{IntoResponse, Response, Html},
5
4
  routing::Router,
6
5
  };
7
6
  use rust_embed::RustEmbed;
@@ -31,10 +30,9 @@ async fn static_handler(uri: Uri) -> impl IntoResponse {
31
30
 
32
31
  match Assets::get(path) {
33
32
  Some(content) => {
34
- let body = boxed(Full::from(content.data));
35
33
  let mime = mime_guess::from_path(path).first_or_octet_stream();
36
34
 
37
- Response::builder().header(header::CONTENT_TYPE, mime.as_ref()).body(body).unwrap()
35
+ ([(header::CONTENT_TYPE, mime.as_ref())], content.data).into_response()
38
36
  }
39
37
  None => {
40
38
  if path.contains('.') {
@@ -49,14 +47,12 @@ async fn static_handler(uri: Uri) -> impl IntoResponse {
49
47
  async fn index_html() -> Response {
50
48
  match Assets::get(INDEX_HTML) {
51
49
  Some(content) => {
52
- let body = boxed(Full::from(content.data));
50
+ Html(content.data).into_response()
53
-
54
- Response::builder().header(header::CONTENT_TYPE, "text/html").body(body).unwrap()
55
51
  }
56
52
  None => not_found().await,
57
53
  }
58
54
  }
59
55
 
60
56
  async fn not_found() -> Response {
61
- Response::builder().status(StatusCode::NOT_FOUND).body(boxed(Full::from("404"))).unwrap()
57
+ (StatusCode::NOT_FOUND, "404").into_response()
62
58
  }
examples/axum.rs CHANGED
@@ -1,5 +1,4 @@
1
1
  use axum::{
2
- body::{boxed, Full},
3
2
  http::{header, StatusCode, Uri},
4
3
  response::{Html, IntoResponse, Response},
5
4
  routing::{get, Router},
@@ -61,11 +60,10 @@ where
61
60
 
62
61
  match Asset::get(path.as_str()) {
63
62
  Some(content) => {
64
- let body = boxed(Full::from(content.data));
65
63
  let mime = mime_guess::from_path(path).first_or_octet_stream();
66
- Response::builder().header(header::CONTENT_TYPE, mime.as_ref()).body(body).unwrap()
64
+ ([(header::CONTENT_TYPE, mime.as_ref())], content.data).into_response()
67
65
  }
68
- None => Response::builder().status(StatusCode::NOT_FOUND).body(boxed(Full::from("404"))).unwrap(),
66
+ None => (StatusCode::NOT_FOUND, "404 Not Found").into_response(),
69
67
  }
70
68
  }
71
69
  }