浏览代码

fix salvo serving static files

Evan Almloff 2 年之前
父节点
当前提交
987a0d5532
共有 1 个文件被更改,包括 12 次插入5 次删除
  1. 12 5
      packages/server/src/adapters/salvo_adapter.rs

+ 12 - 5
packages/server/src/adapters/salvo_adapter.rs

@@ -2,8 +2,9 @@ use std::{error::Error, sync::Arc};
 
 
 use hyper::{http::HeaderValue, StatusCode};
 use hyper::{http::HeaderValue, StatusCode};
 use salvo::{
 use salvo::{
-    async_trait, handler, serve_static::StaticDir, Depot, FlowCtrl, Handler, Request, Response,
-    Router,
+    async_trait, handler,
+    serve_static::{StaticDir, StaticFile},
+    Depot, FlowCtrl, Handler, Request, Response, Router,
 };
 };
 use server_fn::{Payload, ServerFunctionRegistry};
 use server_fn::{Payload, ServerFunctionRegistry};
 use tokio::task::spawn_blocking;
 use tokio::task::spawn_blocking;
@@ -80,7 +81,6 @@ impl DioxusRouterExt for Router {
             if path.ends_with("index.html") {
             if path.ends_with("index.html") {
                 continue;
                 continue;
             }
             }
-            let serve_dir = StaticDir::new([path.clone()]);
             let route = path
             let route = path
                 .strip_prefix(&cfg.assets_path)
                 .strip_prefix(&cfg.assets_path)
                 .unwrap()
                 .unwrap()
@@ -92,8 +92,15 @@ impl DioxusRouterExt for Router {
                 })
                 })
                 .collect::<Vec<_>>()
                 .collect::<Vec<_>>()
                 .join("/");
                 .join("/");
-            let route = format!("/{}/<**path>", route);
-            self = self.push(Router::with_path(route).get(serve_dir))
+            if path.is_file() {
+                let route = format!("/{}", route);
+                let serve_dir = StaticFile::new(path.clone());
+                self = self.push(Router::with_path(route).get(serve_dir))
+            } else {
+                let route = format!("/{}/<**path>", route);
+                let serve_dir = StaticDir::new([path.clone()]);
+                self = self.push(Router::with_path(route).get(serve_dir))
+            }
         }
         }
 
 
         self.connect_hot_reload()
         self.connect_hot_reload()