Skip to content

Commit 7fef3e9

Browse files
committed
Merge torrust#1416: Feature: Add listen_url field to the health check API
abfb733 feat: [torrust#1409] add listen_url field to the health check API (Jose Celano) Pull request description: Added an extra `listen_url` field to th health check API endpoint: http://127.0.0.1:1313/health_check ```json { "status": "Ok", "message": "", "details": [ { "listen_url": "http://0.0.0.0:7070/", "binding": "0.0.0.0:7070", "service_type": "http_tracker", "info": "checking http tracker health check at: http://0.0.0.0:7070/health_check", "result": { "Ok": "200 OK" } } ] } ``` It includes the protocol + binding address. The `binding` is not removed for back compatibility. **NOTICE:** HTTP URLs contain the trailing slash `"listen_url": "http://0.0.0.0:1212/"` because it's the default behavior. ACKs for top commit: josecelano: ACK abfb733 Tree-SHA512: 4a063580890add1a06d99d4e41717b01577423a68ac4b492c394b352575b4ed30f93d99c35b9654ac8ba28bd117a8405b6e7dfe1fa5a98e0d93b71c8afea95e9
2 parents 9a10e3e + abfb733 commit 7fef3e9

File tree

15 files changed

+56
-25
lines changed

15 files changed

+56
-25
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
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
@@ -26,6 +26,7 @@ torrust-server-lib = { version = "3.0.0-develop", path = "../server-lib" }
2626
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
2727
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
2828
tracing = "0"
29+
url = "2.5.4"
2930

3031
[dev-dependencies]
3132
reqwest = { version = "0", features = ["json"] }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ 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(),
3435
binding: c.binding,
3536
info: c.info.clone(),
3637
service_type: c.service_type,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::net::SocketAddr;
22

33
use serde::{Deserialize, Serialize};
4+
use url::Url;
45

56
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
67
pub enum Status {
@@ -11,6 +12,7 @@ pub enum Status {
1112

1213
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
1314
pub struct CheckReport {
15+
pub listen_url: Url,
1416
pub binding: SocketAddr,
1517
pub service_type: String,
1618
pub info: String,

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use tower_http::request_id::{MakeRequestUuid, SetRequestIdLayer};
2525
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
2626
use tower_http::LatencyUnit;
2727
use tracing::{instrument, Level, Span};
28+
use url::Url;
2829

2930
use crate::handlers::health_check_handler;
3031
use crate::HEALTH_CHECK_API_LOG_TARGET;
@@ -101,6 +102,9 @@ pub fn start(
101102

102103
let socket = std::net::TcpListener::bind(bind_to).expect("Could not bind tcp_listener to address.");
103104
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.");
104108

105109
let handle = Handle::new();
106110

@@ -116,7 +120,7 @@ pub fn start(
116120
.handle(handle)
117121
.serve(router.into_make_service_with_connect_info::<SocketAddr>());
118122

119-
tx.send(Started { address })
123+
tx.send(Started { listen_url, address })
120124
.expect("the Health Check API server should not be dropped");
121125

122126
running

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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;
1011
use tokio::sync::oneshot::{Receiver, Sender};
1112
use torrust_axum_server::custom_axum_server::{self, TimeoutAcceptor};
1213
use torrust_axum_server::signals::graceful_shutdown;
@@ -63,8 +64,10 @@ impl Launcher {
6364

6465
let tls = self.tls.clone();
6566
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.");
6669

67-
tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "Starting on: {protocol}://{}", address);
70+
tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "Starting on: {protocol}://{address}");
6871

6972
let app = router(http_tracker_container, address);
7073

@@ -90,7 +93,7 @@ impl Launcher {
9093
tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "{STARTED_ON}: {protocol}://{}", address);
9194

9295
tx_start
93-
.send(Started { address })
96+
.send(Started { listen_url, address })
9497
.expect("the HTTP(s) Tracker service should not be dropped");
9598

9699
running
@@ -177,9 +180,12 @@ impl HttpServer<Stopped> {
177180
launcher
178181
});
179182

180-
let binding = rx_start.await.expect("it should be able to start the service").address;
183+
let started = rx_start.await.expect("it should be able to start the service");
181184

182-
form.send(ServiceRegistration::new(binding, check_fn))
185+
let listen_url = started.listen_url;
186+
let binding = started.address;
187+
188+
form.send(ServiceRegistration::new(listen_url, binding, check_fn))
183189
.expect("it should be able to send service registration");
184190

185191
Ok(HttpServer {
@@ -220,7 +226,7 @@ impl HttpServer<Running> {
220226
/// This function will return an error if unable to connect.
221227
/// Or if the request returns an error.
222228
#[must_use]
223-
pub fn check_fn(binding: &SocketAddr) -> ServiceHealthCheckJob {
229+
pub fn check_fn(listen_url: &Url, binding: &SocketAddr) -> ServiceHealthCheckJob {
224230
let url = format!("http://{binding}/health_check"); // DevSkim: ignore DS137138
225231

226232
let info = format!("checking http tracker health check at: {url}");
@@ -232,7 +238,7 @@ pub fn check_fn(binding: &SocketAddr) -> ServiceHealthCheckJob {
232238
}
233239
});
234240

235-
ServiceHealthCheckJob::new(*binding, info, TYPE_STRING.to_string(), job)
241+
ServiceHealthCheckJob::new(listen_url.clone(), *binding, info, TYPE_STRING.to_string(), job)
236242
}
237243

238244
#[cfg(test)]

packages/axum-rest-tracker-api-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ torrust-udp-tracker-server = { version = "3.0.0-develop", path = "../udp-tracker
4242
tower = { version = "0", features = ["timeout"] }
4343
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
4444
tracing = "0"
45+
url = "2"
4546

4647
[dev-dependencies]
4748
local-ip-address = "0"

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use torrust_server_lib::registar::{ServiceHealthCheckJob, ServiceRegistration, S
4141
use torrust_server_lib::signals::{Halted, Started};
4242
use torrust_tracker_configuration::AccessTokens;
4343
use tracing::{instrument, Level};
44+
use url::Url;
4445

4546
use super::routes::router;
4647
use crate::API_LOG_TARGET;
@@ -148,7 +149,7 @@ impl ApiServer<Stopped> {
148149

149150
let api_server = match rx_start.await {
150151
Ok(started) => {
151-
form.send(ServiceRegistration::new(started.address, check_fn))
152+
form.send(ServiceRegistration::new(started.listen_url, started.address, check_fn))
152153
.expect("it should be able to send service registration");
153154

154155
ApiServer {
@@ -195,7 +196,7 @@ impl ApiServer<Running> {
195196
/// Or if there request returns an error code.
196197
#[must_use]
197198
#[instrument(skip())]
198-
pub fn check_fn(binding: &SocketAddr) -> ServiceHealthCheckJob {
199+
pub fn check_fn(listen_url: &Url, binding: &SocketAddr) -> ServiceHealthCheckJob {
199200
let url = format!("http://{binding}/api/health_check"); // DevSkim: ignore DS137138
200201

201202
let info = format!("checking api health check at: {url}");
@@ -206,7 +207,7 @@ pub fn check_fn(binding: &SocketAddr) -> ServiceHealthCheckJob {
206207
Err(err) => Err(err.to_string()),
207208
}
208209
});
209-
ServiceHealthCheckJob::new(*binding, info, TYPE_STRING.to_string(), job)
210+
ServiceHealthCheckJob::new(listen_url.clone(), *binding, info, TYPE_STRING.to_string(), job)
210211
}
211212

212213
/// A struct responsible for starting the API server.
@@ -260,8 +261,10 @@ impl Launcher {
260261

261262
let tls = self.tls.clone();
262263
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.");
263266

264-
tracing::info!(target: API_LOG_TARGET, "Starting on {protocol}://{}", address);
267+
tracing::info!(target: API_LOG_TARGET, "Starting on: {protocol}://{address}");
265268

266269
let running = Box::pin(async {
267270
match tls {
@@ -282,10 +285,10 @@ impl Launcher {
282285
}
283286
});
284287

285-
tracing::info!(target: API_LOG_TARGET, "{STARTED_ON} {protocol}://{}", address);
288+
tracing::info!(target: API_LOG_TARGET, "{STARTED_ON}: {protocol}://{}", address);
286289

287290
tx_start
288-
.send(Started { address })
291+
.send(Started { listen_url, address })
289292
.expect("the HTTP(s) Tracker API service should not be dropped");
290293

291294
running

packages/server-lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ derive_more = { version = "2", features = ["as_ref", "constructor", "from"] }
1818
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
1919
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
2020
tracing = "0"
21+
url = "2.5.4"
2122

2223
[dev-dependencies]

packages/server-lib/src/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use tower_http::LatencyUnit;
1010
/// ```text
1111
/// 2024-06-25T12:36:25.025312Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6969
1212
/// 2024-06-25T12:36:25.025445Z INFO HTTP TRACKER: Started on: http://0.0.0.0:7070
13-
/// 2024-06-25T12:36:25.025527Z INFO API: Started on http://0.0.0.0:1212
13+
/// 2024-06-25T12:36:25.025527Z INFO API: Started on: http://0.0.0.0:1212
1414
/// 2024-06-25T12:36:25.025580Z INFO HEALTH CHECK API: Started on: http://127.0.0.1:1313
1515
/// ```
1616
pub const STARTED_ON: &str = "Started on";

0 commit comments

Comments
 (0)