From 2efaa864406dabc83f49520d414fed071fe5f97c Mon Sep 17 00:00:00 2001 From: fa-sharp Date: Fri, 20 Feb 2026 03:56:40 -0500 Subject: [PATCH] add hello routes to template --- src/lib.rs | 7 ++++++- src/routes/hello.rs | 16 ++++++++++++++++ src/routes/mod.rs | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/routes/hello.rs create mode 100644 src/routes/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 56de26f..4b5aa63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,10 +3,15 @@ use axum_app_wrapper::App; use crate::config::AppConfig; mod config; +mod routes; mod state; pub async fn create_app() -> anyhow::Result<(axum::Router, AppConfig, impl Future + Send)> { - let (router, state, on_shutdown) = App::new().register(config::plugin()).init().await?; + let (router, state, on_shutdown) = App::new() + .register(config::plugin()) // Extract configuration and add to state + .register(routes::plugin()) // Add API routes + .init() + .await?; let app_config = state.config.to_owned(); Ok((router.with_state(state), app_config, on_shutdown)) diff --git a/src/routes/hello.rs b/src/routes/hello.rs new file mode 100644 index 0000000..ad9df18 --- /dev/null +++ b/src/routes/hello.rs @@ -0,0 +1,16 @@ +use crate::state::AppState; + +/// `/api/hello` +pub fn routes() -> axum::Router { + axum::Router::new() + .route("/", axum::routing::get(hello_handler)) + .route("/", axum::routing::post(post_handler)) +} + +async fn hello_handler() -> String { + "Hello, World!".to_string() +} + +async fn post_handler() -> String { + "Post handler!".to_string() +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..3c17435 --- /dev/null +++ b/src/routes/mod.rs @@ -0,0 +1,14 @@ +use axum_app_wrapper::AdHocPlugin; + +use crate::state::AppState; + +pub mod hello; + +/// Adds all API routes to the server +pub fn plugin() -> AdHocPlugin { + AdHocPlugin::new().on_setup(|router, _state| { + let router = router.nest("/api/hello", hello::routes()); + + Ok(router) + }) +}