diff --git a/apps/api/src/cmd/start_server.rs b/apps/api/src/cmd/start_server.rs index c33cadf..9e9400c 100644 --- a/apps/api/src/cmd/start_server.rs +++ b/apps/api/src/cmd/start_server.rs @@ -11,15 +11,8 @@ use crate::{ cmd::CliCommand, configs::{ProgramSettings, get_program_settings, logging::LoggingSettings}, log, - routes::{self, AppService, AppState}, - services::{ - auth::{ - authentication::{AuthenticationServiceImpl, strategies::password::PasswordStrategy}, - user::UserServiceImpl, - }, - server_state::ServerStateService, - settings::SettingsService, - }, + routes::{self, AppState}, + services::get_app_service, tasks, }; @@ -148,19 +141,7 @@ fn get_app_state( AppState { database_connection: db_connection.clone(), config: Arc::new(settings.clone()), - service: Arc::new(AppService { - server_state: Arc::new(ServerStateService::new(db_connection.clone())), - settings: Arc::new(SettingsService::new(db_connection.clone())), - auth_state: routes::AuthState { - strategy: routes::AuthStrategy { - password: Arc::new(PasswordStrategy::new(db_connection.clone())), - }, - authentication: Arc::new(AuthenticationServiceImpl::new( - settings.auth.jwt_secret.clone(), - )), - }, - user: Arc::new(UserServiceImpl::new(db_connection.clone())), - }), + service: Arc::new(get_app_service(db_connection, settings)), } } diff --git a/apps/api/src/routes.rs b/apps/api/src/routes.rs index cffc71b..d51591f 100644 --- a/apps/api/src/routes.rs +++ b/apps/api/src/routes.rs @@ -12,12 +12,8 @@ use crate::{ configs::{ProgramSettings, server::CORSSettings}, middlewares, services::{ - auth::{ - authentication::{AuthenticationService, strategies::password::PasswordStrategy}, - user::UserService, - }, - server_state::ServerStateStore, - settings::SettingsStore, + AppService, ServiceState, + auth::authentication::{AuthenticationService, strategies::password::PasswordStrategy}, }, }; @@ -28,8 +24,6 @@ pub struct AppState { pub config: Arc, } -pub type ServiceState = Arc; - pub struct AuthStrategy { pub password: ServiceState, } @@ -39,13 +33,6 @@ pub struct AuthState { pub authentication: ServiceState, } -pub struct AppService { - pub settings: ServiceState, - pub auth_state: AuthState, - pub user: ServiceState, - pub server_state: ServiceState, -} - pub fn get_root_router( state: impl Into>, cors_settings: Arc, diff --git a/apps/api/src/services.rs b/apps/api/src/services.rs index 6f68779..2b36eaf 100644 --- a/apps/api/src/services.rs +++ b/apps/api/src/services.rs @@ -2,3 +2,46 @@ pub mod agent_client; pub mod auth; pub mod server_state; pub mod settings; + +use std::sync::Arc; + +use crate::{ + configs::ProgramSettings, + routes::{self, AuthState}, + services::{ + auth::{ + authentication::{AuthenticationServiceImpl, strategies::password::PasswordStrategy}, + user::{UserService, UserServiceImpl}, + }, + server_state::{ServerStateService, ServerStateStore}, + settings::{SettingsService, SettingsStore}, + }, +}; + +pub type ServiceState = Arc; + +pub struct AppService { + pub settings: ServiceState, + pub auth_state: AuthState, + pub user: ServiceState, + pub server_state: ServiceState, +} + +pub fn get_app_service( + db_connection: &Arc, + settings: &ProgramSettings, +) -> AppService { + AppService { + server_state: Arc::new(ServerStateService::new(db_connection.clone())), + settings: Arc::new(SettingsService::new(db_connection.clone())), + auth_state: routes::AuthState { + strategy: routes::AuthStrategy { + password: Arc::new(PasswordStrategy::new(db_connection.clone())), + }, + authentication: Arc::new(AuthenticationServiceImpl::new( + settings.auth.jwt_secret.clone(), + )), + }, + user: Arc::new(UserServiceImpl::new(db_connection.clone())), + } +}