added fetching id for registration response

This commit is contained in:
2025-07-29 21:29:17 +02:00
parent e663839bee
commit 43ac3c4d1a
2 changed files with 36 additions and 17 deletions

View File

@@ -11,7 +11,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
tokio = { version = "1.37", features = ["full"] } tokio = { version = "1.37", features = ["full"] }
local-ip-address = "0.5" local-ip-address = "0.5"
reqwest = { version = "0.11", default-features = false, features = ["json", "blocking", "cookies", "rustls-tls"] } reqwest = { version = "0.11", default-features = false, features = ["json", "blocking", "cookies", "rustls-tls", "statuscode"] }
sysinfo = "0.29" sysinfo = "0.29"
metrics = "0.24.2" metrics = "0.24.2"
chrono = "0.4" chrono = "0.4"

View File

@@ -147,15 +147,15 @@ impl HardwareInfo {
} }
} }
async fn register_with_server(base_url: &str) -> Result<(i32, String), Box<dyn Error>> { async fn get_server_id_by_ip(base_url: &str, ip: &str) -> Result<(i32, String), Box<dyn Error>> {
let client = Client::builder() let client = Client::builder()
.danger_accept_invalid_certs(true) .danger_accept_invalid_certs(true)
.build()?; .build()?;
// First get server ID let url = format!("{}/monitoring/server-id-by-ip?ipAddress={}", base_url, ip);
let url = format!("{}/server-id", base_url);
let (id, ip_address) = loop { loop {
println!("Attempting to fetch server ID..."); println!("Attempting to fetch server ID for IP {}...", ip);
match client.get(&url).send().await { match client.get(&url).send().await {
Ok(resp) if resp.status().is_success() => { Ok(resp) if resp.status().is_success() => {
let id_resp: IdResponse = resp.json().await?; let id_resp: IdResponse = resp.json().await?;
@@ -163,19 +163,38 @@ async fn register_with_server(base_url: &str) -> Result<(i32, String), Box<dyn E
"✅ Received ID {} for IP {}", "✅ Received ID {} for IP {}",
id_resp.id, id_resp.ip_address id_resp.id, id_resp.ip_address
); );
break (id_resp.id, id_resp.ip_address); return Ok((id_resp.id, id_resp.ip_address));
} }
Ok(resp) => println!("⚠️ Server responded with status: {}", resp.status()), Ok(resp) if resp.status() == StatusCode::NOT_FOUND => {
Err(err) => println!("Request failed: {}", err), eprintln!("Server with IP {} not found in database", ip);
return Err("Server not registered in database".into());
} }
Ok(resp) => {
println!("⚠️ Server responded with status: {}", resp.status());
sleep(Duration::from_secs(3)).await; sleep(Duration::from_secs(3)).await;
}; }
Err(err) => {
println!("❌ Request failed: {}", err);
sleep(Duration::from_secs(3)).await;
}
}
}
}
// Then register hardware info async fn register_with_server(base_url: &str) -> Result<(i32, String), Box<dyn Error>> {
// First get local IP
let ip = local_ip_address::local_ip()?.to_string();
// Get server ID from backend
let (server_id, registered_ip) = get_server_id_by_ip(base_url, &ip).await?;
// Collect hardware info
let hardware = HardwareInfo::collect().await?; let hardware = HardwareInfo::collect().await?;
// Send registration data
let registration = RegistrationDto { let registration = RegistrationDto {
id, id: server_id,
ip_address: ip_address.clone(), ip_address: registered_ip,
cpu_type: hardware.cpu_type, cpu_type: hardware.cpu_type,
cpu_cores: hardware.cpu_cores, cpu_cores: hardware.cpu_cores,
gpu_type: hardware.gpu_type, gpu_type: hardware.gpu_type,
@@ -187,12 +206,12 @@ async fn register_with_server(base_url: &str) -> Result<(i32, String), Box<dyn E
if resp.status().is_success() { if resp.status().is_success() {
println!("Successfully registered with server."); println!("Successfully registered with server.");
Ok((server_id, registered_ip))
} else { } else {
let text = resp.text().await?; let text = resp.text().await?;
println!("Registration failed: {}", text); eprintln!("Registration failed: {}", text);
Err(text.into())
} }
Ok((id, ip_address))
} }
async fn heartbeat_loop(base_url: &str, ip: &str) -> Result<(), Box<dyn Error>> { async fn heartbeat_loop(base_url: &str, ip: &str) -> Result<(), Box<dyn Error>> {