Skip to content

Commit df4a59e

Browse files
committed
Merge #1310: Refactor packages: changes needed to extract new packages
56ae9f3 refactor: [#1298] rename method (Jose Celano) 4b250c3 refactor: [#1298] convert fn into static method (Jose Celano) 0a44d01 refactor: [#1298] remove duplicate code (Jose Celano) 3ce6fb2 refactor: [#1298] reorganize code (Jose Celano) 3052ebe refactor: [#1298] convert fn into static method (Jose Celano) c8ec781 refactor: [#1298] move HttpApiContainer to tracker-api-core package (Jose Celano) be05211 refactor: [#1298] remove duplicate code (Jose Celano) 292a7eb refactor: [#1298] convert fn into static method (Jose Celano) 5c1fd97 refactor: [#1298] rename struct to UdpTrackerCoreContainer (Jose Celano) de75a32 refactor: [#1298] move UdpTrackerContainer to udp-tracker-core package (Jose Celano) 786000b refactor: [#1298] remove duplicate code (Jose Celano) 1eca6a3 refactor: [#1298] rename struct and field (Jose Celano) b562f8d refacotr: [#1298] remove duplicate code (Jose Celano) 06a29cd refactor: [#1298] convert fn into static method (Jose Celano) d152043 refactor: [#1298] mmove HttpTrackerContainer to http-tracker-core (Jose Celano) 9f18c6e refactor: [#1298] extract TrackerCoreContainer (Jose Celano) bdec261 refactor: [#1298] move functions to container mod (Jose Celano) 9ba5cdd refactor: [#1298] inine function (Jose Celano) 78002d7 refactor: [#1298] remove dependency on global app container in packages (Jose Celano) 1bd5f0a refactor: [#1298] move loggin setup to configration package (Jose Celano) 265da2d refactor: [#1298] inject only logging config in logging setup (Jose Celano) Pull request description: Refactor packages: changes needed to extract new packages. ACKs for top commit: josecelano: ACK 56ae9f3 Tree-SHA512: 6d7d2660fe90570e45ab901bb1ac5ab0cd9f3430eecd538ef7c9cca024b51474aed1f300b65ad9c389acf88573cdc88eb66884716d6add2e39910e43b6f7d48f
2 parents ecd70ea + 56ae9f3 commit df4a59e

File tree

41 files changed

+806
-474
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+806
-474
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/axum-http-tracker-server/src/container.rs

-17
This file was deleted.

packages/axum-http-tracker-server/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@
305305
//! - [Bencode to Json Online converter](https://chocobo1.github.io/bencode_online).
306306
use serde::{Deserialize, Serialize};
307307

308-
pub mod container;
309308
pub mod server;
310309
pub mod v1;
311310

packages/axum-http-tracker-server/src/server.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44

55
use axum_server::tls_rustls::RustlsConfig;
66
use axum_server::Handle;
7+
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
78
use derive_more::Constructor;
89
use futures::future::BoxFuture;
910
use tokio::sync::oneshot::{Receiver, Sender};
@@ -15,7 +16,6 @@ use torrust_server_lib::signals::{Halted, Started};
1516
use tracing::instrument;
1617

1718
use super::v1::routes::router;
18-
use crate::container::HttpTrackerContainer;
1919
use crate::HTTP_TRACKER_LOG_TARGET;
2020

2121
/// Error that can occur when starting or stopping the HTTP server.
@@ -45,7 +45,7 @@ impl Launcher {
4545
#[instrument(skip(self, http_tracker_container, tx_start, rx_halt))]
4646
fn start(
4747
&self,
48-
http_tracker_container: Arc<HttpTrackerContainer>,
48+
http_tracker_container: Arc<HttpTrackerCoreContainer>,
4949
tx_start: Sender<Started>,
5050
rx_halt: Receiver<Halted>,
5151
) -> BoxFuture<'static, ()> {
@@ -160,7 +160,7 @@ impl HttpServer<Stopped> {
160160
/// back to the main thread.
161161
pub async fn start(
162162
self,
163-
http_tracker_container: Arc<HttpTrackerContainer>,
163+
http_tracker_container: Arc<HttpTrackerCoreContainer>,
164164
form: ServiceRegistrationForm,
165165
) -> Result<HttpServer<Running>, Error> {
166166
let (tx_start, rx_start) = tokio::sync::oneshot::channel::<Started>();
@@ -238,6 +238,7 @@ pub fn check_fn(binding: &SocketAddr) -> ServiceHealthCheckJob {
238238
mod tests {
239239
use std::sync::Arc;
240240

241+
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
241242
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
242243
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
243244
use bittorrent_tracker_core::authentication::service;
@@ -252,10 +253,9 @@ mod tests {
252253
use torrust_tracker_configuration::Configuration;
253254
use torrust_tracker_test_helpers::configuration::ephemeral_public;
254255

255-
use crate::container::HttpTrackerContainer;
256256
use crate::server::{HttpServer, Launcher};
257257

258-
pub fn initialize_container(configuration: &Configuration) -> HttpTrackerContainer {
258+
pub fn initialize_container(configuration: &Configuration) -> HttpTrackerCoreContainer {
259259
let core_config = Arc::new(configuration.core.clone());
260260

261261
let http_trackers = configuration
@@ -268,9 +268,10 @@ mod tests {
268268
let http_tracker_config = Arc::new(http_tracker_config.clone());
269269

270270
// HTTP stats
271-
let (http_stats_event_sender, _http_stats_repository) =
271+
let (http_stats_event_sender, http_stats_repository) =
272272
bittorrent_http_tracker_core::statistics::setup::factory(configuration.core.tracker_usage_statistics);
273273
let http_stats_event_sender = Arc::new(http_stats_event_sender);
274+
let http_stats_repository = Arc::new(http_stats_repository);
274275

275276
let database = initialize_database(&configuration.core);
276277
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
@@ -292,14 +293,16 @@ mod tests {
292293

293294
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));
294295

295-
HttpTrackerContainer {
296+
HttpTrackerCoreContainer {
296297
core_config,
297-
http_tracker_config,
298298
announce_handler,
299299
scrape_handler,
300300
whitelist_authorization,
301-
http_stats_event_sender,
302301
authentication_service,
302+
303+
http_tracker_config,
304+
http_stats_event_sender,
305+
http_stats_repository,
303306
}
304307
}
305308

packages/axum-http-tracker-server/src/v1/routes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use axum::response::Response;
99
use axum::routing::get;
1010
use axum::{BoxError, Router};
1111
use axum_client_ip::SecureClientIpSource;
12+
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
1213
use hyper::{Request, StatusCode};
1314
use torrust_server_lib::logging::Latency;
1415
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
@@ -23,15 +24,14 @@ use tower_http::LatencyUnit;
2324
use tracing::{instrument, Level, Span};
2425

2526
use super::handlers::{announce, health_check, scrape};
26-
use crate::container::HttpTrackerContainer;
2727
use crate::HTTP_TRACKER_LOG_TARGET;
2828

2929
/// It adds the routes to the router.
3030
///
3131
/// > **NOTICE**: it's added a layer to get the client IP from the connection
3232
/// > info. The tracker could use the connection info to get the client IP.
3333
#[instrument(skip(http_tracker_container, server_socket_addr))]
34-
pub fn router(http_tracker_container: Arc<HttpTrackerContainer>, server_socket_addr: SocketAddr) -> Router {
34+
pub fn router(http_tracker_container: Arc<HttpTrackerCoreContainer>, server_socket_addr: SocketAddr) -> Router {
3535
Router::new()
3636
// Health check
3737
.route("/health_check", get(health_check::handler))

packages/configuration/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ serde_with = "3"
2424
thiserror = "2"
2525
toml = "0"
2626
torrust-tracker-located-error = { version = "3.0.0-develop", path = "../located-error" }
27+
tracing = "0"
28+
tracing-subscriber = { version = "0", features = ["json"] }
2729
url = "2"
2830

2931
[dev-dependencies]

packages/configuration/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Torrust Tracker, which is a `BitTorrent` tracker server.
55
//!
66
//! The current version for configuration is [`v2_0_0`].
7+
pub mod logging;
78
pub mod v2_0_0;
89
pub mod validator;
910

@@ -37,6 +38,7 @@ pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_TRACKER_CONFIG_TOML_PATH";
3738

3839
pub type Configuration = v2_0_0::Configuration;
3940
pub type Core = v2_0_0::core::Core;
41+
pub type Logging = v2_0_0::logging::Logging;
4042
pub type HealthCheckApi = v2_0_0::health_check_api::HealthCheckApi;
4143
pub type HttpApi = v2_0_0::tracker_api::HttpApi;
4244
pub type HttpTracker = v2_0_0::http_tracker::HttpTracker;

src/bootstrap/logging.rs packages/configuration/src/logging.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
//! Refer to the [configuration crate documentation](https://docs.rs/torrust-tracker-configuration) to know how to change log settings.
1414
use std::sync::Once;
1515

16-
use torrust_tracker_configuration::{Configuration, Threshold};
1716
use tracing::level_filters::LevelFilter;
1817

18+
use crate::{Logging, Threshold};
19+
1920
static INIT: Once = Once::new();
2021

2122
/// It redirects the log info to the standard output with the log threshold
2223
/// defined in the configuration.
23-
pub fn setup(cfg: &Configuration) {
24-
let tracing_level = map_to_tracing_level_filter(&cfg.logging.threshold);
24+
pub fn setup(cfg: &Logging) {
25+
let tracing_level = map_to_tracing_level_filter(&cfg.threshold);
2526

2627
if tracing_level == LevelFilter::OFF {
2728
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::sync::Arc;
2+
3+
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
4+
use bittorrent_tracker_core::authentication::service::AuthenticationService;
5+
use bittorrent_tracker_core::container::TrackerCoreContainer;
6+
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
7+
use bittorrent_tracker_core::whitelist;
8+
use torrust_tracker_configuration::{Core, HttpTracker};
9+
10+
use crate::statistics;
11+
12+
pub struct HttpTrackerCoreContainer {
13+
// todo: replace with TrackerCoreContainer
14+
pub core_config: Arc<Core>,
15+
pub announce_handler: Arc<AnnounceHandler>,
16+
pub scrape_handler: Arc<ScrapeHandler>,
17+
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
18+
pub authentication_service: Arc<AuthenticationService>,
19+
20+
pub http_tracker_config: Arc<HttpTracker>,
21+
pub http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
22+
pub http_stats_repository: Arc<statistics::repository::Repository>,
23+
}
24+
25+
impl HttpTrackerCoreContainer {
26+
#[must_use]
27+
pub fn initialize(core_config: &Arc<Core>, http_tracker_config: &Arc<HttpTracker>) -> Arc<Self> {
28+
let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(core_config));
29+
Self::initialize_from(&tracker_core_container, http_tracker_config)
30+
}
31+
32+
#[must_use]
33+
pub fn initialize_from(
34+
tracker_core_container: &Arc<TrackerCoreContainer>,
35+
http_tracker_config: &Arc<HttpTracker>,
36+
) -> Arc<Self> {
37+
let (http_stats_event_sender, http_stats_repository) =
38+
statistics::setup::factory(tracker_core_container.core_config.tracker_usage_statistics);
39+
let http_stats_event_sender = Arc::new(http_stats_event_sender);
40+
let http_stats_repository = Arc::new(http_stats_repository);
41+
42+
Arc::new(Self {
43+
core_config: tracker_core_container.core_config.clone(),
44+
announce_handler: tracker_core_container.announce_handler.clone(),
45+
scrape_handler: tracker_core_container.scrape_handler.clone(),
46+
whitelist_authorization: tracker_core_container.whitelist_authorization.clone(),
47+
authentication_service: tracker_core_container.authentication_service.clone(),
48+
49+
http_tracker_config: http_tracker_config.clone(),
50+
http_stats_event_sender: http_stats_event_sender.clone(),
51+
http_stats_repository: http_stats_repository.clone(),
52+
})
53+
}
54+
}

packages/http-tracker-core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod container;
12
pub mod services;
23
pub mod statistics;
34

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::sync::Arc;
2+
3+
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
4+
use bittorrent_tracker_core::authentication::handler::KeysHandler;
5+
use bittorrent_tracker_core::container::TrackerCoreContainer;
6+
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
7+
use bittorrent_tracker_core::whitelist::manager::WhitelistManager;
8+
use bittorrent_udp_tracker_core::container::UdpTrackerCoreContainer;
9+
use bittorrent_udp_tracker_core::services::banning::BanService;
10+
use bittorrent_udp_tracker_core::{self};
11+
use tokio::sync::RwLock;
12+
use torrust_tracker_configuration::{Core, HttpApi, HttpTracker, UdpTracker};
13+
14+
pub struct TrackerHttpApiCoreContainer {
15+
// todo: replace with TrackerCoreContainer
16+
pub core_config: Arc<Core>,
17+
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
18+
pub keys_handler: Arc<KeysHandler>,
19+
pub whitelist_manager: Arc<WhitelistManager>,
20+
21+
// todo: replace with HttpTrackerCoreContainer
22+
pub http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
23+
24+
// todo: replace with UdpTrackerCoreContainer
25+
pub ban_service: Arc<RwLock<BanService>>,
26+
pub udp_stats_repository: Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
27+
28+
pub http_api_config: Arc<HttpApi>,
29+
}
30+
31+
impl TrackerHttpApiCoreContainer {
32+
#[must_use]
33+
pub fn initialize(
34+
core_config: &Arc<Core>,
35+
http_tracker_config: &Arc<HttpTracker>,
36+
udp_tracker_config: &Arc<UdpTracker>,
37+
http_api_config: &Arc<HttpApi>,
38+
) -> Arc<TrackerHttpApiCoreContainer> {
39+
let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(core_config));
40+
let http_tracker_core_container = HttpTrackerCoreContainer::initialize_from(&tracker_core_container, http_tracker_config);
41+
let udp_tracker_core_container = UdpTrackerCoreContainer::initialize_from(&tracker_core_container, udp_tracker_config);
42+
43+
Self::initialize_from(
44+
&tracker_core_container,
45+
&http_tracker_core_container,
46+
&udp_tracker_core_container,
47+
http_api_config,
48+
)
49+
}
50+
51+
#[must_use]
52+
pub fn initialize_from(
53+
tracker_core_container: &Arc<TrackerCoreContainer>,
54+
http_tracker_core_container: &Arc<HttpTrackerCoreContainer>,
55+
udp_tracker_core_container: &Arc<UdpTrackerCoreContainer>,
56+
http_api_config: &Arc<HttpApi>,
57+
) -> Arc<TrackerHttpApiCoreContainer> {
58+
Arc::new(TrackerHttpApiCoreContainer {
59+
core_config: tracker_core_container.core_config.clone(),
60+
in_memory_torrent_repository: tracker_core_container.in_memory_torrent_repository.clone(),
61+
keys_handler: tracker_core_container.keys_handler.clone(),
62+
whitelist_manager: tracker_core_container.whitelist_manager.clone(),
63+
64+
http_stats_repository: http_tracker_core_container.http_stats_repository.clone(),
65+
66+
ban_service: udp_tracker_core_container.ban_service.clone(),
67+
udp_stats_repository: udp_tracker_core_container.udp_stats_repository.clone(),
68+
69+
http_api_config: http_api_config.clone(),
70+
})
71+
}
72+
}

packages/tracker-api-core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod container;
12
pub mod statistics;

0 commit comments

Comments
 (0)