From 55ade3a0d5500ccaa8b8852496b4c6b51e2f4a37 Mon Sep 17 00:00:00 2001 From: kanarus Date: Mon, 11 Aug 2025 04:50:24 +0900 Subject: [PATCH 1/3] enhance(Route): deprecate `.Dir()` and introduce `.Mount()` --- README.md | 4 ++-- examples/static_files/src/main.rs | 2 +- examples/websocket/src/main.rs | 2 +- ohkami/src/ohkami/dir.rs | 2 +- ohkami/src/ohkami/mod.rs | 4 ++-- ohkami/src/ohkami/routing.rs | 19 +++++++++---------- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f4c3fea74..8300decb6 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Hello, your_name! - `.GET()`, `.POST()`, `.PUT()`, `.PATCH()`, `.DELETE()`, `.OPTIONS()` to define API endpoints - `.By({another Ohkami})` to nest `Ohkami`s -- `.Dir({dir_path})` to serve static directory +- `.Mount({directory_path})` to serve static directory (pre-compressed files with `gzip`, `deflate`, `br`, `zstd` are supported) Here `GET`, `POST`, etc. takes a *handler* function: @@ -714,7 +714,7 @@ use ohkami::{Ohkami, Route}; #[tokio::main] async fn main() { Ohkami::new(( - "/".Dir("./dist"), + "/".Mount("./dist"), )).howl("0.0.0.0:3030").await } ``` diff --git a/examples/static_files/src/main.rs b/examples/static_files/src/main.rs index bc35e0338..c64a129a9 100644 --- a/examples/static_files/src/main.rs +++ b/examples/static_files/src/main.rs @@ -17,7 +17,7 @@ impl Default for Options { fn ohkami(Options { omit_dot_html, serve_dotfiles, etag }: Options) -> Ohkami { Ohkami::new(( - "/".Dir("./public") + "/".Mount("./public") .omit_extensions(if omit_dot_html {&["html"]} else {&[]}) .serve_dotfiles(serve_dotfiles) .etag(etag), diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index a4b476a9f..7f40d0761 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -144,7 +144,7 @@ async fn main() { } Ohkami::new((Logger, - "/".Dir("./template").omit_extensions(&[".html"]), + "/".Mount("./template").omit_extensions(&[".html"]), "/echo1".GET(echo_text), "/echo2/:name".GET(echo_text_2), "/echo3/:name".GET(echo_text_3), diff --git a/ohkami/src/ohkami/dir.rs b/ohkami/src/ohkami/dir.rs index d1950cdeb..8f601925f 100644 --- a/ohkami/src/ohkami/dir.rs +++ b/ohkami/src/ohkami/dir.rs @@ -59,7 +59,7 @@ impl StaticFile { } impl Dir { - pub(super) fn new(route: &'static str, dir_path: PathBuf) -> io::Result { + pub(super) fn new(route: &'static str, dir_path: &Path) -> io::Result { let dir_path = dir_path.canonicalize()?; if !dir_path.is_dir() { diff --git a/ohkami/src/ohkami/mod.rs b/ohkami/src/ohkami/mod.rs index faa0595fc..38bac67ea 100644 --- a/ohkami/src/ohkami/mod.rs +++ b/ohkami/src/ohkami/mod.rs @@ -426,7 +426,7 @@ impl Ohkami { /// /// ### static directory serving /// - /// `.Dir` mounts a directory and serves all files in it/its sub directories. + /// `.Mount({directory_path})` mounts a directory and serves all files in it/its sub directories. /// /// This doesn't work on `rt_worker` ( of course because there Ohkami can't /// touch your local file system ). Consider using `asset` of wrangler.{toml/json} @@ -438,7 +438,7 @@ impl Ohkami { /// # fn __() -> Ohkami { /// # let another_ohkami = Ohkami::new(()); /// Ohkami::new( - /// "/public".Dir("./path/to/dir"), + /// "/public".Mount("./path/to/dir"), /// ) /// # } /// ``` diff --git a/ohkami/src/ohkami/routing.rs b/ohkami/src/ohkami/routing.rs index bb2af9cf6..a1ca1b411 100644 --- a/ohkami/src/ohkami/routing.rs +++ b/ohkami/src/ohkami/routing.rs @@ -120,7 +120,12 @@ macro_rules! Route { /// Both pre-compressed file(s) and the original file are required to be in the directory. /// /// See methods's docs for options. - fn Dir(self, static_dir_path: &'static str) -> Dir; + fn Mount(self, directory_path: impl AsRef) -> Dir; + + #[deprecated(note = "Use `Mount` instead")] + fn Dir(self, path: &'static str) -> Dir { + self.Mount(path) + } } impl Route for &'static str { @@ -138,17 +143,11 @@ macro_rules! Route { } #[cfg(feature="__rt_native__")] - fn Dir(self, path: &'static str) -> Dir { + fn Mount(self, path: impl AsRef) -> Dir { // Check `self` is valid route let _ = RouteSegments::from_literal(self); - - match Dir::new( - self, - path.into() - ) { - Ok(dir) => dir, - Err(e) => panic!("{e}") - } + + Dir::new(self, path.as_ref()).expect(&format!("invalid path to serve: `{}`", path.as_ref().display())) } } }; From 28f209273fe7c34f5337816e32e04b1451046aeb Mon Sep 17 00:00:00 2001 From: kanarus Date: Mon, 11 Aug 2025 04:56:47 +0900 Subject: [PATCH 2/3] add `: Sized` constraint to `Route` --- ohkami/src/ohkami/routing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ohkami/src/ohkami/routing.rs b/ohkami/src/ohkami/routing.rs index a1ca1b411..4b308c710 100644 --- a/ohkami/src/ohkami/routing.rs +++ b/ohkami/src/ohkami/routing.rs @@ -101,7 +101,7 @@ macro_rules! Route { /// )).howl("localhost:3000").await /// } /// ``` - pub trait Route { + pub trait Route: Sized { $( fn $method(self, handler: impl IntoHandler) -> HandlerSet; )* From 8406fbc7c39724327c6e3fdf670400bdc11c3676 Mon Sep 17 00:00:00 2001 From: kanarus Date: Mon, 11 Aug 2025 04:59:55 +0900 Subject: [PATCH 3/3] complete cfg --- ohkami/src/ohkami/routing.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ohkami/src/ohkami/routing.rs b/ohkami/src/ohkami/routing.rs index 4b308c710..884f39bfe 100644 --- a/ohkami/src/ohkami/routing.rs +++ b/ohkami/src/ohkami/routing.rs @@ -123,6 +123,7 @@ macro_rules! Route { fn Mount(self, directory_path: impl AsRef) -> Dir; #[deprecated(note = "Use `Mount` instead")] + #[cfg(feature="__rt_native__")] fn Dir(self, path: &'static str) -> Dir { self.Mount(path) }