Files
YANPM/apps/api/src/routes.rs
2025-12-28 18:28:28 +08:00

64 lines
1.4 KiB
Rust

mod api;
mod view;
pub use self::api::ApiDoc;
use std::sync::Arc;
use axum::{Extension, Router};
use migration::sea_orm::DatabaseConnection;
use crate::{
configs::{ProgramSettings, server::CORSSettings},
middlewares,
services::{
AppService, ServiceState,
auth::authentication::{AuthenticationService, strategies::password::PasswordStrategy},
},
};
#[derive(Clone)]
pub struct AppState {
pub database_connection: Arc<DatabaseConnection>,
pub service: Arc<AppService>,
pub config: Arc<ProgramSettings>,
}
pub struct AuthStrategy {
pub password: ServiceState<PasswordStrategy>,
}
pub struct AuthState {
pub strategy: AuthStrategy,
pub authentication: ServiceState<dyn AuthenticationService>,
}
pub fn get_root_router(
state: impl Into<Arc<AppState>>,
cors_settings: Arc<CORSSettings>,
) -> Router {
let mut router = Router::new();
let state = state.into();
router = router
.nest("/api", api::get_api_router(state.clone()))
.merge(view::get_view_router());
router = middlewares::apply_root_middleware(router, state.clone(), cors_settings);
router = router.layer(Extension(state.clone()));
router
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ensure_state_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<AppState>();
}
}