~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.
7f1de531
—
Nguyễn Hồng Quân 2 years ago
Shorter example code for axum
- examples/axum-spa/main.rs +4 -8
- 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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|