Skip to content

Commit de7c6f0

Browse files
committed
Merge torrust#1222: Overhaul core Tracker: reorganize code
4921f7b fix: [torrust#1221] docs links (Jose Celano) 1db58b1 refactor: [torrust#1221] move whitelist manager setup to whitelist context (Jose Celano) 716e7b2 refactor: [torrust#1221] move DB setup to databases context (Jose Celano) d830c78 refactor: [torrust#1221] move core torrent mod to torrent context (Jose Celano) 948cc8c refactor: [torrust#1221] move core statistics mod to statistics context (Jose Celano) Pull request description: Overhaul core Tracker: reorganize code ACKs for top commit: josecelano: ACK 4921f7b Tree-SHA512: 5497f4318f80404ce5f4f4826b0bb99b3f633ec918c625b8acecc2bb0811e4cb82da80d0b06df3c733b68ff605bfd449736dd64c522aa2217f5391185826516f
2 parents 46e9d25 + 4921f7b commit de7c6f0

File tree

28 files changed

+85
-86
lines changed

28 files changed

+85
-86
lines changed

src/bootstrap/app.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ use crate::core::authentication::handler::KeysHandler;
2727
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
2828
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
2929
use crate::core::authentication::service;
30+
use crate::core::databases::setup::initialize_database;
3031
use crate::core::scrape_handler::ScrapeHandler;
31-
use crate::core::services::{initialize_database, initialize_whitelist_manager, statistics};
32+
use crate::core::statistics;
3233
use crate::core::torrent::manager::TorrentsManager;
3334
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
3435
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
3536
use crate::core::whitelist::authorization::WhitelistAuthorization;
3637
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
38+
use crate::core::whitelist::setup::initialize_whitelist_manager;
3739
use crate::servers::udp::server::banning::BanService;
3840
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
3941
use crate::shared::crypto::ephemeral_instance_keys;

src/core/announce_handler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ mod tests {
414414
use crate::core::announce_handler::tests::the_announce_handler::peer_ip;
415415
use crate::core::announce_handler::{AnnounceHandler, PeersWanted};
416416
use crate::core::core_tests::{sample_info_hash, sample_peer};
417-
use crate::core::services::initialize_database;
417+
use crate::core::databases::setup::initialize_database;
418418
use crate::core::torrent::manager::TorrentsManager;
419419
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
420420
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;

src/core/authentication/handler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ mod tests {
246246
use crate::core::authentication::handler::KeysHandler;
247247
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
248248
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
249-
use crate::core::services::initialize_database;
249+
use crate::core::databases::setup::initialize_database;
250250

251251
fn instantiate_keys_handler() -> KeysHandler {
252252
let config = configuration::ephemeral_private();

src/core/authentication/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod tests {
2727
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
2828
use crate::core::authentication::service;
2929
use crate::core::authentication::service::AuthenticationService;
30-
use crate::core::services::initialize_database;
30+
use crate::core::databases::setup::initialize_database;
3131

3232
fn instantiate_keys_manager_and_authentication() -> (Arc<KeysHandler>, Arc<AuthenticationService>) {
3333
let config = configuration::ephemeral_private();

src/core/core_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use torrust_tracker_primitives::peer::Peer;
99
use torrust_tracker_primitives::DurationSinceUnixEpoch;
1010

1111
use super::announce_handler::AnnounceHandler;
12+
use super::databases::setup::initialize_database;
1213
use super::scrape_handler::ScrapeHandler;
13-
use super::services::initialize_database;
1414
use super::torrent::repository::in_memory::InMemoryTorrentRepository;
1515
use super::torrent::repository::persisted::DatabasePersistentTorrentRepository;
1616
use super::whitelist::repository::in_memory::InMemoryWhitelist;

src/core/databases/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
pub mod driver;
4747
pub mod error;
4848
pub mod mysql;
49+
pub mod setup;
4950
pub mod sqlite;
5051

5152
use std::marker::PhantomData;

src/core/databases/setup.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::sync::Arc;
2+
3+
use torrust_tracker_configuration::v2_0_0::database;
4+
use torrust_tracker_configuration::Configuration;
5+
6+
use super::driver::{self, Driver};
7+
use super::Database;
8+
9+
/// # Panics
10+
///
11+
/// Will panic if database cannot be initialized.
12+
#[must_use]
13+
pub fn initialize_database(config: &Configuration) -> Arc<Box<dyn Database>> {
14+
let driver = match config.core.database.driver {
15+
database::Driver::Sqlite3 => Driver::Sqlite3,
16+
database::Driver::MySQL => Driver::MySQL,
17+
};
18+
19+
Arc::new(driver::build(&driver, &config.core.database.path).expect("Database driver build failed."))
20+
}

src/core/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@
344344
//!
345345
//! # Services
346346
//!
347-
//! Services are domain services on top of the core tracker. Right now there are two types of service:
347+
//! Services are domain services on top of the core tracker domain. Right now there are two types of service:
348348
//!
349-
//! - For statistics
350-
//! - For torrents
349+
//! - For statistics: [`crate::core::statistics::services`]
350+
//! - For torrents: [`crate::core::torrent::services`]
351351
//!
352352
//! Services usually format the data inside the tracker to make it easier to consume by other parts.
353353
//! 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 @@
356356
//!
357357
//! Services can include extra features like pagination, for example.
358358
//!
359-
//! Refer to [`services`] module for more information about services.
360-
//!
361359
//! # Authentication
362360
//!
363361
//! One of the core `Tracker` responsibilities is to create and keep authentication keys. Auth keys are used by HTTP trackers
@@ -444,7 +442,6 @@ pub mod authentication;
444442
pub mod databases;
445443
pub mod error;
446444
pub mod scrape_handler;
447-
pub mod services;
448445
pub mod statistics;
449446
pub mod torrent;
450447
pub mod whitelist;

src/core/services/mod.rs

-41
This file was deleted.

src/core/statistics/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ pub mod event;
2828
pub mod keeper;
2929
pub mod metrics;
3030
pub mod repository;
31+
pub mod services;
32+
pub mod setup;

src/core/services/statistics/mod.rs src/core/statistics/services.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! It includes:
44
//!
5-
//! - A [`factory`](crate::core::services::statistics::setup::factory) function to build the structs needed to collect the tracker metrics.
5+
//! - A [`factory`](crate::core::statistics::setup::factory) function to build the structs needed to collect the tracker metrics.
66
//! - A [`get_metrics`] service to get the tracker [`metrics`](crate::core::statistics::metrics::Metrics).
77
//!
88
//! Tracker metrics are collected using a Publisher-Subscribe pattern.
@@ -36,8 +36,6 @@
3636
//! // ...
3737
//! }
3838
//! ```
39-
pub mod setup;
40-
4139
use std::sync::Arc;
4240

4341
use tokio::sync::RwLock;
@@ -117,9 +115,9 @@ mod tests {
117115
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
118116
use torrust_tracker_test_helpers::configuration;
119117

120-
use crate::core::services::statistics::{self, get_metrics, TrackerMetrics};
118+
use crate::core::statistics::services::{get_metrics, TrackerMetrics};
121119
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
122-
use crate::core::{self};
120+
use crate::core::{self, statistics};
123121
use crate::servers::udp::server::banning::BanService;
124122
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
125123

File renamed without changes.

src/core/torrent/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
//!
2828
pub mod manager;
2929
pub mod repository;
30+
pub mod services;
3031

3132
use torrust_tracker_torrent_repository::TorrentsSkipMapMutexStd;
3233

src/core/services/torrent.rs src/core/torrent/services.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ mod tests {
135135

136136
use bittorrent_primitives::info_hash::InfoHash;
137137

138-
use crate::core::services::torrent::tests::sample_peer;
139-
use crate::core::services::torrent::{get_torrent_info, Info};
140138
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
139+
use crate::core::torrent::services::tests::sample_peer;
140+
use crate::core::torrent::services::{get_torrent_info, Info};
141141

142142
#[tokio::test]
143143
async fn should_return_none_if_the_tracker_does_not_have_the_torrent() {
@@ -184,9 +184,9 @@ mod tests {
184184

185185
use bittorrent_primitives::info_hash::InfoHash;
186186

187-
use crate::core::services::torrent::tests::sample_peer;
188-
use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination};
189187
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
188+
use crate::core::torrent::services::tests::sample_peer;
189+
use crate::core::torrent::services::{get_torrents_page, BasicInfo, Pagination};
190190

191191
#[tokio::test]
192192
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {

src/core/whitelist/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod authorization;
22
pub mod manager;
33
pub mod repository;
4+
pub mod setup;
45
pub mod whitelist_tests;
56

67
#[cfg(test)]

src/core/whitelist/setup.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::sync::Arc;
2+
3+
use super::manager::WhitelistManager;
4+
use super::repository::in_memory::InMemoryWhitelist;
5+
use super::repository::persisted::DatabaseWhitelist;
6+
use crate::core::databases::Database;
7+
8+
#[must_use]
9+
pub fn initialize_whitelist_manager(
10+
database: Arc<Box<dyn Database>>,
11+
in_memory_whitelist: Arc<InMemoryWhitelist>,
12+
) -> Arc<WhitelistManager> {
13+
let database_whitelist = Arc::new(DatabaseWhitelist::new(database));
14+
Arc::new(WhitelistManager::new(database_whitelist, in_memory_whitelist))
15+
}

src/core/whitelist/whitelist_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use torrust_tracker_configuration::Configuration;
55
use super::authorization::WhitelistAuthorization;
66
use super::manager::WhitelistManager;
77
use super::repository::in_memory::InMemoryWhitelist;
8-
use crate::core::services::{initialize_database, initialize_whitelist_manager};
8+
use crate::core::databases::setup::initialize_database;
9+
use crate::core::whitelist::setup::initialize_whitelist_manager;
910

1011
#[must_use]
1112
pub fn initialize_whitelist_services(config: &Configuration) -> (Arc<WhitelistAuthorization>, Arc<WhitelistManager>) {

src/servers/apis/v1/context/stats/handlers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use serde::Deserialize;
99
use tokio::sync::RwLock;
1010

1111
use super::responses::{metrics_response, stats_response};
12-
use crate::core::services::statistics::get_metrics;
1312
use crate::core::statistics::repository::Repository;
13+
use crate::core::statistics::services::get_metrics;
1414
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
1515
use crate::servers::udp::server::banning::BanService;
1616

src/servers/apis/v1/context/stats/resources.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! API context.
33
use serde::{Deserialize, Serialize};
44

5-
use crate::core::services::statistics::TrackerMetrics;
5+
use crate::core::statistics::services::TrackerMetrics;
66

77
/// It contains all the statistics generated by the tracker.
88
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
@@ -121,8 +121,8 @@ mod tests {
121121
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
122122

123123
use super::Stats;
124-
use crate::core::services::statistics::TrackerMetrics;
125124
use crate::core::statistics::metrics::Metrics;
125+
use crate::core::statistics::services::TrackerMetrics;
126126

127127
#[test]
128128
fn stats_resource_should_be_converted_from_tracker_metrics() {

src/servers/apis/v1/context/stats/responses.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use axum::response::{IntoResponse, Json, Response};
44

55
use super::resources::Stats;
6-
use crate::core::services::statistics::TrackerMetrics;
6+
use crate::core::statistics::services::TrackerMetrics;
77

88
/// `200` response that contains the [`Stats`] resource as json.
99
#[must_use]

src/servers/apis/v1/context/torrent/handlers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use thiserror::Error;
1313
use torrust_tracker_primitives::pagination::Pagination;
1414

1515
use super::responses::{torrent_info_response, torrent_list_response, torrent_not_known_response};
16-
use crate::core::services::torrent::{get_torrent_info, get_torrents, get_torrents_page};
1716
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
17+
use crate::core::torrent::services::{get_torrent_info, get_torrents, get_torrents_page};
1818
use crate::servers::apis::v1::responses::invalid_info_hash_param_response;
1919
use crate::servers::apis::InfoHashParam;
2020

src/servers/apis/v1/context/torrent/resources/torrent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! the JSON response.
77
use serde::{Deserialize, Serialize};
88

9-
use crate::core::services::torrent::{BasicInfo, Info};
9+
use crate::core::torrent::services::{BasicInfo, Info};
1010

1111
/// `Torrent` API resource.
1212
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
@@ -102,7 +102,7 @@ mod tests {
102102
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
103103

104104
use super::Torrent;
105-
use crate::core::services::torrent::{BasicInfo, Info};
105+
use crate::core::torrent::services::{BasicInfo, Info};
106106
use crate::servers::apis::v1::context::torrent::resources::peer::Peer;
107107
use crate::servers::apis::v1::context::torrent::resources::torrent::ListItem;
108108

src/servers/apis/v1/context/torrent/responses.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use axum::response::{IntoResponse, Json, Response};
44
use serde_json::json;
55

66
use super::resources::torrent::{ListItem, Torrent};
7-
use crate::core::services::torrent::{BasicInfo, Info};
7+
use crate::core::torrent::services::{BasicInfo, Info};
88

99
/// `200` response that contains an array of
1010
/// [`ListItem`]

src/servers/http/v1/handlers/announce.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ mod tests {
257257
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
258258
use crate::core::authentication::service::AuthenticationService;
259259
use crate::core::core_tests::sample_info_hash;
260-
use crate::core::services::{initialize_database, statistics};
260+
use crate::core::databases::setup::initialize_database;
261+
use crate::core::statistics;
261262
use crate::core::statistics::event::sender::Sender;
262263
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
263264
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;

src/servers/http/v1/handlers/scrape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ mod tests {
177177
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
178178
use crate::core::authentication::service::AuthenticationService;
179179
use crate::core::scrape_handler::ScrapeHandler;
180-
use crate::core::services::statistics;
180+
use crate::core::statistics;
181181
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
182182
use crate::core::whitelist::authorization::WhitelistAuthorization;
183183
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;

src/servers/http/v1/services/announce.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ mod tests {
6565
use torrust_tracker_test_helpers::configuration;
6666

6767
use crate::core::announce_handler::AnnounceHandler;
68-
use crate::core::services::{initialize_database, statistics};
68+
use crate::core::databases::setup::initialize_database;
69+
use crate::core::statistics;
6970
use crate::core::statistics::event::sender::Sender;
7071
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
7172
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
@@ -137,7 +138,7 @@ mod tests {
137138
use super::{sample_peer_using_ipv4, sample_peer_using_ipv6};
138139
use crate::core::announce_handler::{AnnounceHandler, PeersWanted};
139140
use crate::core::core_tests::sample_info_hash;
140-
use crate::core::services::initialize_database;
141+
use crate::core::databases::setup::initialize_database;
141142
use crate::core::statistics;
142143
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
143144
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;

src/servers/http/v1/services/scrape.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ mod tests {
8282

8383
use crate::core::announce_handler::AnnounceHandler;
8484
use crate::core::core_tests::sample_info_hash;
85+
use crate::core::databases::setup::initialize_database;
8586
use crate::core::scrape_handler::ScrapeHandler;
86-
use crate::core::services::initialize_database;
8787
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
8888
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
8989
use crate::core::whitelist::authorization::WhitelistAuthorization;
@@ -153,7 +153,7 @@ mod tests {
153153

154154
#[tokio::test]
155155
async fn it_should_return_the_scrape_data_for_a_torrent() {
156-
let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false);
156+
let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false);
157157
let stats_event_sender = Arc::new(stats_event_sender);
158158

159159
let (announce_handler, scrape_handler) = initialize_announce_and_scrape_handlers_for_public_tracker();
@@ -236,7 +236,7 @@ mod tests {
236236

237237
#[tokio::test]
238238
async fn it_should_always_return_the_zeroed_scrape_data_for_a_torrent() {
239-
let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false);
239+
let (stats_event_sender, _stats_repository) = crate::core::statistics::setup::factory(false);
240240
let stats_event_sender = Arc::new(stats_event_sender);
241241

242242
let (announce_handler, _scrape_handler) = initialize_announce_and_scrape_handlers_for_public_tracker();

0 commit comments

Comments
 (0)