Files
YANPM/apps/api/src/routes.rs

77 lines
1.7 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::{
auth::{
authentication::{AuthenticationService, strategies::password::PasswordStrategy},
user::UserService,
},
server_state::ServerStateStore,
settings::SettingsStore,
},
};
#[derive(Clone)]
pub struct AppState {
pub database_connection: Arc<DatabaseConnection>,
pub service: Arc<AppService>,
pub config: Arc<ProgramSettings>,
}
pub type ServiceState<T> = Arc<T>;
pub struct AuthStrategy {
pub password: ServiceState<PasswordStrategy>,
}
pub struct AuthState {
pub strategy: AuthStrategy,
pub authentication: ServiceState<dyn AuthenticationService>,
}
pub struct AppService {
pub settings: ServiceState<dyn SettingsStore>,
pub auth_state: AuthState,
pub user: ServiceState<dyn UserService>,
pub server_state: ServiceState<dyn ServerStateStore>,
}
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>();
}
}