feat: custom method, headers, body, timeout on monitors
This commit is contained in:
@@ -52,7 +52,31 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor) -> PingResult {
|
||||
None
|
||||
};
|
||||
|
||||
let result = client.get(&monitor.url).send().await;
|
||||
// Build request with method, headers, body, timeout
|
||||
let method = monitor.method.as_deref().unwrap_or("GET").to_uppercase();
|
||||
let timeout = std::time::Duration::from_millis(monitor.timeout_ms.unwrap_or(30000));
|
||||
|
||||
let req_method = reqwest::Method::from_bytes(method.as_bytes())
|
||||
.unwrap_or(reqwest::Method::GET);
|
||||
|
||||
let mut req = client.request(req_method, &monitor.url).timeout(timeout);
|
||||
|
||||
if let Some(headers) = &monitor.request_headers {
|
||||
for (k, v) in headers {
|
||||
if let (Ok(name), Ok(value)) = (
|
||||
reqwest::header::HeaderName::from_bytes(k.as_bytes()),
|
||||
reqwest::header::HeaderValue::from_str(v),
|
||||
) {
|
||||
req = req.header(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(body) = &monitor.request_body {
|
||||
req = req.body(body.clone());
|
||||
}
|
||||
|
||||
let result = req.send().await;
|
||||
let latency_ms = start.elapsed().as_millis() as u64;
|
||||
|
||||
match result {
|
||||
|
||||
+15
-10
@@ -1,21 +1,26 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Monitor {
|
||||
pub id: String,
|
||||
pub url: String,
|
||||
pub interval_s: i64,
|
||||
pub query: Option<Value>,
|
||||
pub id: String,
|
||||
pub url: String,
|
||||
pub method: Option<String>,
|
||||
pub request_headers: Option<HashMap<String, String>>,
|
||||
pub request_body: Option<String>,
|
||||
pub timeout_ms: Option<u64>,
|
||||
pub interval_s: i64,
|
||||
pub query: Option<Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct PingResult {
|
||||
pub monitor_id: String,
|
||||
pub status_code: Option<u16>,
|
||||
pub latency_ms: Option<u64>,
|
||||
pub up: bool,
|
||||
pub error: Option<String>,
|
||||
pub monitor_id: String,
|
||||
pub status_code: Option<u16>,
|
||||
pub latency_ms: Option<u64>,
|
||||
pub up: bool,
|
||||
pub error: Option<String>,
|
||||
pub cert_expiry_days: Option<i64>,
|
||||
pub meta: Option<Value>,
|
||||
pub meta: Option<Value>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user