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
6 changes: 5 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/axum-health-check-api-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal
torrust-axum-server = { version = "3.0.0-develop", path = "../axum-server" }
torrust-server-lib = { version = "3.0.0-develop", path = "../server-lib" }
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
tracing = "0"
url = "2.5.4"
Expand Down
4 changes: 2 additions & 2 deletions packages/axum-health-check-api-server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub(crate) async fn health_check_handler(State(register): State<ServiceRegistry>
let jobs = checks.drain(..).map(|c| {
tokio::spawn(async move {
CheckReport {
listen_url: c.listen_url.clone(),
binding: c.binding,
service_binding: c.service_binding.url(),
binding: c.service_binding.bind_address(),
info: c.info.clone(),
service_type: c.service_type,
result: c.job.await.expect("it should be able to join into the checking function"),
Expand Down
2 changes: 1 addition & 1 deletion packages/axum-health-check-api-server/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum Status {

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct CheckReport {
pub listen_url: Url,
pub service_binding: Url,
pub binding: SocketAddr,
pub service_type: String,
pub info: String,
Expand Down
14 changes: 8 additions & 6 deletions packages/axum-health-check-api-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ use torrust_axum_server::signals::graceful_shutdown;
use torrust_server_lib::logging::Latency;
use torrust_server_lib::registar::ServiceRegistry;
use torrust_server_lib::signals::{Halted, Started};
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
use tower_http::classify::ServerErrorsFailureClass;
use tower_http::compression::CompressionLayer;
use tower_http::propagate_header::PropagateHeaderLayer;
use tower_http::request_id::{MakeRequestUuid, SetRequestIdLayer};
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
use tower_http::LatencyUnit;
use tracing::{instrument, Level, Span};
use url::Url;

use crate::handlers::health_check_handler;
use crate::HEALTH_CHECK_API_LOG_TARGET;
Expand Down Expand Up @@ -102,9 +102,8 @@ pub fn start(

let socket = std::net::TcpListener::bind(bind_to).expect("Could not bind tcp_listener to address.");
let address = socket.local_addr().expect("Could not get local_addr from tcp_listener.");
let protocol = "http"; // The health check API only supports HTTP directly now. Use a reverse proxy for HTTPS.
let listen_url =
Url::parse(&format!("{protocol}://{address}")).expect("Could not parse internal service url for health check API.");
let protocol = Protocol::HTTP; // The health check API only supports HTTP directly now. Use a reverse proxy for HTTPS.
let service_binding = ServiceBinding::new(protocol.clone(), address).expect("Service binding creation failed");

let handle = Handle::new();

Expand All @@ -120,8 +119,11 @@ pub fn start(
.handle(handle)
.serve(router.into_make_service_with_connect_info::<SocketAddr>());

tx.send(Started { listen_url, address })
.expect("the Health Check API server should not be dropped");
tx.send(Started {
service_binding,
address,
})
.expect("the Health Check API server should not be dropped");

running
}
22 changes: 12 additions & 10 deletions packages/axum-http-tracker-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use axum_server::Handle;
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
use derive_more::Constructor;
use futures::future::BoxFuture;
use reqwest::Url;
use tokio::sync::oneshot::{Receiver, Sender};
use torrust_axum_server::custom_axum_server::{self, TimeoutAcceptor};
use torrust_axum_server::signals::graceful_shutdown;
use torrust_server_lib::logging::STARTED_ON;
use torrust_server_lib::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
use torrust_server_lib::signals::{Halted, Started};
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
use tracing::instrument;

use super::v1::routes::router;
Expand Down Expand Up @@ -63,9 +63,8 @@ impl Launcher {
));

let tls = self.tls.clone();
let protocol = if tls.is_some() { "https" } else { "http" };
let listen_url =
Url::parse(&format!("{protocol}://{address}")).expect("Could not parse internal service url for HTTP tracker.");
let protocol = if tls.is_some() { Protocol::HTTPS } else { Protocol::HTTP };
let service_binding = ServiceBinding::new(protocol.clone(), address).expect("Service binding creation failed");

tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "Starting on: {protocol}://{address}");

Expand Down Expand Up @@ -93,7 +92,10 @@ impl Launcher {
tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "{STARTED_ON}: {protocol}://{}", address);

tx_start
.send(Started { listen_url, address })
.send(Started {
service_binding,
address,
})
.expect("the HTTP(s) Tracker service should not be dropped");

running
Expand Down Expand Up @@ -182,10 +184,10 @@ impl HttpServer<Stopped> {

let started = rx_start.await.expect("it should be able to start the service");

let listen_url = started.listen_url;
let listen_url = started.service_binding;
let binding = started.address;

form.send(ServiceRegistration::new(listen_url, binding, check_fn))
form.send(ServiceRegistration::new(listen_url, check_fn))
.expect("it should be able to send service registration");

Ok(HttpServer {
Expand Down Expand Up @@ -226,8 +228,8 @@ impl HttpServer<Running> {
/// This function will return an error if unable to connect.
/// Or if the request returns an error.
#[must_use]
pub fn check_fn(listen_url: &Url, binding: &SocketAddr) -> ServiceHealthCheckJob {
let url = format!("http://{binding}/health_check"); // DevSkim: ignore DS137138
pub fn check_fn(service_binding: &ServiceBinding) -> ServiceHealthCheckJob {
let url = format!("http://{}/health_check", service_binding.bind_address()); // DevSkim: ignore DS137138

let info = format!("checking http tracker health check at: {url}");

Expand All @@ -238,7 +240,7 @@ pub fn check_fn(listen_url: &Url, binding: &SocketAddr) -> ServiceHealthCheckJob
}
});

ServiceHealthCheckJob::new(listen_url.clone(), *binding, info, TYPE_STRING.to_string(), job)
ServiceHealthCheckJob::new(service_binding.clone(), info, TYPE_STRING.to_string(), job)
}

#[cfg(test)]
Expand Down
20 changes: 11 additions & 9 deletions packages/axum-rest-tracker-api-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ use torrust_server_lib::logging::STARTED_ON;
use torrust_server_lib::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
use torrust_server_lib::signals::{Halted, Started};
use torrust_tracker_configuration::AccessTokens;
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
use tracing::{instrument, Level};
use url::Url;

use super::routes::router;
use crate::API_LOG_TARGET;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl ApiServer<Stopped> {

let api_server = match rx_start.await {
Ok(started) => {
form.send(ServiceRegistration::new(started.listen_url, started.address, check_fn))
form.send(ServiceRegistration::new(started.service_binding, check_fn))
.expect("it should be able to send service registration");

ApiServer {
Expand Down Expand Up @@ -196,8 +196,8 @@ impl ApiServer<Running> {
/// Or if there request returns an error code.
#[must_use]
#[instrument(skip())]
pub fn check_fn(listen_url: &Url, binding: &SocketAddr) -> ServiceHealthCheckJob {
let url = format!("http://{binding}/api/health_check"); // DevSkim: ignore DS137138
pub fn check_fn(service_binding: &ServiceBinding) -> ServiceHealthCheckJob {
let url = format!("http://{}/api/health_check", service_binding.bind_address()); // DevSkim: ignore DS137138

let info = format!("checking api health check at: {url}");

Expand All @@ -207,7 +207,7 @@ pub fn check_fn(listen_url: &Url, binding: &SocketAddr) -> ServiceHealthCheckJob
Err(err) => Err(err.to_string()),
}
});
ServiceHealthCheckJob::new(listen_url.clone(), *binding, info, TYPE_STRING.to_string(), job)
ServiceHealthCheckJob::new(service_binding.clone(), info, TYPE_STRING.to_string(), job)
}

/// A struct responsible for starting the API server.
Expand Down Expand Up @@ -260,9 +260,8 @@ impl Launcher {
));

let tls = self.tls.clone();
let protocol = if tls.is_some() { "https" } else { "http" };
let listen_url =
Url::parse(&format!("{protocol}://{address}")).expect("Could not parse internal service url for tracker API.");
let protocol = if tls.is_some() { Protocol::HTTPS } else { Protocol::HTTP };
let service_binding = ServiceBinding::new(protocol.clone(), address).expect("Service binding creation failed");

tracing::info!(target: API_LOG_TARGET, "Starting on: {protocol}://{address}");

Expand All @@ -288,7 +287,10 @@ impl Launcher {
tracing::info!(target: API_LOG_TARGET, "{STARTED_ON}: {protocol}://{}", address);

tx_start
.send(Started { listen_url, address })
.send(Started {
service_binding,
address,
})
.expect("the HTTP(s) Tracker API service should not be dropped");

running
Expand Down
4 changes: 4 additions & 0 deletions packages/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ tdyne-peer-id = "1"
tdyne-peer-id-registry = "0"
thiserror = "2"
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
url = "2.5.4"
zerocopy = "0.7"

[dev-dependencies]
rstest = "0.25.0"
1 change: 1 addition & 0 deletions packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub mod core;
pub mod pagination;
pub mod peer;
pub mod service_binding;
pub mod swarm_metadata;

use std::collections::BTreeMap;
Expand Down
Loading