@@ -4,7 +4,7 @@ use std::sync::Arc;
4
4
use aquatic_udp_protocol:: { ConnectRequest , Response , TransactionId } ;
5
5
use axum:: extract:: State ;
6
6
use axum:: Json ;
7
- use torrust_tracker_configuration:: Configuration ;
7
+ use torrust_tracker_configuration:: { Configuration , HttpApi , HttpTracker , UdpTracker } ;
8
8
9
9
use super :: resources:: Report ;
10
10
use super :: responses;
@@ -21,27 +21,49 @@ const UNKNOWN_PORT: u16 = 0;
21
21
/// configuration. If port 0 is specified in the configuration the health check
22
22
/// for that service is skipped.
23
23
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 > > {
24
40
// todo: when port 0 is specified in the configuration get the port from the
25
41
// running service, after starting it as we do for testing with ephemeral
26
42
// configurations.
27
43
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" ) ;
32
46
33
47
if addr. port ( ) != UNKNOWN_PORT {
34
48
let health_check_url = format ! ( "http://{addr}/health_check" ) ;
35
49
36
50
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
+ ) ) ) ;
38
54
}
39
55
}
40
56
}
41
57
42
- // Health check for HTTP Trackers
58
+ None
59
+ }
43
60
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 {
45
67
if !http_tracker_config. enabled {
46
68
continue ;
47
69
}
@@ -55,16 +77,22 @@ pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>
55
77
let health_check_url = format ! ( "http://{addr}/health_check" ) ;
56
78
57
79
if !get_req_is_ok ( & health_check_url) . await {
58
- return responses:: error ( format ! (
80
+ return Some ( responses:: error ( format ! (
59
81
"HTTP Tracker is not healthy. Health check endpoint: {health_check_url}"
60
- ) ) ;
82
+ ) ) ) ;
61
83
}
62
84
}
63
85
}
64
86
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.
66
94
67
- for udp_tracker_config in & config . udp_trackers {
95
+ for udp_tracker_config in udp_trackers {
68
96
if !udp_tracker_config. enabled {
69
97
continue ;
70
98
}
@@ -75,11 +103,13 @@ pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>
75
103
. expect ( "invalid socket address for UDP tracker" ) ;
76
104
77
105
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
+ ) ) ) ;
79
109
}
80
110
}
81
111
82
- responses :: ok ( )
112
+ None
83
113
}
84
114
85
115
async fn get_req_is_ok ( url : & str ) -> bool {
0 commit comments