|
| 1 | +use reqwest::Url; |
| 2 | +use serde_json::json; |
| 3 | +use tracing::{debug, error}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + config::Configuration, |
| 7 | + monitor::{logs::ProxyLog, shares::ShareInfo}, |
| 8 | + shared::error::Error, |
| 9 | + LOCAL_URL, PRODUCTION_URL, STAGING_URL, TESTNET3_URL, |
| 10 | +}; |
| 11 | + |
| 12 | +pub mod logs; |
| 13 | +pub mod shares; |
| 14 | + |
| 15 | +pub struct MonitorAPI { |
| 16 | + pub url: Url, |
| 17 | + pub client: reqwest::Client, |
| 18 | +} |
| 19 | +fn proxy_log_server_endpoint() -> String { |
| 20 | + match Configuration::environment().as_str() { |
| 21 | + "staging" => format!("{}/api/proxy/logs", STAGING_URL), |
| 22 | + "testnet3" => format!("{}/api/proxy/logs", TESTNET3_URL), |
| 23 | + "local" => format!("{}/api/proxy/logs", LOCAL_URL), |
| 24 | + "production" => format!("{}/api/proxy/logs", PRODUCTION_URL), |
| 25 | + _ => unreachable!(), |
| 26 | + } |
| 27 | +} |
| 28 | +fn shares_server_endpoint() -> String { |
| 29 | + // Determine the monitoring server URL based on the environment |
| 30 | + match Configuration::environment().as_str() { |
| 31 | + "staging" => format!("{}/api/share/save", STAGING_URL), |
| 32 | + "testnet3" => format!("{}/api/share/save", TESTNET3_URL), |
| 33 | + "local" => format!("{}/api/share/save", LOCAL_URL), |
| 34 | + "production" => format!("{}/api/share/save", PRODUCTION_URL), |
| 35 | + _ => unreachable!(), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl MonitorAPI { |
| 40 | + pub fn new(url: String) -> Self { |
| 41 | + let client = reqwest::Client::new(); |
| 42 | + MonitorAPI { |
| 43 | + url: url.parse().expect("Invalid URL"), |
| 44 | + client, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Sends a batch of shares to the monitoring server. |
| 49 | + async fn send_shares(&self, shares: Vec<ShareInfo>) -> Result<(), Error> { |
| 50 | + let token = crate::config::Configuration::token().expect("Token is not set"); |
| 51 | + |
| 52 | + debug!("Sending batch of {} shares to API", shares.len()); |
| 53 | + let response = self |
| 54 | + .client |
| 55 | + .post(self.url.clone()) |
| 56 | + .json(&json!({ "shares": shares, "token": token })) |
| 57 | + .send() |
| 58 | + .await?; |
| 59 | + |
| 60 | + match response.error_for_status() { |
| 61 | + Ok(_) => Ok(()), |
| 62 | + Err(err) => { |
| 63 | + error!("Failed to send shares: {}", err); |
| 64 | + Err(err.into()) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Sends a log to the monitoring server. |
| 70 | + pub async fn send_log(&self, log: ProxyLog) -> Result<(), Error> { |
| 71 | + let token = crate::config::Configuration::token().expect("Token is not set"); |
| 72 | + |
| 73 | + debug!("Sending log to API: {:?}", log); |
| 74 | + let response = self |
| 75 | + .client |
| 76 | + .post(self.url.clone()) |
| 77 | + .json(&json!({ "log": log, "token": token })) |
| 78 | + .send() |
| 79 | + .await?; |
| 80 | + |
| 81 | + match response.error_for_status() { |
| 82 | + Ok(_) => Ok(()), |
| 83 | + Err(err) => { |
| 84 | + error!("Failed to send log: {}", err); |
| 85 | + Err(err.into()) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments