Make example test more robust & improve docs

This commit is contained in:
Tristan D. 2025-04-14 18:53:29 +02:00
parent f049d39bb8
commit 8d2faf1f64
Signed by: tristan
SSH key fingerprint: SHA256:9oFM1J63hYWJjCnLG6C0fxBS15rwNcWwdQNMOHYKJ/4
4 changed files with 35 additions and 25 deletions

View file

@ -1,28 +1,7 @@
use axum::Router; mod server;
use axum_folder_router::folder_router;
#[derive(Clone, Debug)]
struct AppState {
_foo: String,
}
folder_router!("examples/advanced/api", AppState);
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
// Create app state server::server().await?;
let app_state = AppState {
_foo: "".to_string(),
};
// Generate the router using the macro
let folder_router: Router<AppState> = 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(()) Ok(())
} }

View file

@ -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<AppState> = 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(())
}

View file

@ -6,6 +6,7 @@ struct AppState {
_foo: String, _foo: String,
} }
// Imports route.rs files & generates an init fn
folder_router!("./examples/simple/api", AppState); folder_router!("./examples/simple/api", AppState);
#[tokio::main] #[tokio::main]
@ -15,7 +16,7 @@ async fn main() -> anyhow::Result<()> {
_foo: "".to_string(), _foo: "".to_string(),
}; };
// Generate the router using the macro // Use the init fn generated above
let folder_router: Router<AppState> = folder_router(); let folder_router: Router<AppState> = folder_router();
// Build the router and provide the state // Build the router and provide the state

View file

@ -11,7 +11,7 @@
//! //!
//! ```toml //! ```toml
//! [dependencies] //! [dependencies]
//! axum_folder_router = "0.1.0" //! axum_folder_router = "0.2"
//! axum = "0.8" //! axum = "0.8"
//! ``` //! ```
//! //!
@ -120,6 +120,8 @@
//! ## Limitations //! ## Limitations
//! //!
//! - **Compile-time Only**: The routing is determined at compile time, so dynamic route registration isn't supported. //! - **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::{ use std::{
collections::HashMap, collections::HashMap,
fs, fs,