Skip to content

Commit

Permalink
Refactoring: migrate routes to separate modules (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBakys0 authored Oct 7, 2024
1 parent 2db3283 commit c271079
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 35 deletions.
44 changes: 9 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand Down Expand Up @@ -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));

Expand Down
17 changes: 17 additions & 0 deletions src/routes/auth.rs
Original file line number Diff line number Diff line change
@@ -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))
}
7 changes: 7 additions & 0 deletions src/routes/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -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))
}
23 changes: 23 additions & 0 deletions src/routes/matchmaking.rs
Original file line number Diff line number Diff line change
@@ -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))
}
11 changes: 11 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
9 changes: 9 additions & 0 deletions src/routes/score.rs
Original file line number Diff line number Diff line change
@@ -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))
}
16 changes: 16 additions & 0 deletions src/routes/user.rs
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit c271079

Please sign in to comment.