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


b7ff1434 lazywalker

4 years ago
fix the 404 and broken images of index.html
Files changed (1) hide show
  1. examples/axum.rs +13 -2
examples/axum.rs CHANGED
@@ -19,6 +19,9 @@ async fn main() {
19
19
  let app = Router::new()
20
20
  .route("/hello", get(helloworld))
21
21
  // handle static files with rust_embed
22
+ .route("/", get(index_handler))
23
+ .route("/index.html", get(index_handler))
24
+ .route("/dist/", static_handler.into_service())
22
25
  .or(static_handler.into_service());
23
26
 
24
27
  // run it
@@ -31,9 +34,17 @@ async fn helloworld() -> Html<&'static str> {
31
34
  Html("<h1>Hello, World!</h1>")
32
35
  }
33
36
 
37
+ // serve index.html from examples/public/index.html
34
- async fn static_handler(uri: Uri) -> impl IntoResponse {
38
+ async fn index_handler() -> impl IntoResponse {
35
- let path = uri.path().trim_start_matches('/').to_string();
39
+ static_handler("/index.html".parse::<Uri>().unwrap()).await
40
+ }
36
41
 
42
+ // static_handler is a handler that serves static files from the
43
+ async fn static_handler(uri: Uri) -> impl IntoResponse {
44
+ let mut path = uri.path().trim_start_matches('/').to_string();
45
+ if path.starts_with("dist/") {
46
+ path = path.replace("dist/", "");
47
+ }
37
48
  StaticFile(path)
38
49
  }
39
50