forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.rs
108 lines (88 loc) · 3.12 KB
/
service.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use colored::Colorize;
use reqwest::{Client, Url};
use super::config::Configuration;
use super::console::Console;
use crate::checker::printer::Printer;
pub struct Service {
pub(crate) config: Arc<Configuration>,
pub(crate) console: Console,
}
pub type CheckResult = Result<(), CheckError>;
#[derive(Debug)]
pub enum CheckError {
UdpError,
HttpError,
HealthCheckError { url: Url },
}
impl Service {
/// # Errors
///
/// Will return OK is all checks pass or an array with the check errors.
pub async fn run_checks(&self) -> Vec<CheckResult> {
self.console.println("Running checks for trackers ...");
self.check_udp_trackers();
self.check_http_trackers();
self.run_health_checks().await
}
fn check_udp_trackers(&self) {
self.console.println("UDP trackers ...");
for udp_tracker in &self.config.udp_trackers {
self.check_udp_tracker(udp_tracker);
}
}
fn check_http_trackers(&self) {
self.console.println("HTTP trackers ...");
for http_tracker in &self.config.http_trackers {
self.check_http_tracker(http_tracker);
}
}
async fn run_health_checks(&self) -> Vec<CheckResult> {
self.console.println("Health checks ...");
let mut check_results = vec![];
for health_check_url in &self.config.health_checks {
match self.run_health_check(health_check_url.clone()).await {
Ok(()) => check_results.push(Ok(())),
Err(err) => check_results.push(Err(err)),
}
}
check_results
}
fn check_udp_tracker(&self, address: &SocketAddr) {
// todo:
// - Make announce request
// - Make scrape request
self.console
.println(&format!("{} - UDP tracker at {:?} is OK (TODO)", "✓".green(), address));
}
fn check_http_tracker(&self, url: &Url) {
// todo:
// - Make announce request
// - Make scrape request
self.console
.println(&format!("{} - HTTP tracker at {} is OK (TODO)", "✓".green(), url));
}
async fn run_health_check(&self, url: Url) -> Result<(), CheckError> {
let client = Client::builder().timeout(Duration::from_secs(5)).build().unwrap();
match client.get(url.clone()).send().await {
Ok(response) => {
if response.status().is_success() {
self.console
.println(&format!("{} - Health API at {} is OK", "✓".green(), url));
Ok(())
} else {
self.console
.eprintln(&format!("{} - Health API at {} failing: {:?}", "✗".red(), url, response));
Err(CheckError::HealthCheckError { url })
}
}
Err(err) => {
self.console
.eprintln(&format!("{} - Health API at {} failing: {:?}", "✗".red(), url, err));
Err(CheckError::HealthCheckError { url })
}
}
}
}