From 1cb32e3d2c3761e50351443c6b59cac8f5e4e066 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Thu, 16 Apr 2026 13:16:53 +0200 Subject: [PATCH] small refactor for the varius app state and nesting of api paths --- src/app_state.rs | 5 +++++ src/http/api_router.rs | 10 ++++++++++ src/http/auth_router.rs | 17 +++++++++++++++++ src/http/{public_router.rs => health_router.rs} | 9 ++++----- src/http/mod.rs | 4 +++- src/main.rs | 13 ++++--------- 6 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 src/app_state.rs create mode 100644 src/http/api_router.rs create mode 100644 src/http/auth_router.rs rename src/http/{public_router.rs => health_router.rs} (64%) diff --git a/src/app_state.rs b/src/app_state.rs new file mode 100644 index 0000000..93944d9 --- /dev/null +++ b/src/app_state.rs @@ -0,0 +1,5 @@ +#[derive(Clone)] +pub struct AppState { + #[allow(dead_code)] + pub db: sqlx::PgPool, +} diff --git a/src/http/api_router.rs b/src/http/api_router.rs new file mode 100644 index 0000000..d5454da --- /dev/null +++ b/src/http/api_router.rs @@ -0,0 +1,10 @@ +use crate::app_state::AppState; +use axum::Router; + +use crate::http::{auth_router, health_router}; + +pub fn router() -> Router { + Router::new() + .nest("/health", health_router::router()) + .nest("/auth", auth_router::router()) +} diff --git a/src/http/auth_router.rs b/src/http/auth_router.rs new file mode 100644 index 0000000..c4795f7 --- /dev/null +++ b/src/http/auth_router.rs @@ -0,0 +1,17 @@ +use axum::{Json, Router, routing::post}; +use serde::Serialize; + +use crate::app_state::AppState; + +#[derive(Serialize)] +struct HealthResponse { + status: &'static str, +} + +pub fn router() -> Router { + Router::new().route("/register", post(register)) +} + +async fn register() -> Json { + Json(HealthResponse { status: "ok" }) +} diff --git a/src/http/public_router.rs b/src/http/health_router.rs similarity index 64% rename from src/http/public_router.rs rename to src/http/health_router.rs index f905fc0..da437d3 100644 --- a/src/http/public_router.rs +++ b/src/http/health_router.rs @@ -1,16 +1,15 @@ use axum::{Json, Router, routing::get}; use serde::Serialize; +use crate::app_state::AppState; + #[derive(Serialize)] struct HealthResponse { status: &'static str, } -pub fn router() -> Router -where - S: Clone + Send + Sync + 'static, -{ - Router::new().route("/health", get(get_health)) +pub fn router() -> Router { + Router::new().route("/", get(get_health)) } async fn get_health() -> Json { diff --git a/src/http/mod.rs b/src/http/mod.rs index 2650074..4c87eef 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -1 +1,3 @@ -pub mod public_router; +pub mod api_router; +mod auth_router; +mod health_router; diff --git a/src/main.rs b/src/main.rs index d412be4..45d7d92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod app_state; mod config; mod db; mod error; @@ -5,18 +6,12 @@ mod http; use crate::db::database; use crate::error::AppError; -use crate::http::public_router; +use crate::http::api_router; use axum::Router; use tower_http::trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer}; use tracing::{Level, info}; use tracing_subscriber::{EnvFilter, fmt}; -#[derive(Clone)] -struct AppState { - #[allow(dead_code)] - db: sqlx::PgPool, -} - fn init_tracing() { let filter = EnvFilter::try_from_default_env() .unwrap_or_else(|_| EnvFilter::new("info,tower_http=info,sqlx=warn")); @@ -32,9 +27,9 @@ async fn main() -> Result<(), AppError> { let pool = database::create_pool(&cfg.database_url()).await?; info!("database connection established"); - let state = AppState { db: pool }; + let state = app_state::AppState { db: pool }; let app = Router::new() - .merge(public_router::router()) + .nest("/api", api_router::router()) .with_state(state) .layer( TraceLayer::new_for_http()