You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

52 lines
2.0 KiB

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<DictionaryService>,
pub auth_service: Arc<AuthService>,
pub major_system_service: Arc<MajorSystemService>,
}
impl AppDependencies {
pub async fn new(
database_url: &str,
firebase_project_id: Option<String>,
firebase_emulator_url: Option<String>,
api_tokens: Vec<String>,
) -> anyhow::Result<Self> {
let repo_factory = Arc::new(SqliteDictRepository::new(database_url).await?);
let dictionary_service = Arc::new(DictionaryService::new(repo_factory.clone()));
let jwt_auth: Arc<dyn Authenticator> = 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<dyn Authenticator> = Arc::new(api_token_authenticator);
let token_store: Arc<dyn TokenStore> = Arc::new(InMemoryTokenStore::new());
let auth_service = Arc::new(AuthService::new(jwt_auth, api_token_auth, token_store));
let decoder: Arc<dyn SystemDecoder> = Arc::new(Decoder::new(rules_pl::get_rules()));
let encoder: Arc<dyn SystemEncoder> = 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,
})
}
}