From 4eeca006377072bb29c0b8a0f2432fc87c3302d6 Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 7 Apr 2025 14:02:54 -0400 Subject: [PATCH] Code cleanup --- src/callers/common.rs | 61 ++++++++++++++++++++++++------------------- src/main.rs | 10 +++++-- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/callers/common.rs b/src/callers/common.rs index afb5ffb..bf178b3 100644 --- a/src/callers/common.rs +++ b/src/callers/common.rs @@ -1,30 +1,37 @@ -use axum::{Extension, Json, http::StatusCode}; +pub mod response { + use serde::{Deserialize, Serialize}; -use serde::{Deserialize, Serialize}; - -#[derive(Deserialize, Serialize)] -pub struct TestResult { - message: String, -} - -// basic handler that responds with a static string -pub async fn root() -> &'static str { - "Hello, World!" -} - -pub async fn db_ping(Extension(pool): Extension) -> (StatusCode, Json) { - match sqlx::query("SELECT 1").execute(&pool).await { - Ok(_) => { - let tr = TestResult { - message: String::from("This works"), - }; - (StatusCode::OK, Json(tr)) - } - Err(e) => ( - StatusCode::BAD_REQUEST, - Json(TestResult { - message: e.to_string(), - }), - ), + #[derive(Deserialize, Serialize)] + pub struct TestResult { + pub message: String, + } +} + +pub mod endpoint { + use super::*; + use axum::{Extension, Json, http::StatusCode}; + + // basic handler that responds with a static string + pub async fn root() -> &'static str { + "Hello, World!" + } + + pub async fn db_ping( + Extension(pool): Extension, + ) -> (StatusCode, Json) { + match sqlx::query("SELECT 1").execute(&pool).await { + Ok(_) => { + let tr = response::TestResult { + message: String::from("This works"), + }; + (StatusCode::OK, Json(tr)) + } + Err(e) => ( + StatusCode::BAD_REQUEST, + Json(response::TestResult { + message: e.to_string(), + }), + ), + } } } diff --git a/src/main.rs b/src/main.rs index 2dfe642..48239d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,8 +25,14 @@ mod init { pub async fn routes() -> Router { // build our application with a route Router::new() - .route(callers::endpoints::DBTEST, get(callers::common::db_ping)) - .route(callers::endpoints::ROOT, get(callers::common::root)) + .route( + callers::endpoints::DBTEST, + get(callers::common::endpoint::db_ping), + ) + .route( + callers::endpoints::ROOT, + get(callers::common::endpoint::root), + ) .route( callers::endpoints::REGISTER, post(callers::register::register_user),