~repos /rust-embed
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.
a1f5565d
—
Peter John 2 years ago
Merge pull request #213 from hongquan/feature/shorter-axum
- examples/axum-spa/main.rs +4 -10
- 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::{Html, IntoResponse, Response},
|
|
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
|
-
|
|
35
|
+
([(header::CONTENT_TYPE, mime.as_ref())], content.data).into_response()
|
|
38
36
|
}
|
|
39
37
|
None => {
|
|
40
38
|
if path.contains('.') {
|
|
@@ -48,15 +46,11 @@ async fn static_handler(uri: Uri) -> impl IntoResponse {
|
|
|
48
46
|
|
|
49
47
|
async fn index_html() -> Response {
|
|
50
48
|
match Assets::get(INDEX_HTML) {
|
|
51
|
-
Some(content) => {
|
|
52
|
-
|
|
49
|
+
Some(content) => Html(content.data).into_response(),
|
|
53
|
-
|
|
54
|
-
Response::builder().header(header::CONTENT_TYPE, "text/html").body(body).unwrap()
|
|
55
|
-
}
|
|
56
50
|
None => not_found().await,
|
|
57
51
|
}
|
|
58
52
|
}
|
|
59
53
|
|
|
60
54
|
async fn not_found() -> Response {
|
|
61
|
-
|
|
55
|
+
(StatusCode::NOT_FOUND, "404").into_response()
|
|
62
56
|
}
|
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
|
-
|
|
64
|
+
([(header::CONTENT_TYPE, mime.as_ref())], content.data).into_response()
|
|
67
65
|
}
|
|
68
|
-
None =>
|
|
66
|
+
None => (StatusCode::NOT_FOUND, "404 Not Found").into_response(),
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
69
|
}
|