add hello routes to template
This commit is contained in:
@@ -3,10 +3,15 @@ use axum_app_wrapper::App;
|
|||||||
use crate::config::AppConfig;
|
use crate::config::AppConfig;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
mod routes;
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
pub async fn create_app() -> anyhow::Result<(axum::Router, AppConfig, impl Future + Send)> {
|
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();
|
let app_config = state.config.to_owned();
|
||||||
|
|
||||||
Ok((router.with_state(state), app_config, on_shutdown))
|
Ok((router.with_state(state), app_config, on_shutdown))
|
||||||
|
|||||||
16
src/routes/hello.rs
Normal file
16
src/routes/hello.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use crate::state::AppState;
|
||||||
|
|
||||||
|
/// `/api/hello`
|
||||||
|
pub fn routes() -> axum::Router<AppState> {
|
||||||
|
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()
|
||||||
|
}
|
||||||
14
src/routes/mod.rs
Normal file
14
src/routes/mod.rs
Normal file
@@ -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<AppState> {
|
||||||
|
AdHocPlugin::new().on_setup(|router, _state| {
|
||||||
|
let router = router.nest("/api/hello", hello::routes());
|
||||||
|
|
||||||
|
Ok(router)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user