Skip to content

Commit 9e74449

Browse files
committed
Merge torrust#1423: Refactor: extract type ServiceBinding
29fa324 refactor: [torrust#1422] extract type ServiceBinding (Jose Celano) 376c1df fix: missing feature in package cargo config (Jose Celano) Pull request description: Refactor: extract type `ServiceBinding`. It's the protocol + socket binding address for a service. - Protocols are only: udp, http and https (protocols used by the tracker now). - The port must be greater than zero (already assigned). - Any protocol and port combination is possible. For example: http protocol with a port different than 80. ACKs for top commit: josecelano: ACK 29fa324 Tree-SHA512: 82cb0ff34998b8d14d8e80f48fa9c7173fbc1313b9fe7a87edee85ff74202a0ece8ccb63bd85080b5b4d450fdcf584f8d77761608611affb1e590490f892effa
2 parents 7fef3e9 + 29fa324 commit 9e74449

File tree

17 files changed

+275
-56
lines changed

17 files changed

+275
-56
lines changed

Cargo.lock

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/axum-health-check-api-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal
2424
torrust-axum-server = { version = "3.0.0-develop", path = "../axum-server" }
2525
torrust-server-lib = { version = "3.0.0-develop", path = "../server-lib" }
2626
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
27+
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
2728
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
2829
tracing = "0"
2930
url = "2.5.4"

packages/axum-health-check-api-server/src/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub(crate) async fn health_check_handler(State(register): State<ServiceRegistry>
3131
let jobs = checks.drain(..).map(|c| {
3232
tokio::spawn(async move {
3333
CheckReport {
34-
listen_url: c.listen_url.clone(),
35-
binding: c.binding,
34+
service_binding: c.service_binding.url(),
35+
binding: c.service_binding.bind_address(),
3636
info: c.info.clone(),
3737
service_type: c.service_type,
3838
result: c.job.await.expect("it should be able to join into the checking function"),

packages/axum-health-check-api-server/src/resources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum Status {
1212

1313
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
1414
pub struct CheckReport {
15-
pub listen_url: Url,
15+
pub service_binding: Url,
1616
pub binding: SocketAddr,
1717
pub service_type: String,
1818
pub info: String,

packages/axum-health-check-api-server/src/server.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ use torrust_axum_server::signals::graceful_shutdown;
1818
use torrust_server_lib::logging::Latency;
1919
use torrust_server_lib::registar::ServiceRegistry;
2020
use torrust_server_lib::signals::{Halted, Started};
21+
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
2122
use tower_http::classify::ServerErrorsFailureClass;
2223
use tower_http::compression::CompressionLayer;
2324
use tower_http::propagate_header::PropagateHeaderLayer;
2425
use tower_http::request_id::{MakeRequestUuid, SetRequestIdLayer};
2526
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
2627
use tower_http::LatencyUnit;
2728
use tracing::{instrument, Level, Span};
28-
use url::Url;
2929

3030
use crate::handlers::health_check_handler;
3131
use crate::HEALTH_CHECK_API_LOG_TARGET;
@@ -102,9 +102,8 @@ pub fn start(
102102

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

109108
let handle = Handle::new();
110109

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

123-
tx.send(Started { listen_url, address })
124-
.expect("the Health Check API server should not be dropped");
122+
tx.send(Started {
123+
service_binding,
124+
address,
125+
})
126+
.expect("the Health Check API server should not be dropped");
125127

126128
running
127129
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use axum_server::Handle;
77
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
88
use derive_more::Constructor;
99
use futures::future::BoxFuture;
10-
use reqwest::Url;
1110
use tokio::sync::oneshot::{Receiver, Sender};
1211
use torrust_axum_server::custom_axum_server::{self, TimeoutAcceptor};
1312
use torrust_axum_server::signals::graceful_shutdown;
1413
use torrust_server_lib::logging::STARTED_ON;
1514
use torrust_server_lib::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
1615
use torrust_server_lib::signals::{Halted, Started};
16+
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
1717
use tracing::instrument;
1818

1919
use super::v1::routes::router;
@@ -63,9 +63,8 @@ impl Launcher {
6363
));
6464

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

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

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

9594
tx_start
96-
.send(Started { listen_url, address })
95+
.send(Started {
96+
service_binding,
97+
address,
98+
})
9799
.expect("the HTTP(s) Tracker service should not be dropped");
98100

99101
running
@@ -182,10 +184,10 @@ impl HttpServer<Stopped> {
182184

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

185-
let listen_url = started.listen_url;
187+
let listen_url = started.service_binding;
186188
let binding = started.address;
187189

188-
form.send(ServiceRegistration::new(listen_url, binding, check_fn))
190+
form.send(ServiceRegistration::new(listen_url, check_fn))
189191
.expect("it should be able to send service registration");
190192

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

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

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

241-
ServiceHealthCheckJob::new(listen_url.clone(), *binding, info, TYPE_STRING.to_string(), job)
243+
ServiceHealthCheckJob::new(service_binding.clone(), info, TYPE_STRING.to_string(), job)
242244
}
243245

244246
#[cfg(test)]

packages/axum-rest-tracker-api-server/src/server.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ use torrust_server_lib::logging::STARTED_ON;
4040
use torrust_server_lib::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
4141
use torrust_server_lib::signals::{Halted, Started};
4242
use torrust_tracker_configuration::AccessTokens;
43+
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
4344
use tracing::{instrument, Level};
44-
use url::Url;
4545

4646
use super::routes::router;
4747
use crate::API_LOG_TARGET;
@@ -149,7 +149,7 @@ impl ApiServer<Stopped> {
149149

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

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

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

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

213213
/// A struct responsible for starting the API server.
@@ -260,9 +260,8 @@ impl Launcher {
260260
));
261261

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

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

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

290289
tx_start
291-
.send(Started { listen_url, address })
290+
.send(Started {
291+
service_binding,
292+
address,
293+
})
292294
.expect("the HTTP(s) Tracker API service should not be dropped");
293295

294296
running

packages/primitives/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ tdyne-peer-id = "1"
2424
tdyne-peer-id-registry = "0"
2525
thiserror = "2"
2626
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
27+
url = "2.5.4"
2728
zerocopy = "0.7"
29+
30+
[dev-dependencies]
31+
rstest = "0.25.0"

packages/primitives/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub mod core;
88
pub mod pagination;
99
pub mod peer;
10+
pub mod service_binding;
1011
pub mod swarm_metadata;
1112

1213
use std::collections::BTreeMap;

0 commit comments

Comments
 (0)