use applib::sys_major::rules_pl; use applib::sys_major::{Decoder, Encoder, LenValueMap}; use applib::{ ApiTokenAuthenticator, AuthService, Authenticator, DictionaryService, InMemoryTokenStore, JwtAuthenticator, MajorSystemService, SqliteDictRepository, SystemDecoder, SystemEncoder, TokenStore, }; use std::sync::Arc; #[derive(Clone)] pub struct AppDependencies { pub dictionary_service: Arc, pub auth_service: Arc, pub major_system_service: Arc, } impl AppDependencies { pub async fn new( database_url: &str, firebase_project_id: Option, firebase_emulator_url: Option, api_tokens: Vec, ) -> anyhow::Result { let repo_factory = Arc::new(SqliteDictRepository::new(database_url).await?); let dictionary_service = Arc::new(DictionaryService::new(repo_factory.clone())); let jwt_auth: Arc = if let Some(emulator_url) = firebase_emulator_url { Arc::new(JwtAuthenticator::with_emulator(emulator_url)) } else if let Some(project_id) = firebase_project_id { Arc::new(JwtAuthenticator::new(project_id)) } else { Arc::new(JwtAuthenticator::with_placeholder()) }; let api_token_authenticator = ApiTokenAuthenticator::with_tokens(api_tokens); let api_token_auth: Arc = Arc::new(api_token_authenticator); let token_store: Arc = Arc::new(InMemoryTokenStore::new()); let auth_service = Arc::new(AuthService::new(jwt_auth, api_token_auth, token_store)); let decoder: Arc = Arc::new(Decoder::new(rules_pl::get_rules())); let encoder: Arc = Arc::new(Encoder::new(LenValueMap::new())); let major_system_service = Arc::new(MajorSystemService::new(decoder).with_encoder(encoder)); Ok(Self { dictionary_service, auth_service, major_system_service, }) } }