Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/static_files/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/websocket/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion ohkami/src/ohkami/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl StaticFile {
}

impl Dir {
pub(super) fn new(route: &'static str, dir_path: PathBuf) -> io::Result<Self> {
pub(super) fn new(route: &'static str, dir_path: &Path) -> io::Result<Self> {
let dir_path = dir_path.canonicalize()?;

if !dir_path.is_dir() {
Expand Down
4 changes: 2 additions & 2 deletions ohkami/src/ohkami/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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"),
/// )
/// # }
/// ```
Expand Down
22 changes: 11 additions & 11 deletions ohkami/src/ohkami/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ macro_rules! Route {
/// )).howl("localhost:3000").await
/// }
/// ```
pub trait Route {
pub trait Route: Sized {
$(
fn $method<T>(self, handler: impl IntoHandler<T>) -> HandlerSet;
)*
Expand All @@ -120,7 +120,13 @@ 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<std::path::Path>) -> Dir;

#[deprecated(note = "Use `Mount` instead")]
#[cfg(feature="__rt_native__")]
fn Dir(self, path: &'static str) -> Dir {
self.Mount(path)
}
}

impl Route for &'static str {
Expand All @@ -138,17 +144,11 @@ macro_rules! Route {
}

#[cfg(feature="__rt_native__")]
fn Dir(self, path: &'static str) -> Dir {
fn Mount(self, path: impl AsRef<std::path::Path>) -> 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()))
}
}
};
Expand Down