small refactor for the varius app state and nesting of api paths
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 6m54s

This commit is contained in:
Dmitri 2026-04-16 13:16:53 +02:00
parent 4b9ef4691e
commit 1cb32e3d2c
Signed by: kanopo
GPG Key ID: 759ADD40E3132AC7
6 changed files with 43 additions and 15 deletions

5
src/app_state.rs Normal file
View File

@ -0,0 +1,5 @@
#[derive(Clone)]
pub struct AppState {
#[allow(dead_code)]
pub db: sqlx::PgPool,
}

10
src/http/api_router.rs Normal file
View File

@ -0,0 +1,10 @@
use crate::app_state::AppState;
use axum::Router;
use crate::http::{auth_router, health_router};
pub fn router() -> Router<AppState> {
Router::new()
.nest("/health", health_router::router())
.nest("/auth", auth_router::router())
}

17
src/http/auth_router.rs Normal file
View File

@ -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<AppState> {
Router::new().route("/register", post(register))
}
async fn register() -> Json<HealthResponse> {
Json(HealthResponse { status: "ok" })
}

View File

@ -1,16 +1,15 @@
use axum::{Json, Router, routing::get}; use axum::{Json, Router, routing::get};
use serde::Serialize; use serde::Serialize;
use crate::app_state::AppState;
#[derive(Serialize)] #[derive(Serialize)]
struct HealthResponse { struct HealthResponse {
status: &'static str, status: &'static str,
} }
pub fn router<S>() -> Router<S> pub fn router() -> Router<AppState> {
where Router::new().route("/", get(get_health))
S: Clone + Send + Sync + 'static,
{
Router::new().route("/health", get(get_health))
} }
async fn get_health() -> Json<HealthResponse> { async fn get_health() -> Json<HealthResponse> {

View File

@ -1 +1,3 @@
pub mod public_router; pub mod api_router;
mod auth_router;
mod health_router;

View File

@ -1,3 +1,4 @@
mod app_state;
mod config; mod config;
mod db; mod db;
mod error; mod error;
@ -5,18 +6,12 @@ mod http;
use crate::db::database; use crate::db::database;
use crate::error::AppError; use crate::error::AppError;
use crate::http::public_router; use crate::http::api_router;
use axum::Router; use axum::Router;
use tower_http::trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer}; use tower_http::trace::{DefaultOnRequest, DefaultOnResponse, TraceLayer};
use tracing::{Level, info}; use tracing::{Level, info};
use tracing_subscriber::{EnvFilter, fmt}; use tracing_subscriber::{EnvFilter, fmt};
#[derive(Clone)]
struct AppState {
#[allow(dead_code)]
db: sqlx::PgPool,
}
fn init_tracing() { fn init_tracing() {
let filter = EnvFilter::try_from_default_env() let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info,tower_http=info,sqlx=warn")); .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?; let pool = database::create_pool(&cfg.database_url()).await?;
info!("database connection established"); info!("database connection established");
let state = AppState { db: pool }; let state = app_state::AppState { db: pool };
let app = Router::new() let app = Router::new()
.merge(public_router::router()) .nest("/api", api_router::router())
.with_state(state) .with_state(state)
.layer( .layer(
TraceLayer::new_for_http() TraceLayer::new_for_http()