fixed json formatting

This commit is contained in:
2025-10-29 21:07:29 +01:00
parent b134be4c88
commit 375b4450f0
5 changed files with 599 additions and 23 deletions

View File

@@ -193,37 +193,30 @@ pub async fn broadcast_docker_containers(
server_id: u16,
container_dto: &DockerRegistrationDto,
) -> Result<(), Box<dyn Error + Send + Sync>> {
// First get local IP
println!("Preparing to broadcast docker containers...");
// Create HTTP client for registration
let client = Client::builder()
.danger_accept_invalid_certs(true)
.build()?;
// Create a new struct that matches the server's expected format
#[derive(Serialize)]
struct ServiceDiscoveryRequest {
#[serde(rename = "Server_id")]
server_id: u16,
#[serde(rename = "Containers")]
containers: String, // JSON string instead of Vec
}
// Serialize containers to JSON string
let containers_json = serde_json::to_string(&container_dto.containers)
.map_err(|e| format!("Failed to serialize containers: {}", e))?;
let broadcast_data = ServiceDiscoveryRequest {
server_id,
containers: containers_json,
};
// Prepare registration data
let mut broadcast_data = container_dto.clone();
broadcast_data.server_id = server_id;
// Try to register (will retry on failure)
loop {
println!("Attempting to broadcast containers...");
let json_body = serde_json::to_string_pretty(&broadcast_data)?;
println!("📤 JSON being posted:\n{}", json_body);
let url = format!("{}/monitoring/service-discovery", base_url);
match client.post(&url).json(&broadcast_data).send().await {
match client.post(&url).json(&container_dto).send().await {
Ok(resp) if resp.status().is_success() => {
println!(
"✅ Successfully broadcasted docker containers for server {}",
server_id
"✅ Successfully broadcasted following docker container: {:?}",
container_dto
);
return Ok(());
}

View File

@@ -277,9 +277,12 @@ impl DockerManager {
&self,
) -> Result<DockerRegistrationDto, Box<dyn Error + Send + Sync>> {
let containers = self.get_containers().await?;
let container_string = serde_json::to_value(&containers)?;
let dto = DockerRegistrationDto {
server_id: 0, // This will be set by the caller
containers, // Fallback to empty array
containers: container_string,
};
Ok(dto)

View File

@@ -114,10 +114,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let container_dto = if let Some(ref docker_manager) = docker_manager {
docker_manager.create_registration_dto().await?
} else {
println!("Fallback for failing registration");
models::DockerRegistrationDto {
server_id: 0,
//container_count: 0, --- IGNORE ---
containers: Vec::new(),
containers: serde_json::to_value(&"")?,
}
};
let _ =

View File

@@ -12,6 +12,7 @@
use crate::docker::stats;
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Registration data sent to the backend server.
///
@@ -199,7 +200,7 @@ pub struct DockerRegistrationDto {
/// image: docker image name
/// name: container name
#[serde(rename = "Containers")]
pub containers: Vec<DockerContainer>, // Vec<DockerContainer>,
pub containers: Value, // Vec<DockerContainer>,
}
#[derive(Debug, Serialize, Clone)]