From 948cc8c2bcacc83fed4653145fcd769979c68a00 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 29 Jan 2025 12:46:10 +0000 Subject: [PATCH 1/5] refactor: [#1221] move core statistics mod to statistics context --- src/bootstrap/app.rs | 3 ++- src/core/services/mod.rs | 7 ------- src/core/statistics/mod.rs | 2 ++ .../statistics/mod.rs => statistics/services.rs} | 8 +++----- src/core/{services => }/statistics/setup.rs | 0 src/servers/apis/v1/context/stats/handlers.rs | 2 +- src/servers/apis/v1/context/stats/resources.rs | 4 ++-- src/servers/apis/v1/context/stats/responses.rs | 2 +- src/servers/http/v1/handlers/announce.rs | 3 ++- src/servers/http/v1/handlers/scrape.rs | 2 +- src/servers/http/v1/services/announce.rs | 3 ++- src/servers/http/v1/services/scrape.rs | 4 ++-- src/servers/udp/handlers.rs | 16 ++++++++-------- 13 files changed, 26 insertions(+), 30 deletions(-) rename src/core/{services/statistics/mod.rs => statistics/services.rs} (95%) rename src/core/{services => }/statistics/setup.rs (100%) diff --git a/src/bootstrap/app.rs b/src/bootstrap/app.rs index 71684a7e3..7661a36ec 100644 --- a/src/bootstrap/app.rs +++ b/src/bootstrap/app.rs @@ -28,7 +28,8 @@ use crate::core::authentication::key::repository::in_memory::InMemoryKeyReposito use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository; use crate::core::authentication::service; use crate::core::scrape_handler::ScrapeHandler; -use crate::core::services::{initialize_database, initialize_whitelist_manager, statistics}; +use crate::core::services::{initialize_database, initialize_whitelist_manager}; +use crate::core::statistics; use crate::core::torrent::manager::TorrentsManager; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; diff --git a/src/core/services/mod.rs b/src/core/services/mod.rs index f2ee79993..30a05a992 100644 --- a/src/core/services/mod.rs +++ b/src/core/services/mod.rs @@ -1,10 +1,3 @@ -//! Tracker domain services. Core and statistics services. -//! -//! There are two types of service: -//! -//! - [Core tracker services](crate::core::services::torrent): related to the tracker main functionalities like getting info about torrents. -//! - [Services for statistics](crate::core::services::statistics): related to tracker metrics. Aggregate data about the tracker server. -pub mod statistics; pub mod torrent; use std::sync::Arc; diff --git a/src/core/statistics/mod.rs b/src/core/statistics/mod.rs index 49a82bea9..2ffbc0c8f 100644 --- a/src/core/statistics/mod.rs +++ b/src/core/statistics/mod.rs @@ -28,3 +28,5 @@ pub mod event; pub mod keeper; pub mod metrics; pub mod repository; +pub mod services; +pub mod setup; diff --git a/src/core/services/statistics/mod.rs b/src/core/statistics/services.rs similarity index 95% rename from src/core/services/statistics/mod.rs rename to src/core/statistics/services.rs index 79bc5f268..337731aea 100644 --- a/src/core/services/statistics/mod.rs +++ b/src/core/statistics/services.rs @@ -2,7 +2,7 @@ //! //! It includes: //! -//! - A [`factory`](crate::core::services::statistics::setup::factory) function to build the structs needed to collect the tracker metrics. +//! - A [`factory`](crate::core::statistics::setup::factory) function to build the structs needed to collect the tracker metrics. //! - A [`get_metrics`] service to get the tracker [`metrics`](crate::core::statistics::metrics::Metrics). //! //! Tracker metrics are collected using a Publisher-Subscribe pattern. @@ -36,8 +36,6 @@ //! // ... //! } //! ``` -pub mod setup; - use std::sync::Arc; use tokio::sync::RwLock; @@ -117,9 +115,9 @@ mod tests { use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_test_helpers::configuration; - use crate::core::services::statistics::{self, get_metrics, TrackerMetrics}; + use crate::core::statistics::services::{get_metrics, TrackerMetrics}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; - use crate::core::{self}; + use crate::core::{self, statistics}; use crate::servers::udp::server::banning::BanService; use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP; diff --git a/src/core/services/statistics/setup.rs b/src/core/statistics/setup.rs similarity index 100% rename from src/core/services/statistics/setup.rs rename to src/core/statistics/setup.rs diff --git a/src/servers/apis/v1/context/stats/handlers.rs b/src/servers/apis/v1/context/stats/handlers.rs index da87696fc..b8e7abd87 100644 --- a/src/servers/apis/v1/context/stats/handlers.rs +++ b/src/servers/apis/v1/context/stats/handlers.rs @@ -9,8 +9,8 @@ use serde::Deserialize; use tokio::sync::RwLock; use super::responses::{metrics_response, stats_response}; -use crate::core::services::statistics::get_metrics; use crate::core::statistics::repository::Repository; +use crate::core::statistics::services::get_metrics; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::servers::udp::server::banning::BanService; diff --git a/src/servers/apis/v1/context/stats/resources.rs b/src/servers/apis/v1/context/stats/resources.rs index c6a526a7d..97ece22fc 100644 --- a/src/servers/apis/v1/context/stats/resources.rs +++ b/src/servers/apis/v1/context/stats/resources.rs @@ -2,7 +2,7 @@ //! API context. use serde::{Deserialize, Serialize}; -use crate::core::services::statistics::TrackerMetrics; +use crate::core::statistics::services::TrackerMetrics; /// It contains all the statistics generated by the tracker. #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -121,8 +121,8 @@ mod tests { use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use super::Stats; - use crate::core::services::statistics::TrackerMetrics; use crate::core::statistics::metrics::Metrics; + use crate::core::statistics::services::TrackerMetrics; #[test] fn stats_resource_should_be_converted_from_tracker_metrics() { diff --git a/src/servers/apis/v1/context/stats/responses.rs b/src/servers/apis/v1/context/stats/responses.rs index a67b5328a..6fda43f8c 100644 --- a/src/servers/apis/v1/context/stats/responses.rs +++ b/src/servers/apis/v1/context/stats/responses.rs @@ -3,7 +3,7 @@ use axum::response::{IntoResponse, Json, Response}; use super::resources::Stats; -use crate::core::services::statistics::TrackerMetrics; +use crate::core::statistics::services::TrackerMetrics; /// `200` response that contains the [`Stats`] resource as json. #[must_use] diff --git a/src/servers/http/v1/handlers/announce.rs b/src/servers/http/v1/handlers/announce.rs index d6c850327..3de17df58 100644 --- a/src/servers/http/v1/handlers/announce.rs +++ b/src/servers/http/v1/handlers/announce.rs @@ -257,7 +257,8 @@ mod tests { use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::service::AuthenticationService; use crate::core::core_tests::sample_info_hash; - use crate::core::services::{initialize_database, statistics}; + use crate::core::services::initialize_database; + use crate::core::statistics; use crate::core::statistics::event::sender::Sender; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; diff --git a/src/servers/http/v1/handlers/scrape.rs b/src/servers/http/v1/handlers/scrape.rs index a197263e8..35c5b1409 100644 --- a/src/servers/http/v1/handlers/scrape.rs +++ b/src/servers/http/v1/handlers/scrape.rs @@ -177,7 +177,7 @@ mod tests { use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::service::AuthenticationService; use crate::core::scrape_handler::ScrapeHandler; - use crate::core::services::statistics; + use crate::core::statistics; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::whitelist::authorization::WhitelistAuthorization; use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; diff --git a/src/servers/http/v1/services/announce.rs b/src/servers/http/v1/services/announce.rs index e96face6a..6314c0a98 100644 --- a/src/servers/http/v1/services/announce.rs +++ b/src/servers/http/v1/services/announce.rs @@ -65,7 +65,8 @@ mod tests { use torrust_tracker_test_helpers::configuration; use crate::core::announce_handler::AnnounceHandler; - use crate::core::services::{initialize_database, statistics}; + use crate::core::services::initialize_database; + use crate::core::statistics; use crate::core::statistics::event::sender::Sender; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; diff --git a/src/servers/http/v1/services/scrape.rs b/src/servers/http/v1/services/scrape.rs index 7e65b9442..16821e724 100644 --- a/src/servers/http/v1/services/scrape.rs +++ b/src/servers/http/v1/services/scrape.rs @@ -153,7 +153,7 @@ mod tests { #[tokio::test] async fn it_should_return_the_scrape_data_for_a_torrent() { - let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); + let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); let (announce_handler, scrape_handler) = initialize_announce_and_scrape_handlers_for_public_tracker(); @@ -236,7 +236,7 @@ mod tests { #[tokio::test] async fn it_should_always_return_the_zeroed_scrape_data_for_a_torrent() { - let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); + let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); let (announce_handler, _scrape_handler) = initialize_announce_and_scrape_handlers_for_public_tracker(); diff --git a/src/servers/udp/handlers.rs b/src/servers/udp/handlers.rs index 992f27a44..f531718db 100644 --- a/src/servers/udp/handlers.rs +++ b/src/servers/udp/handlers.rs @@ -476,13 +476,13 @@ mod tests { use super::gen_remote_fingerprint; use crate::core::announce_handler::AnnounceHandler; use crate::core::scrape_handler::ScrapeHandler; - use crate::core::services::{initialize_database, statistics}; + use crate::core::services::initialize_database; use crate::core::statistics::event::sender::Sender; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; - use crate::core::whitelist; use crate::core::whitelist::authorization::WhitelistAuthorization; use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; + use crate::core::{statistics, whitelist}; use crate::CurrentClock; struct CoreTrackerServices { @@ -656,7 +656,7 @@ mod tests { #[tokio::test] async fn a_connect_response_should_contain_the_same_transaction_id_as_the_connect_request() { - let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); + let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); let request = ConnectRequest { @@ -676,7 +676,7 @@ mod tests { #[tokio::test] async fn a_connect_response_should_contain_a_new_connection_id() { - let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); + let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); let request = ConnectRequest { @@ -696,7 +696,7 @@ mod tests { #[tokio::test] async fn a_connect_response_should_contain_a_new_connection_id_ipv6() { - let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); + let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); let request = ConnectRequest { @@ -1001,7 +1001,7 @@ mod tests { announce_handler: Arc, whitelist_authorization: Arc, ) -> Response { - let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); + let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); let remote_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080); @@ -1306,7 +1306,7 @@ mod tests { announce_handler: Arc, whitelist_authorization: Arc, ) -> Response { - let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); + let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); let client_ip_v4 = Ipv4Addr::new(126, 0, 0, 1); @@ -1494,7 +1494,7 @@ mod tests { use super::{gen_remote_fingerprint, TorrentPeerBuilder}; use crate::core::scrape_handler::ScrapeHandler; - use crate::core::services::statistics; + use crate::core::statistics; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::servers::udp::connection_cookie::make; use crate::servers::udp::handlers::handle_scrape; From d830c78cf865398ae27efbcbd519feb9040a5640 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 29 Jan 2025 13:08:16 +0000 Subject: [PATCH 2/5] refactor: [#1221] move core torrent mod to torrent context --- src/core/services/mod.rs | 2 -- src/core/torrent/mod.rs | 1 + src/core/{services/torrent.rs => torrent/services.rs} | 8 ++++---- src/servers/apis/v1/context/torrent/handlers.rs | 2 +- src/servers/apis/v1/context/torrent/resources/torrent.rs | 4 ++-- src/servers/apis/v1/context/torrent/responses.rs | 2 +- 6 files changed, 9 insertions(+), 10 deletions(-) rename src/core/{services/torrent.rs => torrent/services.rs} (97%) diff --git a/src/core/services/mod.rs b/src/core/services/mod.rs index 30a05a992..1050e41f8 100644 --- a/src/core/services/mod.rs +++ b/src/core/services/mod.rs @@ -1,5 +1,3 @@ -pub mod torrent; - use std::sync::Arc; use databases::driver::Driver; diff --git a/src/core/torrent/mod.rs b/src/core/torrent/mod.rs index 95a5ff1eb..2aa19130e 100644 --- a/src/core/torrent/mod.rs +++ b/src/core/torrent/mod.rs @@ -27,6 +27,7 @@ //! pub mod manager; pub mod repository; +pub mod services; use torrust_tracker_torrent_repository::TorrentsSkipMapMutexStd; diff --git a/src/core/services/torrent.rs b/src/core/torrent/services.rs similarity index 97% rename from src/core/services/torrent.rs rename to src/core/torrent/services.rs index dac93ce16..5a4810412 100644 --- a/src/core/services/torrent.rs +++ b/src/core/torrent/services.rs @@ -135,9 +135,9 @@ mod tests { use bittorrent_primitives::info_hash::InfoHash; - use crate::core::services::torrent::tests::sample_peer; - use crate::core::services::torrent::{get_torrent_info, Info}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::services::tests::sample_peer; + use crate::core::torrent::services::{get_torrent_info, Info}; #[tokio::test] async fn should_return_none_if_the_tracker_does_not_have_the_torrent() { @@ -184,9 +184,9 @@ mod tests { use bittorrent_primitives::info_hash::InfoHash; - use crate::core::services::torrent::tests::sample_peer; - use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::services::tests::sample_peer; + use crate::core::torrent::services::{get_torrents_page, BasicInfo, Pagination}; #[tokio::test] async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() { diff --git a/src/servers/apis/v1/context/torrent/handlers.rs b/src/servers/apis/v1/context/torrent/handlers.rs index 8fe20ab80..0ec90441d 100644 --- a/src/servers/apis/v1/context/torrent/handlers.rs +++ b/src/servers/apis/v1/context/torrent/handlers.rs @@ -13,8 +13,8 @@ use thiserror::Error; use torrust_tracker_primitives::pagination::Pagination; use super::responses::{torrent_info_response, torrent_list_response, torrent_not_known_response}; -use crate::core::services::torrent::{get_torrent_info, get_torrents, get_torrents_page}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; +use crate::core::torrent::services::{get_torrent_info, get_torrents, get_torrents_page}; use crate::servers::apis::v1::responses::invalid_info_hash_param_response; use crate::servers::apis::InfoHashParam; diff --git a/src/servers/apis/v1/context/torrent/resources/torrent.rs b/src/servers/apis/v1/context/torrent/resources/torrent.rs index 237470d88..c90a2a05f 100644 --- a/src/servers/apis/v1/context/torrent/resources/torrent.rs +++ b/src/servers/apis/v1/context/torrent/resources/torrent.rs @@ -6,7 +6,7 @@ //! the JSON response. use serde::{Deserialize, Serialize}; -use crate::core::services::torrent::{BasicInfo, Info}; +use crate::core::torrent::services::{BasicInfo, Info}; /// `Torrent` API resource. #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] @@ -102,7 +102,7 @@ mod tests { use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; use super::Torrent; - use crate::core::services::torrent::{BasicInfo, Info}; + use crate::core::torrent::services::{BasicInfo, Info}; use crate::servers::apis::v1::context::torrent::resources::peer::Peer; use crate::servers::apis::v1::context::torrent::resources::torrent::ListItem; diff --git a/src/servers/apis/v1/context/torrent/responses.rs b/src/servers/apis/v1/context/torrent/responses.rs index 5daceaf94..5174c9abe 100644 --- a/src/servers/apis/v1/context/torrent/responses.rs +++ b/src/servers/apis/v1/context/torrent/responses.rs @@ -4,7 +4,7 @@ use axum::response::{IntoResponse, Json, Response}; use serde_json::json; use super::resources::torrent::{ListItem, Torrent}; -use crate::core::services::torrent::{BasicInfo, Info}; +use crate::core::torrent::services::{BasicInfo, Info}; /// `200` response that contains an array of /// [`ListItem`] From 716e7b2fe050b03d524a47bc9fed41646a250ff1 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 29 Jan 2025 13:13:58 +0000 Subject: [PATCH 3/5] refactor: [#1221] move DB setup to databases context --- src/bootstrap/app.rs | 3 ++- src/core/announce_handler.rs | 2 +- src/core/authentication/handler.rs | 2 +- src/core/authentication/mod.rs | 2 +- src/core/core_tests.rs | 2 +- src/core/databases/mod.rs | 1 + src/core/databases/setup.rs | 20 ++++++++++++++++++++ src/core/services/mod.rs | 19 +------------------ src/core/whitelist/whitelist_tests.rs | 3 ++- src/servers/http/v1/handlers/announce.rs | 2 +- src/servers/http/v1/services/announce.rs | 4 ++-- src/servers/http/v1/services/scrape.rs | 2 +- src/servers/udp/handlers.rs | 4 ++-- 13 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 src/core/databases/setup.rs diff --git a/src/bootstrap/app.rs b/src/bootstrap/app.rs index 7661a36ec..44fc4ea00 100644 --- a/src/bootstrap/app.rs +++ b/src/bootstrap/app.rs @@ -27,8 +27,9 @@ use crate::core::authentication::handler::KeysHandler; use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository; use crate::core::authentication::service; +use crate::core::databases::setup::initialize_database; use crate::core::scrape_handler::ScrapeHandler; -use crate::core::services::{initialize_database, initialize_whitelist_manager}; +use crate::core::services::initialize_whitelist_manager; use crate::core::statistics; use crate::core::torrent::manager::TorrentsManager; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; diff --git a/src/core/announce_handler.rs b/src/core/announce_handler.rs index 1a5f84d47..816663bf6 100644 --- a/src/core/announce_handler.rs +++ b/src/core/announce_handler.rs @@ -414,7 +414,7 @@ mod tests { use crate::core::announce_handler::tests::the_announce_handler::peer_ip; use crate::core::announce_handler::{AnnounceHandler, PeersWanted}; use crate::core::core_tests::{sample_info_hash, sample_peer}; - use crate::core::services::initialize_database; + use crate::core::databases::setup::initialize_database; use crate::core::torrent::manager::TorrentsManager; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; diff --git a/src/core/authentication/handler.rs b/src/core/authentication/handler.rs index 5ec9a11b4..d6477a948 100644 --- a/src/core/authentication/handler.rs +++ b/src/core/authentication/handler.rs @@ -246,7 +246,7 @@ mod tests { use crate::core::authentication::handler::KeysHandler; use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository; - use crate::core::services::initialize_database; + use crate::core::databases::setup::initialize_database; fn instantiate_keys_handler() -> KeysHandler { let config = configuration::ephemeral_private(); diff --git a/src/core/authentication/mod.rs b/src/core/authentication/mod.rs index 0180b3a1e..eddcc1ae7 100644 --- a/src/core/authentication/mod.rs +++ b/src/core/authentication/mod.rs @@ -27,7 +27,7 @@ mod tests { use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository; use crate::core::authentication::service; use crate::core::authentication::service::AuthenticationService; - use crate::core::services::initialize_database; + use crate::core::databases::setup::initialize_database; fn instantiate_keys_manager_and_authentication() -> (Arc, Arc) { let config = configuration::ephemeral_private(); diff --git a/src/core/core_tests.rs b/src/core/core_tests.rs index 45949bae2..35d5fb9b7 100644 --- a/src/core/core_tests.rs +++ b/src/core/core_tests.rs @@ -9,8 +9,8 @@ use torrust_tracker_primitives::peer::Peer; use torrust_tracker_primitives::DurationSinceUnixEpoch; use super::announce_handler::AnnounceHandler; +use super::databases::setup::initialize_database; use super::scrape_handler::ScrapeHandler; -use super::services::initialize_database; use super::torrent::repository::in_memory::InMemoryTorrentRepository; use super::torrent::repository::persisted::DatabasePersistentTorrentRepository; use super::whitelist::repository::in_memory::InMemoryWhitelist; diff --git a/src/core/databases/mod.rs b/src/core/databases/mod.rs index e0b1b4f1b..dec6b799d 100644 --- a/src/core/databases/mod.rs +++ b/src/core/databases/mod.rs @@ -46,6 +46,7 @@ pub mod driver; pub mod error; pub mod mysql; +pub mod setup; pub mod sqlite; use std::marker::PhantomData; diff --git a/src/core/databases/setup.rs b/src/core/databases/setup.rs new file mode 100644 index 000000000..728913e05 --- /dev/null +++ b/src/core/databases/setup.rs @@ -0,0 +1,20 @@ +use std::sync::Arc; + +use torrust_tracker_configuration::v2_0_0::database; +use torrust_tracker_configuration::Configuration; + +use super::driver::{self, Driver}; +use super::Database; + +/// # Panics +/// +/// Will panic if database cannot be initialized. +#[must_use] +pub fn initialize_database(config: &Configuration) -> Arc> { + let driver = match config.core.database.driver { + database::Driver::Sqlite3 => Driver::Sqlite3, + database::Driver::MySQL => Driver::MySQL, + }; + + Arc::new(driver::build(&driver, &config.core.database.path).expect("Database driver build failed.")) +} diff --git a/src/core/services/mod.rs b/src/core/services/mod.rs index 1050e41f8..4d30fa966 100644 --- a/src/core/services/mod.rs +++ b/src/core/services/mod.rs @@ -1,27 +1,10 @@ use std::sync::Arc; -use databases::driver::Driver; -use torrust_tracker_configuration::v2_0_0::database; -use torrust_tracker_configuration::Configuration; - -use super::databases::{self, Database}; +use super::databases::Database; use super::whitelist::manager::WhitelistManager; use super::whitelist::repository::in_memory::InMemoryWhitelist; use super::whitelist::repository::persisted::DatabaseWhitelist; -/// # Panics -/// -/// Will panic if database cannot be initialized. -#[must_use] -pub fn initialize_database(config: &Configuration) -> Arc> { - let driver = match config.core.database.driver { - database::Driver::Sqlite3 => Driver::Sqlite3, - database::Driver::MySQL => Driver::MySQL, - }; - - Arc::new(databases::driver::build(&driver, &config.core.database.path).expect("Database driver build failed.")) -} - #[must_use] pub fn initialize_whitelist_manager( database: Arc>, diff --git a/src/core/whitelist/whitelist_tests.rs b/src/core/whitelist/whitelist_tests.rs index aa9c5ca14..cbe1e6488 100644 --- a/src/core/whitelist/whitelist_tests.rs +++ b/src/core/whitelist/whitelist_tests.rs @@ -5,7 +5,8 @@ use torrust_tracker_configuration::Configuration; use super::authorization::WhitelistAuthorization; use super::manager::WhitelistManager; use super::repository::in_memory::InMemoryWhitelist; -use crate::core::services::{initialize_database, initialize_whitelist_manager}; +use crate::core::databases::setup::initialize_database; +use crate::core::services::initialize_whitelist_manager; #[must_use] pub fn initialize_whitelist_services(config: &Configuration) -> (Arc, Arc) { diff --git a/src/servers/http/v1/handlers/announce.rs b/src/servers/http/v1/handlers/announce.rs index 3de17df58..544d706fa 100644 --- a/src/servers/http/v1/handlers/announce.rs +++ b/src/servers/http/v1/handlers/announce.rs @@ -257,7 +257,7 @@ mod tests { use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::service::AuthenticationService; use crate::core::core_tests::sample_info_hash; - use crate::core::services::initialize_database; + use crate::core::databases::setup::initialize_database; use crate::core::statistics; use crate::core::statistics::event::sender::Sender; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; diff --git a/src/servers/http/v1/services/announce.rs b/src/servers/http/v1/services/announce.rs index 6314c0a98..ee682559e 100644 --- a/src/servers/http/v1/services/announce.rs +++ b/src/servers/http/v1/services/announce.rs @@ -65,7 +65,7 @@ mod tests { use torrust_tracker_test_helpers::configuration; use crate::core::announce_handler::AnnounceHandler; - use crate::core::services::initialize_database; + use crate::core::databases::setup::initialize_database; use crate::core::statistics; use crate::core::statistics::event::sender::Sender; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -138,7 +138,7 @@ mod tests { use super::{sample_peer_using_ipv4, sample_peer_using_ipv6}; use crate::core::announce_handler::{AnnounceHandler, PeersWanted}; use crate::core::core_tests::sample_info_hash; - use crate::core::services::initialize_database; + use crate::core::databases::setup::initialize_database; use crate::core::statistics; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; diff --git a/src/servers/http/v1/services/scrape.rs b/src/servers/http/v1/services/scrape.rs index 16821e724..b5a858b83 100644 --- a/src/servers/http/v1/services/scrape.rs +++ b/src/servers/http/v1/services/scrape.rs @@ -82,8 +82,8 @@ mod tests { use crate::core::announce_handler::AnnounceHandler; use crate::core::core_tests::sample_info_hash; + use crate::core::databases::setup::initialize_database; use crate::core::scrape_handler::ScrapeHandler; - use crate::core::services::initialize_database; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::core::whitelist::authorization::WhitelistAuthorization; diff --git a/src/servers/udp/handlers.rs b/src/servers/udp/handlers.rs index f531718db..90c32771f 100644 --- a/src/servers/udp/handlers.rs +++ b/src/servers/udp/handlers.rs @@ -475,8 +475,8 @@ mod tests { use super::gen_remote_fingerprint; use crate::core::announce_handler::AnnounceHandler; + use crate::core::databases::setup::initialize_database; use crate::core::scrape_handler::ScrapeHandler; - use crate::core::services::initialize_database; use crate::core::statistics::event::sender::Sender; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; @@ -1393,7 +1393,7 @@ mod tests { use mockall::predicate::eq; use crate::core::announce_handler::AnnounceHandler; - use crate::core::services::initialize_database; + use crate::core::databases::setup::initialize_database; use crate::core::statistics; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; From 1db58b16603c225585842c85e9f8a759623e0722 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 29 Jan 2025 13:17:16 +0000 Subject: [PATCH 4/5] refactor: [#1221] move whitelist manager setup to whitelist context --- src/bootstrap/app.rs | 2 +- src/core/mod.rs | 1 - src/core/whitelist/mod.rs | 1 + src/core/{services/mod.rs => whitelist/setup.rs} | 8 ++++---- src/core/whitelist/whitelist_tests.rs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) rename src/core/{services/mod.rs => whitelist/setup.rs} (61%) diff --git a/src/bootstrap/app.rs b/src/bootstrap/app.rs index 44fc4ea00..8a084dc7f 100644 --- a/src/bootstrap/app.rs +++ b/src/bootstrap/app.rs @@ -29,13 +29,13 @@ use crate::core::authentication::key::repository::persisted::DatabaseKeyReposito use crate::core::authentication::service; use crate::core::databases::setup::initialize_database; use crate::core::scrape_handler::ScrapeHandler; -use crate::core::services::initialize_whitelist_manager; use crate::core::statistics; use crate::core::torrent::manager::TorrentsManager; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::core::whitelist::authorization::WhitelistAuthorization; use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; +use crate::core::whitelist::setup::initialize_whitelist_manager; use crate::servers::udp::server::banning::BanService; use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP; use crate::shared::crypto::ephemeral_instance_keys; diff --git a/src/core/mod.rs b/src/core/mod.rs index 7d5e7d4d6..038264446 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -444,7 +444,6 @@ pub mod authentication; pub mod databases; pub mod error; pub mod scrape_handler; -pub mod services; pub mod statistics; pub mod torrent; pub mod whitelist; diff --git a/src/core/whitelist/mod.rs b/src/core/whitelist/mod.rs index c23740111..1f5f87626 100644 --- a/src/core/whitelist/mod.rs +++ b/src/core/whitelist/mod.rs @@ -1,6 +1,7 @@ pub mod authorization; pub mod manager; pub mod repository; +pub mod setup; pub mod whitelist_tests; #[cfg(test)] diff --git a/src/core/services/mod.rs b/src/core/whitelist/setup.rs similarity index 61% rename from src/core/services/mod.rs rename to src/core/whitelist/setup.rs index 4d30fa966..bdd35737c 100644 --- a/src/core/services/mod.rs +++ b/src/core/whitelist/setup.rs @@ -1,9 +1,9 @@ use std::sync::Arc; -use super::databases::Database; -use super::whitelist::manager::WhitelistManager; -use super::whitelist::repository::in_memory::InMemoryWhitelist; -use super::whitelist::repository::persisted::DatabaseWhitelist; +use super::manager::WhitelistManager; +use super::repository::in_memory::InMemoryWhitelist; +use super::repository::persisted::DatabaseWhitelist; +use crate::core::databases::Database; #[must_use] pub fn initialize_whitelist_manager( diff --git a/src/core/whitelist/whitelist_tests.rs b/src/core/whitelist/whitelist_tests.rs index cbe1e6488..38c2bbde3 100644 --- a/src/core/whitelist/whitelist_tests.rs +++ b/src/core/whitelist/whitelist_tests.rs @@ -6,7 +6,7 @@ use super::authorization::WhitelistAuthorization; use super::manager::WhitelistManager; use super::repository::in_memory::InMemoryWhitelist; use crate::core::databases::setup::initialize_database; -use crate::core::services::initialize_whitelist_manager; +use crate::core::whitelist::setup::initialize_whitelist_manager; #[must_use] pub fn initialize_whitelist_services(config: &Configuration) -> (Arc, Arc) { From 4921f7b31b73752a0822f9d808c610da4854a31d Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 29 Jan 2025 13:30:08 +0000 Subject: [PATCH 5/5] fix: [#1221] docs links --- src/core/mod.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/core/mod.rs b/src/core/mod.rs index 038264446..125a67b5a 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -344,10 +344,10 @@ //! //! # Services //! -//! Services are domain services on top of the core tracker. Right now there are two types of service: +//! Services are domain services on top of the core tracker domain. Right now there are two types of service: //! -//! - For statistics -//! - For torrents +//! - For statistics: [`crate::core::statistics::services`] +//! - For torrents: [`crate::core::torrent::services`] //! //! Services usually format the data inside the tracker to make it easier to consume by other parts. //! They also decouple the internal data structure, used by the tracker, from the way we deliver that data to the consumers. @@ -356,8 +356,6 @@ //! //! Services can include extra features like pagination, for example. //! -//! Refer to [`services`] module for more information about services. -//! //! # Authentication //! //! One of the core `Tracker` responsibilities is to create and keep authentication keys. Auth keys are used by HTTP trackers