Skip to content

Commit

Permalink
refactor: [torrust#1187] inline fn initialize_globals_and_tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 17, 2025
1 parent 4aea9db commit 36db088
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 30 deletions.
12 changes: 1 addition & 11 deletions src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,14 @@ pub fn check_seed() {
assert_eq!(seed, instance, "maybe using zeroed seed in production!?");
}

/// It initializes the application with the given configuration.
///
/// The configuration may be obtained from the environment (via config file or env vars).
#[must_use]
#[instrument(skip())]
pub fn initialize_globals_and_tracker(configuration: &Configuration) -> Arc<Tracker> {
initialize_global_services(configuration);
Arc::new(initialize_tracker(configuration))
}

/// It initializes the global services.
#[instrument(skip())]
pub fn initialize_global_services(configuration: &Configuration) {
initialize_static();
initialize_logging(configuration);
}

/// It initializes the stIoC Container.
/// It initializes the IoC Container.
#[instrument(skip())]
pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
let (stats_event_sender, stats_repository) = statistics::setup::factory(configuration.core.tracker_usage_statistics);
Expand Down
8 changes: 6 additions & 2 deletions src/bootstrap/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mod tests {

use torrust_tracker_test_helpers::configuration::ephemeral_public;

use crate::bootstrap::app::initialize_globals_and_tracker;
use crate::bootstrap::app::{initialize_global_services, initialize_tracker};
use crate::bootstrap::jobs::http_tracker::start_job;
use crate::core::services::statistics;
use crate::servers::http::Version;
Expand All @@ -97,9 +97,13 @@ mod tests {
let cfg = Arc::new(ephemeral_public());
let http_tracker = cfg.http_trackers.clone().expect("missing HTTP tracker configuration");
let config = &http_tracker[0];

let (stats_event_sender, _stats_repository) = statistics::setup::factory(cfg.core.tracker_usage_statistics);
let stats_event_sender = Arc::new(stats_event_sender);
let tracker = initialize_globals_and_tracker(&cfg);

initialize_global_services(&cfg);
let tracker = Arc::new(initialize_tracker(&cfg));

let version = Version::V1;

start_job(config, tracker, stats_event_sender, Registar::default().give_form(), version)
Expand Down
5 changes: 3 additions & 2 deletions src/bootstrap/jobs/tracker_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ mod tests {
use tokio::sync::RwLock;
use torrust_tracker_test_helpers::configuration::ephemeral_public;

use crate::bootstrap::app::initialize_globals_and_tracker;
use crate::bootstrap::app::{initialize_global_services, initialize_tracker};
use crate::bootstrap::jobs::tracker_apis::start_job;
use crate::core::services::statistics;
use crate::servers::apis::Version;
Expand All @@ -158,7 +158,8 @@ mod tests {
let stats_event_sender = Arc::new(stats_event_sender);
let stats_repository = Arc::new(stats_repository);

let tracker = initialize_globals_and_tracker(&cfg);
initialize_global_services(&cfg);
let tracker = Arc::new(initialize_tracker(&cfg));

let version = Version::V1;

Expand Down
6 changes: 4 additions & 2 deletions src/servers/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ mod tests {
use tokio::sync::RwLock;
use torrust_tracker_test_helpers::configuration::ephemeral_public;

use crate::bootstrap::app::initialize_globals_and_tracker;
use crate::bootstrap::app::{initialize_global_services, initialize_tracker};
use crate::bootstrap::jobs::make_rust_tls;
use crate::core::services::statistics;
use crate::servers::apis::server::{ApiServer, Launcher};
Expand All @@ -350,7 +350,9 @@ mod tests {
let (stats_event_sender, stats_repository) = statistics::setup::factory(cfg.core.tracker_usage_statistics);
let stats_event_sender = Arc::new(stats_event_sender);
let stats_repository = Arc::new(stats_repository);
let tracker = initialize_globals_and_tracker(&cfg);

initialize_global_services(&cfg);
let tracker = Arc::new(initialize_tracker(&cfg));

let bind_to = config.bind_address;

Expand Down
6 changes: 4 additions & 2 deletions src/servers/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ mod tests {

use torrust_tracker_test_helpers::configuration::ephemeral_public;

use crate::bootstrap::app::initialize_globals_and_tracker;
use crate::bootstrap::app::{initialize_global_services, initialize_tracker};
use crate::bootstrap::jobs::make_rust_tls;
use crate::core::services::statistics;
use crate::servers::http::server::{HttpServer, Launcher};
Expand All @@ -254,7 +254,9 @@ mod tests {

let (stats_event_sender, _stats_repository) = statistics::setup::factory(cfg.core.tracker_usage_statistics);
let stats_event_sender = Arc::new(stats_event_sender);
let tracker = initialize_globals_and_tracker(&cfg);

initialize_global_services(&cfg);
let tracker = Arc::new(initialize_tracker(&cfg));

let http_trackers = cfg.http_trackers.clone().expect("missing HTTP trackers configuration");
let config = &http_trackers[0];
Expand Down
10 changes: 7 additions & 3 deletions src/servers/udp/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod tests {

use super::spawner::Spawner;
use super::Server;
use crate::bootstrap::app::initialize_globals_and_tracker;
use crate::bootstrap::app::{initialize_global_services, initialize_tracker};
use crate::core::services::statistics;
use crate::servers::registar::Registar;
use crate::servers::udp::server::banning::BanService;
Expand All @@ -76,7 +76,9 @@ mod tests {
let (stats_event_sender, _stats_repository) = statistics::setup::factory(cfg.core.tracker_usage_statistics);
let stats_event_sender = Arc::new(stats_event_sender);
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
let tracker = initialize_globals_and_tracker(&cfg);

initialize_global_services(&cfg);
let tracker = Arc::new(initialize_tracker(&cfg));

let udp_trackers = cfg.udp_trackers.clone().expect("missing UDP trackers configuration");
let config = &udp_trackers[0];
Expand Down Expand Up @@ -110,7 +112,9 @@ mod tests {
let (stats_event_sender, _stats_repository) = statistics::setup::factory(cfg.core.tracker_usage_statistics);
let stats_event_sender = Arc::new(stats_event_sender);
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
let tracker = initialize_globals_and_tracker(&cfg);

initialize_global_services(&cfg);
let tracker = Arc::new(initialize_tracker(&cfg));

let config = &cfg.udp_trackers.as_ref().unwrap().first().unwrap();
let bind_to = config.bind_address;
Expand Down
7 changes: 4 additions & 3 deletions tests/servers/api/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use futures::executor::block_on;
use tokio::sync::RwLock;
use torrust_tracker_api_client::connection_info::{ConnectionInfo, Origin};
use torrust_tracker_configuration::{Configuration, HttpApi};
use torrust_tracker_lib::bootstrap::app::initialize_globals_and_tracker;
use torrust_tracker_lib::bootstrap::app::{initialize_global_services, initialize_tracker};
use torrust_tracker_lib::bootstrap::jobs::make_rust_tls;
use torrust_tracker_lib::core::services::statistics;
use torrust_tracker_lib::core::statistics::event::sender::Sender;
Expand Down Expand Up @@ -50,9 +50,10 @@ impl Environment<Stopped> {
let stats_repository = Arc::new(stats_repository);
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));

let tracker = initialize_globals_and_tracker(configuration);
initialize_global_services(configuration);
let tracker = Arc::new(initialize_tracker(configuration));

// todo: instantiate outside of `initialize_globals_and_tracker`
// todo: instantiate outside of `initialize_tracker_dependencies`
let whitelist_manager = tracker.whitelist_manager.clone();

let config = Arc::new(configuration.http_api.clone().expect("missing API configuration"));
Expand Down
7 changes: 4 additions & 3 deletions tests/servers/http/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use bittorrent_primitives::info_hash::InfoHash;
use futures::executor::block_on;
use torrust_tracker_configuration::{Configuration, HttpTracker};
use torrust_tracker_lib::bootstrap::app::initialize_globals_and_tracker;
use torrust_tracker_lib::bootstrap::app::{initialize_global_services, initialize_tracker};
use torrust_tracker_lib::bootstrap::jobs::make_rust_tls;
use torrust_tracker_lib::core::services::statistics;
use torrust_tracker_lib::core::statistics::event::sender::Sender;
Expand Down Expand Up @@ -38,9 +38,10 @@ impl Environment<Stopped> {
let stats_event_sender = Arc::new(stats_event_sender);
let stats_repository = Arc::new(stats_repository);

let tracker = initialize_globals_and_tracker(configuration);
initialize_global_services(configuration);
let tracker = Arc::new(initialize_tracker(configuration));

// todo: instantiate outside of `initialize_globals_and_tracker`
// todo: instantiate outside of `initialize_tracker_dependencies`
let whitelist_manager = tracker.whitelist_manager.clone();

let http_tracker = configuration
Expand Down
5 changes: 3 additions & 2 deletions tests/servers/udp/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use bittorrent_primitives::info_hash::InfoHash;
use tokio::sync::RwLock;
use torrust_tracker_configuration::{Configuration, UdpTracker, DEFAULT_TIMEOUT};
use torrust_tracker_lib::bootstrap::app::initialize_globals_and_tracker;
use torrust_tracker_lib::bootstrap::app::{initialize_global_services, initialize_tracker};
use torrust_tracker_lib::core::services::statistics;
use torrust_tracker_lib::core::statistics::event::sender::Sender;
use torrust_tracker_lib::core::statistics::repository::Repository;
Expand Down Expand Up @@ -49,7 +49,8 @@ impl Environment<Stopped> {
let stats_repository = Arc::new(stats_repository);
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));

let tracker = initialize_globals_and_tracker(configuration);
initialize_global_services(configuration);
let tracker = Arc::new(initialize_tracker(configuration));

let udp_tracker = configuration.udp_trackers.clone().expect("missing UDP tracker configuration");

Expand Down

0 comments on commit 36db088

Please sign in to comment.