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

77 lines
1.8 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::{
middlewares,
services::{
auth::{
authentication::{AuthenticationService, strategies::password::PasswordStrategy},
user::UserService,
},
settings::SettingsStore,
},
};
#[derive(Clone)]
pub struct AppState {
// TODO: remove dead_code allowances when fields are used
#[allow(dead_code)]
pub database_connection: Arc<DatabaseConnection>,
// TODO: remove dead_code allowances when fields are used
#[allow(dead_code)]
pub service: Arc<AppService>,
}
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 user: ServiceState<dyn UserService>,
}
pub struct AppService {
// #[allow(dead_code)] // TODO: remove when used
pub settings: ServiceState<dyn SettingsStore>,
pub auth_state: AuthState,
// #[allow(dead_code)] // TODO: remove when used
pub user: ServiceState<dyn UserService>,
}
pub fn get_root_router(state: impl Into<Arc<AppState>>) -> 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());
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>();
}
}