diff --git a/examples/advanced/main.rs b/examples/advanced/main.rs index 67b7e3c..3a656e9 100644 --- a/examples/advanced/main.rs +++ b/examples/advanced/main.rs @@ -1,28 +1,7 @@ -use axum::Router; -use axum_folder_router::folder_router; - -#[derive(Clone, Debug)] -struct AppState { - _foo: String, -} - -folder_router!("examples/advanced/api", AppState); +mod server; #[tokio::main] async fn main() -> anyhow::Result<()> { - // Create app state - let app_state = AppState { - _foo: "".to_string(), - }; - - // Generate the router using the macro - let folder_router: Router = folder_router(); - - // Build the router and provide the state - let app: Router<()> = folder_router.with_state(app_state); - - let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; - println!("Listening on http://{}", listener.local_addr()?); - axum::serve(listener, app).await?; + server::server().await?; Ok(()) } diff --git a/examples/advanced/server.rs b/examples/advanced/server.rs new file mode 100644 index 0000000..7aa1acb --- /dev/null +++ b/examples/advanced/server.rs @@ -0,0 +1,28 @@ +use axum::Router; +use axum_folder_router::folder_router; + +#[derive(Clone, Debug)] +struct AppState { + _foo: String, +} + +// Imports route.rs files & generates an init fn +folder_router!("examples/advanced/api", AppState); + +pub async fn server() -> anyhow::Result<()> { + // Create app state + let app_state = AppState { + _foo: "".to_string(), + }; + + // Use the init fn generated above + let folder_router: Router = folder_router(); + + // Build the router and provide the state + let app: Router<()> = folder_router.with_state(app_state); + + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; + println!("Listening on http://{}", listener.local_addr()?); + axum::serve(listener, app).await?; + Ok(()) +} diff --git a/examples/simple/main.rs b/examples/simple/main.rs index 6a22e04..c315ee1 100644 --- a/examples/simple/main.rs +++ b/examples/simple/main.rs @@ -6,6 +6,7 @@ struct AppState { _foo: String, } +// Imports route.rs files & generates an init fn folder_router!("./examples/simple/api", AppState); #[tokio::main] @@ -15,7 +16,7 @@ async fn main() -> anyhow::Result<()> { _foo: "".to_string(), }; - // Generate the router using the macro + // Use the init fn generated above let folder_router: Router = folder_router(); // Build the router and provide the state diff --git a/src/lib.rs b/src/lib.rs index aa9dd06..0d9e79b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ //! //! ```toml //! [dependencies] -//! axum_folder_router = "0.1.0" +//! axum_folder_router = "0.2" //! axum = "0.8" //! ``` //! @@ -120,6 +120,8 @@ //! ## Limitations //! //! - **Compile-time Only**: The routing is determined at compile time, so dynamic route registration isn't supported. +//! - **Expects seperate directory**: To make rust-analyzer & co work correctly the macro imports all route.rs files inside the given directory tree. +//! It is highly recommended to keep the route directory seperate from the rest of your module-tree. use std::{ collections::HashMap, fs,