From c271079462283bfaae322a8d60a63bb8681e342a Mon Sep 17 00:00:00 2001 From: JonasBakys0 Date: Mon, 7 Oct 2024 07:25:30 +0300 Subject: [PATCH] Refactoring: migrate routes to separate modules (#14) --- src/lib.rs | 44 ++++++++------------------------------- src/routes/auth.rs | 17 +++++++++++++++ src/routes/diagnostics.rs | 7 +++++++ src/routes/matchmaking.rs | 23 ++++++++++++++++++++ src/routes/mod.rs | 11 ++++++++++ src/routes/score.rs | 9 ++++++++ src/routes/user.rs | 16 ++++++++++++++ 7 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 src/routes/auth.rs create mode 100644 src/routes/diagnostics.rs create mode 100644 src/routes/matchmaking.rs create mode 100644 src/routes/mod.rs create mode 100644 src/routes/score.rs create mode 100644 src/routes/user.rs diff --git a/src/lib.rs b/src/lib.rs index 77e9ace..bb9fa41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,17 @@ use axum::{ http::{self, Method}, - routing::{get, post, put}, Extension, Router, }; -use handlers::{ - auth_handlers::{ - login_handler, new_password_handler, otp_handler, send_pass_reset_handler, signup_handler, - }, - cp_handler::code_handler, - crud_handlers::{ - add_friend_handler, change_flag_handler, create_matched_handler, get_accepted_boys_handler, - get_all_users_handler, get_boys_handler, get_girl_request_handler, get_girls_handler, - get_matched_handler, get_user_by_id_handler, get_user_handler, reject_handler, - update_contest_score_handler, update_score_handler, update_user_character_handler, - }, - tests_handlers::health_check, -}; + +use routes::*; use sea_orm::Database; use tower_http::cors::{AllowOrigin, CorsLayer}; + mod bcrypts; mod configs; mod handlers; mod model; +mod routes; mod utils; pub async fn run() -> Router<()> { @@ -58,27 +48,11 @@ pub async fn run() -> Router<()> { .await .expect("could not connect"); let app: Router<()> = Router::new() - .route("/health_check", get(health_check)) - .route("/sendpassreset", get(send_pass_reset_handler)) - .route("/newpassword", get(new_password_handler)) - .route("/otp", get(otp_handler)) - .route("/login", post(login_handler)) - .route("/signup", post(signup_handler)) - .route("/getuser", post(get_user_handler)) - .route("/getboys", get(get_boys_handler)) - .route("/getgirls", get(get_girls_handler)) - .route("/updatescore", put(update_score_handler)) - .route("/getallusers", get(get_all_users_handler)) - .route("/addfriend", post(add_friend_handler)) - .route("/updatecharacter", put(update_user_character_handler)) - .route("/getgirlrequests", post(get_girl_request_handler)) - .route("/getacceptedboys", post(get_accepted_boys_handler)) - .route("/changeflag", post(change_flag_handler)) - .route("/creatematch", post(create_matched_handler)) - .route("/getmatched", post(get_matched_handler)) - .route("/updatecontestscore", put(update_contest_score_handler)) - .route("/getuserbyid", post(get_user_by_id_handler)) - .route("/reject", post(reject_handler)) + .nest("/auth", auth_routes()) + .nest("/user", user_routes()) + .nest("/matchmaking", matchmaking_routes()) + .nest("/score", score_routes()) + .nest("/diagnostics", diagnostics_routes()) .layer(cors) .layer(Extension(db)); diff --git a/src/routes/auth.rs b/src/routes/auth.rs new file mode 100644 index 0000000..7574a8b --- /dev/null +++ b/src/routes/auth.rs @@ -0,0 +1,17 @@ +use axum::{ + routing::{get, post}, + Router, +}; + +use crate::handlers::auth_handlers::{ + login_handler, new_password_handler, otp_handler, send_pass_reset_handler, signup_handler, +}; + +pub fn auth_routes() -> Router { + Router::new() + .route("/login", post(login_handler)) + .route("/signup", post(signup_handler)) + .route("/sendpassreset", get(send_pass_reset_handler)) + .route("/newpassword", get(new_password_handler)) + .route("/otp", get(otp_handler)) +} diff --git a/src/routes/diagnostics.rs b/src/routes/diagnostics.rs new file mode 100644 index 0000000..43fb3d3 --- /dev/null +++ b/src/routes/diagnostics.rs @@ -0,0 +1,7 @@ +use axum::{routing::get, Router}; + +use crate::handlers::tests_handlers::health_check; + +pub fn diagnostics_routes() -> Router { + Router::new().route("/health_check", get(health_check)) +} diff --git a/src/routes/matchmaking.rs b/src/routes/matchmaking.rs new file mode 100644 index 0000000..4d6b01c --- /dev/null +++ b/src/routes/matchmaking.rs @@ -0,0 +1,23 @@ +use axum::{ + routing::{get, post}, + Router, +}; + +use crate::handlers::crud_handlers::{ + add_friend_handler, change_flag_handler, create_matched_handler, get_accepted_boys_handler, + get_boys_handler, get_girl_request_handler, get_girls_handler, get_matched_handler, + reject_handler, +}; + +pub fn matchmaking_routes() -> Router { + Router::new() + .route("/getboys", get(get_boys_handler)) + .route("/getacceptedboys", post(get_accepted_boys_handler)) + .route("/getgirls", get(get_girls_handler)) + .route("/getgirlrequests", post(get_girl_request_handler)) + .route("/addfriend", post(add_friend_handler)) + .route("/changeflag", post(change_flag_handler)) + .route("/creatematch", post(create_matched_handler)) + .route("/getmatched", post(get_matched_handler)) + .route("/reject", post(reject_handler)) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..f538abc --- /dev/null +++ b/src/routes/mod.rs @@ -0,0 +1,11 @@ +mod auth; +mod diagnostics; +mod matchmaking; +mod score; +mod user; + +pub use auth::*; +pub use diagnostics::*; +pub use matchmaking::*; +pub use score::*; +pub use user::*; diff --git a/src/routes/score.rs b/src/routes/score.rs new file mode 100644 index 0000000..cea496d --- /dev/null +++ b/src/routes/score.rs @@ -0,0 +1,9 @@ +use axum::{routing::put, Router}; + +use crate::handlers::crud_handlers::{update_contest_score_handler, update_score_handler}; + +pub fn score_routes() -> Router { + Router::new() + .route("/updatescore", put(update_score_handler)) + .route("/updatecontestscore", put(update_contest_score_handler)) +} diff --git a/src/routes/user.rs b/src/routes/user.rs new file mode 100644 index 0000000..1ba6753 --- /dev/null +++ b/src/routes/user.rs @@ -0,0 +1,16 @@ +use axum::{ + routing::{get, post, put}, + Router, +}; + +use crate::handlers::crud_handlers::{ + get_all_users_handler, get_user_by_id_handler, get_user_handler, update_user_character_handler, +}; + +pub fn user_routes() -> Router { + Router::new() + .route("/getuser", post(get_user_handler)) + .route("/getallusers", get(get_all_users_handler)) + .route("/updatecharacter", put(update_user_character_handler)) + .route("/getuserbyid", post(get_user_by_id_handler)) +}