Fixed the api issue so that it works now

This commit is contained in:
Jonathan Wanke 2025-11-18 00:01:35 +01:00
commit e136278f26

View file

@ -1,3 +1,4 @@
use crate::gui::log_panel::log_message;
use axum::http::HeaderValue; use axum::http::HeaderValue;
use http_body_util::Full; use http_body_util::Full;
use hyper::body::Bytes; use hyper::body::Bytes;
@ -18,54 +19,71 @@ pub async fn handle(
.body(Full::new(Bytes::from("403 Forbidden".to_string()))) .body(Full::new(Bytes::from("403 Forbidden".to_string())))
.unwrap(); .unwrap();
} }
let mut path_parts = path.split("/");
let (status, content, body_text) = match path_parts.nth(1).unwrap() { // Collect path parts into a vector to avoid iterator consumption issues
"app_state" => (StatusCode::OK, "application/json", { let path_parts: Vec<&str> = path.split("/").collect();
let json = APP_STATE.lock().unwrap().clone();
json.to_json().to_string() // path_parts[0] is empty (before first /), path_parts[1] should be "api", path_parts[2] is the endpoint
}), let (status, content, body_text) = if path_parts.len() >= 3 {
"users" => (StatusCode::OK, "application/json", { match path_parts[2] {
if path_parts.nth(2).is_some() { "app_state" => (StatusCode::OK, "application/json", {
if path_parts.nth(2).unwrap() == "add" { let json = APP_STATE.lock().unwrap().clone();
"{}".to_string() json.to_json().to_string()
} else if path_parts.nth(2).unwrap() == "remove" { }),
"{}".to_string() "users" => (StatusCode::OK, "application/json", {
} else if path_parts.nth(2).unwrap() == "get" { // Check for sub-endpoints like /api/users/add, /api/users/get, etc.
if path_parts.len() >= 4 {
match path_parts[3] {
"add" => "{}".to_string(),
"remove" => "{}".to_string(),
"get" => {
let users = user_manager::get_users();
let mut json = JsonValue::new_array();
for user in users {
let _ = json.push(user.to_json());
}
json.to_string()
}
_ => "{}".to_string(),
}
} else {
// Default: return all users
let users = user_manager::get_users(); let users = user_manager::get_users();
let mut json = JsonValue::new_array(); let mut json = JsonValue::new_array();
for user in users { for user in users {
let _ = json.push(user.to_json()); let _ = json.push(user.to_json());
} }
json.to_string() json.to_string()
} else {
"{}".to_string()
} }
} else { }),
let users = user_manager::get_users(); "communities" => (StatusCode::OK, "application/json", {
let communities = community_manager::get_communities().await;
let mut json = JsonValue::new_array(); let mut json = JsonValue::new_array();
for user in users { for community in communities {
let _ = json.push(user.to_json()); let _ = json.push(community.to_json().await);
} }
json.to_string() json.to_string()
} }),
}), "settings" => (StatusCode::OK, "application/json", {
"communities" => (StatusCode::OK, "application/json", { CONFIG.lock().unwrap().config.to_string()
let communities = community_manager::get_communities().await; }),
let mut json = JsonValue::new_array();
for community in communities {
let _ = json.push(community.to_json().await);
}
json.to_string()
}),
"settings" => (StatusCode::OK, "application/json", {
CONFIG.lock().unwrap().config.to_string()
}),
_ => ( _ => {
log_message(format!("Unknown API endpoint: {}", path));
(
StatusCode::NOT_FOUND,
"application/json",
"404 Not Found".to_string(),
)
}
}
} else {
log_message(format!("Invalid API path: {}", path));
(
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,
"application/json", "application/json",
"404 Not Found".to_string(), "404 Not Found".to_string(),
), )
}; };
let body = Full::new(Bytes::from(body_text.to_string())); let body = Full::new(Bytes::from(body_text.to_string()));
HttpResponse::builder() HttpResponse::builder()