Skip to content

Commit 5e0a686

Browse files
committed
refactor: [torrust#508] extract health check methods
1 parent bf23479 commit 5e0a686

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

src/servers/health_check_api/handlers.rs

+44-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Arc;
44
use aquatic_udp_protocol::{ConnectRequest, Response, TransactionId};
55
use axum::extract::State;
66
use axum::Json;
7-
use torrust_tracker_configuration::Configuration;
7+
use torrust_tracker_configuration::{Configuration, HttpApi, HttpTracker, UdpTracker};
88

99
use super::resources::Report;
1010
use super::responses;
@@ -21,27 +21,49 @@ const UNKNOWN_PORT: u16 = 0;
2121
/// configuration. If port 0 is specified in the configuration the health check
2222
/// for that service is skipped.
2323
pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>>) -> Json<Report> {
24+
if let Some(err_response) = api_health_check(&config.http_api).await {
25+
return err_response;
26+
}
27+
28+
if let Some(err_response) = http_trackers_health_check(&config.http_trackers).await {
29+
return err_response;
30+
}
31+
32+
if let Some(err_response) = udp_trackers_health_check(&config.udp_trackers).await {
33+
return err_response;
34+
}
35+
36+
responses::ok()
37+
}
38+
39+
async fn api_health_check(config: &HttpApi) -> Option<Json<Report>> {
2440
// todo: when port 0 is specified in the configuration get the port from the
2541
// running service, after starting it as we do for testing with ephemeral
2642
// configurations.
2743

28-
// Health check for API
29-
30-
if config.http_api.enabled {
31-
let addr: SocketAddr = config.http_api.bind_address.parse().expect("invalid socket address for API");
44+
if config.enabled {
45+
let addr: SocketAddr = config.bind_address.parse().expect("invalid socket address for API");
3246

3347
if addr.port() != UNKNOWN_PORT {
3448
let health_check_url = format!("http://{addr}/health_check");
3549

3650
if !get_req_is_ok(&health_check_url).await {
37-
return responses::error(format!("API is not healthy. Health check endpoint: {health_check_url}"));
51+
return Some(responses::error(format!(
52+
"API is not healthy. Health check endpoint: {health_check_url}"
53+
)));
3854
}
3955
}
4056
}
4157

42-
// Health check for HTTP Trackers
58+
None
59+
}
4360

44-
for http_tracker_config in &config.http_trackers {
61+
async fn http_trackers_health_check(http_trackers: &Vec<HttpTracker>) -> Option<Json<Report>> {
62+
// todo: when port 0 is specified in the configuration get the port from the
63+
// running service, after starting it as we do for testing with ephemeral
64+
// configurations.
65+
66+
for http_tracker_config in http_trackers {
4567
if !http_tracker_config.enabled {
4668
continue;
4769
}
@@ -55,16 +77,22 @@ pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>
5577
let health_check_url = format!("http://{addr}/health_check");
5678

5779
if !get_req_is_ok(&health_check_url).await {
58-
return responses::error(format!(
80+
return Some(responses::error(format!(
5981
"HTTP Tracker is not healthy. Health check endpoint: {health_check_url}"
60-
));
82+
)));
6183
}
6284
}
6385
}
6486

65-
// Health check for UDP Trackers
87+
None
88+
}
89+
90+
async fn udp_trackers_health_check(udp_trackers: &Vec<UdpTracker>) -> Option<Json<Report>> {
91+
// todo: when port 0 is specified in the configuration get the port from the
92+
// running service, after starting it as we do for testing with ephemeral
93+
// configurations.
6694

67-
for udp_tracker_config in &config.udp_trackers {
95+
for udp_tracker_config in udp_trackers {
6896
if !udp_tracker_config.enabled {
6997
continue;
7098
}
@@ -75,11 +103,13 @@ pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>
75103
.expect("invalid socket address for UDP tracker");
76104

77105
if addr.port() != UNKNOWN_PORT && !can_connect_to_udp_tracker(&addr.to_string()).await {
78-
return responses::error(format!("UDP Tracker is not healthy. Can't connect to: {addr}"));
106+
return Some(responses::error(format!(
107+
"UDP Tracker is not healthy. Can't connect to: {addr}"
108+
)));
79109
}
80110
}
81111

82-
responses::ok()
112+
None
83113
}
84114

85115
async fn get_req_is_ok(url: &str) -> bool {

0 commit comments

Comments
 (0)