refactor: move route definitions to a separate module

This commit is contained in:
2025-11-02 09:37:20 +01:00
parent 455e144ffb
commit 7ea91da20a
2 changed files with 28 additions and 25 deletions

View File

@@ -1,21 +1,12 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use axum::{
Router,
routing::{get, post},
};
use crate::handlers::{
album_handlers, auth_handlers,
media_handlers::{self},
};
pub mod config; pub mod config;
pub mod error; pub mod error;
pub mod factory; pub mod factory;
pub mod handlers; pub mod handlers;
pub mod middleware; pub mod middleware;
pub mod repositories; pub mod repositories;
pub mod routes;
pub mod security; pub mod security;
pub mod services; pub mod services;
pub mod state; pub mod state;
@@ -27,21 +18,7 @@ async fn main() -> anyhow::Result<()> {
let app_state = factory::build_app_state(config).await?; let app_state = factory::build_app_state(config).await?;
let auth_routes = Router::new() let app = routes::api_routes().with_state(app_state);
.route("/register", post(auth_handlers::register))
.route("/login", post(auth_handlers::login));
let user_routes = Router::new().route("/me", get(auth_handlers::get_me));
let media_routes = media_handlers::media_routes();
let album_routes = album_handlers::album_routes();
let app = Router::new()
.route("/api/v1/health", get(|| async { "OK" }))
.nest("/api/v1/auth", auth_routes)
.nest("/api/v1/users", user_routes)
.nest("/api/v1/media", media_routes)
.nest("/api/v1/albums", album_routes)
.with_state(app_state);
println!("Starting server at http://{}", addr); println!("Starting server at http://{}", addr);

View File

@@ -0,0 +1,26 @@
use axum::{
Router,
routing::{get, post},
};
use crate::{
handlers::{album_handlers, auth_handlers, media_handlers},
state::AppState,
};
pub fn api_routes() -> Router<AppState> {
let auth_routes = Router::new()
.route("/register", post(auth_handlers::register))
.route("/login", post(auth_handlers::login));
let user_routes = Router::new().route("/me", get(auth_handlers::get_me));
let media_routes = media_handlers::media_routes();
let album_routes = album_handlers::album_routes();
Router::new()
.route("/api/v1/health", get(|| async { "OK" }))
.nest("/api/v1/auth", auth_routes)
.nest("/api/v1/users", user_routes)
.nest("/api/v1/media", media_routes)
.nest("/api/v1/albums", album_routes)
}