Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ 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, statistics};
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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/announce_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/authentication/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/core/authentication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeysHandler>, Arc<AuthenticationService>) {
let config = configuration::ephemeral_private();
Expand Down
2 changes: 1 addition & 1 deletion src/core/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/core/databases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
pub mod driver;
pub mod error;
pub mod mysql;
pub mod setup;
pub mod sqlite;

use std::marker::PhantomData;
Expand Down
20 changes: 20 additions & 0 deletions src/core/databases/setup.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn Database>> {
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."))
}
9 changes: 3 additions & 6 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -444,7 +442,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;
Expand Down
41 changes: 0 additions & 41 deletions src/core/services/mod.rs

This file was deleted.

2 changes: 2 additions & 0 deletions src/core/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ pub mod event;
pub mod keeper;
pub mod metrics;
pub mod repository;
pub mod services;
pub mod setup;
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -36,8 +36,6 @@
//! // ...
//! }
//! ```
pub mod setup;

use std::sync::Arc;

use tokio::sync::RwLock;
Expand Down Expand Up @@ -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;

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/core/torrent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
//!
pub mod manager;
pub mod repository;
pub mod services;

use torrust_tracker_torrent_repository::TorrentsSkipMapMutexStd;

Expand Down
8 changes: 4 additions & 4 deletions src/core/services/torrent.rs → src/core/torrent/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/core/whitelist/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod authorization;
pub mod manager;
pub mod repository;
pub mod setup;
pub mod whitelist_tests;

#[cfg(test)]
Expand Down
15 changes: 15 additions & 0 deletions src/core/whitelist/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::sync::Arc;

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(
database: Arc<Box<dyn Database>>,
in_memory_whitelist: Arc<InMemoryWhitelist>,
) -> Arc<WhitelistManager> {
let database_whitelist = Arc::new(DatabaseWhitelist::new(database));
Arc::new(WhitelistManager::new(database_whitelist, in_memory_whitelist))
}
3 changes: 2 additions & 1 deletion src/core/whitelist/whitelist_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::whitelist::setup::initialize_whitelist_manager;

#[must_use]
pub fn initialize_whitelist_services(config: &Configuration) -> (Arc<WhitelistAuthorization>, Arc<WhitelistManager>) {
Expand Down
2 changes: 1 addition & 1 deletion src/servers/apis/v1/context/stats/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/servers/apis/v1/context/stats/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/servers/apis/v1/context/stats/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/servers/apis/v1/context/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/servers/apis/v1/context/torrent/resources/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/servers/apis/v1/context/torrent/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
3 changes: 2 additions & 1 deletion src/servers/http/v1/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::databases::setup::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;
Expand Down
2 changes: 1 addition & 1 deletion src/servers/http/v1/handlers/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/servers/http/v1/services/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::databases::setup::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;
Expand Down Expand Up @@ -137,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;
Expand Down
6 changes: 3 additions & 3 deletions src/servers/http/v1/services/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Loading