Skip to content

Commit

Permalink
dev: extract config from health check
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Jan 17, 2024
1 parent 3b49257 commit b310c75
Show file tree
Hide file tree
Showing 22 changed files with 392 additions and 183 deletions.
2 changes: 2 additions & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"Cyberneering",
"datagram",
"datetime",
"Deque",
"Dijke",
"distroless",
"dockerhub",
Expand Down Expand Up @@ -91,6 +92,7 @@
"Rasterbar",
"realpath",
"reannounce",
"Registar",
"repr",
"reqwest",
"rerequests",
Expand Down
29 changes: 23 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use tokio::task::JoinHandle;
use torrust_tracker_configuration::Configuration;

use crate::bootstrap::jobs::{health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
use crate::servers::registar::Registar;
use crate::{core, servers};

/// # Panics
Expand All @@ -36,9 +37,11 @@ use crate::{core, servers};
///
/// - Can't retrieve tracker keys from database.
/// - Can't load whitelist from database.
pub async fn start(config: Arc<Configuration>, tracker: Arc<core::Tracker>) -> Vec<JoinHandle<()>> {
pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<JoinHandle<()>> {
let mut jobs: Vec<JoinHandle<()>> = Vec::new();

let registar = Registar::default();

// Load peer keys
if tracker.is_private() {
tracker
Expand Down Expand Up @@ -67,31 +70,45 @@ pub async fn start(config: Arc<Configuration>, tracker: Arc<core::Tracker>) -> V
udp_tracker_config.bind_address, config.mode
);
} else {
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone()).await);
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone(), registar.give_form()).await);
}
}

// Start the HTTP blocks
for http_tracker_config in &config.http_trackers {
if let Some(job) = http_tracker::start_job(http_tracker_config, tracker.clone(), servers::http::Version::V1).await {
if let Some(job) = http_tracker::start_job(
http_tracker_config,
tracker.clone(),
registar.give_form(),
servers::http::Version::V1,
)
.await
{
jobs.push(job);
};
}

// Start HTTP API
if config.http_api.enabled {
if let Some(job) = tracker_apis::start_job(&config.http_api, tracker.clone(), servers::apis::Version::V1).await {
if let Some(job) = tracker_apis::start_job(
&config.http_api,
tracker.clone(),
registar.give_form(),
servers::apis::Version::V1,
)
.await
{
jobs.push(job);
};
}

// Start runners to remove torrents without peers, every interval
if config.inactive_peer_cleanup_interval > 0 {
jobs.push(torrent_cleanup::start_job(&config, &tracker));
jobs.push(torrent_cleanup::start_job(config, &tracker));
}

// Start Health Check API
jobs.push(health_check_api::start_job(config).await);
jobs.push(health_check_api::start_job(&config.health_check_api, registar.entries()).await);

jobs
}
9 changes: 4 additions & 5 deletions src/bootstrap/jobs/health_check_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
//!
//! Refer to the [configuration documentation](https://docs.rs/torrust-tracker-configuration)
//! for the API configuration options.
use std::sync::Arc;

use log::info;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::Configuration;
use torrust_tracker_configuration::HealthCheckApi;

use super::Started;
use crate::servers::health_check_api::server;
use crate::servers::registar::ServiceRegistry;

/// This function starts a new Health Check API server with the provided
/// configuration.
Expand All @@ -33,9 +33,8 @@ use crate::servers::health_check_api::server;
/// # Panics
///
/// It would panic if unable to send the `ApiServerJobStarted` notice.
pub async fn start_job(config: Arc<Configuration>) -> JoinHandle<()> {
pub async fn start_job(config: &HealthCheckApi, register: ServiceRegistry) -> JoinHandle<()> {
let bind_addr = config
.health_check_api
.bind_address
.parse::<std::net::SocketAddr>()
.expect("it should have a valid health check bind address");
Expand All @@ -46,7 +45,7 @@ pub async fn start_job(config: Arc<Configuration>) -> JoinHandle<()> {
let join_handle = tokio::spawn(async move {
info!(target: "Health Check API", "Starting on: http://{}", bind_addr);

let handle = server::start(bind_addr, tx_start, config.clone());
let handle = server::start(bind_addr, tx_start, register);

if let Ok(()) = handle.await {
info!(target: "Health Check API", "Stopped server running on: http://{}", bind_addr);
Expand Down
22 changes: 17 additions & 5 deletions src/bootstrap/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::make_rust_tls;
use crate::core;
use crate::servers::http::server::{HttpServer, Launcher};
use crate::servers::http::Version;
use crate::servers::registar::ServiceRegistrationForm;

/// It starts a new HTTP server with the provided configuration and version.
///
Expand All @@ -32,7 +33,12 @@ use crate::servers::http::Version;
///
/// It would panic if the `config::HttpTracker` struct would contain inappropriate values.
///
pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> Option<JoinHandle<()>> {
pub async fn start_job(
config: &HttpTracker,
tracker: Arc<core::Tracker>,
form: ServiceRegistrationForm,
version: Version,
) -> Option<JoinHandle<()>> {
if config.enabled {
let socket = config
.bind_address
Expand All @@ -44,17 +50,22 @@ pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, versio
.map(|tls| tls.expect("it should have a valid http tracker tls configuration"));

match version {
Version::V1 => Some(start_v1(socket, tls, tracker.clone()).await),
Version::V1 => Some(start_v1(socket, tls, tracker.clone(), form).await),
}
} else {
info!("Note: Not loading Http Tracker Service, Not Enabled in Configuration.");
None
}
}

async fn start_v1(socket: SocketAddr, tls: Option<RustlsConfig>, tracker: Arc<core::Tracker>) -> JoinHandle<()> {
async fn start_v1(
socket: SocketAddr,
tls: Option<RustlsConfig>,
tracker: Arc<core::Tracker>,
form: ServiceRegistrationForm,
) -> JoinHandle<()> {
let server = HttpServer::new(Launcher::new(socket, tls))
.start(tracker)
.start(tracker, form)
.await
.expect("it should be able to start to the http tracker");

Expand All @@ -80,6 +91,7 @@ mod tests {
use crate::bootstrap::app::initialize_with_configuration;
use crate::bootstrap::jobs::http_tracker::start_job;
use crate::servers::http::Version;
use crate::servers::registar::Registar;

#[tokio::test]
async fn it_should_start_http_tracker() {
Expand All @@ -88,7 +100,7 @@ mod tests {
let tracker = initialize_with_configuration(&cfg);
let version = Version::V1;

start_job(config, tracker, version)
start_job(config, tracker, Registar::default().give_form(), version)
.await
.expect("it should be able to join to the http tracker start-job");
}
Expand Down
16 changes: 12 additions & 4 deletions src/bootstrap/jobs/tracker_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use super::make_rust_tls;
use crate::core;
use crate::servers::apis::server::{ApiServer, Launcher};
use crate::servers::apis::Version;
use crate::servers::registar::ServiceRegistrationForm;

/// This is the message that the "launcher" spawned task sends to the main
/// application process to notify the API server was successfully started.
Expand All @@ -53,7 +54,12 @@ pub struct ApiServerJobStarted();
/// It would panic if unable to send the `ApiServerJobStarted` notice.
///
///
pub async fn start_job(config: &HttpApi, tracker: Arc<core::Tracker>, version: Version) -> Option<JoinHandle<()>> {
pub async fn start_job(
config: &HttpApi,
tracker: Arc<core::Tracker>,
form: ServiceRegistrationForm,
version: Version,
) -> Option<JoinHandle<()>> {
if config.enabled {
let bind_to = config
.bind_address
Expand All @@ -67,7 +73,7 @@ pub async fn start_job(config: &HttpApi, tracker: Arc<core::Tracker>, version: V
let access_tokens = Arc::new(config.access_tokens.clone());

match version {
Version::V1 => Some(start_v1(bind_to, tls, tracker.clone(), access_tokens).await),
Version::V1 => Some(start_v1(bind_to, tls, tracker.clone(), form, access_tokens).await),
}
} else {
info!("Note: Not loading Http Tracker Service, Not Enabled in Configuration.");
Expand All @@ -79,10 +85,11 @@ async fn start_v1(
socket: SocketAddr,
tls: Option<RustlsConfig>,
tracker: Arc<core::Tracker>,
form: ServiceRegistrationForm,
access_tokens: Arc<AccessTokens>,
) -> JoinHandle<()> {
let server = ApiServer::new(Launcher::new(socket, tls))
.start(tracker, access_tokens)
.start(tracker, form, access_tokens)
.await
.expect("it should be able to start to the tracker api");

Expand All @@ -101,6 +108,7 @@ mod tests {
use crate::bootstrap::app::initialize_with_configuration;
use crate::bootstrap::jobs::tracker_apis::start_job;
use crate::servers::apis::Version;
use crate::servers::registar::Registar;

#[tokio::test]
async fn it_should_start_http_tracker() {
Expand All @@ -109,7 +117,7 @@ mod tests {
let tracker = initialize_with_configuration(&cfg);
let version = Version::V1;

start_job(config, tracker, version)
start_job(config, tracker, Registar::default().give_form(), version)
.await
.expect("it should be able to join to the tracker api start-job");
}
Expand Down
5 changes: 3 additions & 2 deletions src/bootstrap/jobs/udp_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tokio::task::JoinHandle;
use torrust_tracker_configuration::UdpTracker;

use crate::core;
use crate::servers::registar::ServiceRegistrationForm;
use crate::servers::udp::server::{Launcher, UdpServer};

/// It starts a new UDP server with the provided configuration.
Expand All @@ -25,14 +26,14 @@ use crate::servers::udp::server::{Launcher, UdpServer};
/// It will panic if it is unable to start the UDP service.
/// It will panic if the task did not finish successfully.
#[must_use]
pub async fn start_job(config: &UdpTracker, tracker: Arc<core::Tracker>) -> JoinHandle<()> {
pub async fn start_job(config: &UdpTracker, tracker: Arc<core::Tracker>, form: ServiceRegistrationForm) -> JoinHandle<()> {
let bind_to = config
.bind_address
.parse::<std::net::SocketAddr>()
.expect("it should have a valid udp tracker bind address");

let server = UdpServer::new(Launcher::new(bind_to))
.start(tracker)
.start(tracker, form)
.await
.expect("it should be able to start the udp tracker");

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use torrust_tracker::{app, bootstrap};
async fn main() {
let (config, tracker) = bootstrap::app::setup();

let jobs = app::start(config.into(), tracker.clone()).await;
let jobs = app::start(&config, tracker).await;

// handle the signals
tokio::select! {
Expand Down
Loading

0 comments on commit b310c75

Please sign in to comment.