use axum::{response::IntoResponse, Json, extract::Path}; use tracing::{error, instrument}; use solarlib::star::Star; use solarlib::planet::Planet; use crate::{errors::*, get_star}; pub async fn list() -> JsonResult>> { let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); let mut star = Star::new(con_url)?; let inhabitants = star.inhabitants()?; Ok(Json(inhabitants)) } pub async fn get(Path(uuid): Path) -> JsonResult> { let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); let mut star = Star::new(con_url)?; if let Ok(p) = star.find_planet(uuid) { return Ok(Json(p)); } else { return Err(ServiceError::NotFound); } } pub async fn shutdown(Path(uuid): Path) -> NoneResult { let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); let mut star = Star::new(con_url)?; if let Ok(p) = star.find_planet(uuid) { p.shutdown()?; } else { return Err(ServiceError::NotFound); } Ok(()) } pub async fn start(Path(uuid): Path) -> NoneResult { let mut s = get_star()?; if let Ok(p) = s.find_planet(uuid) { p.start()?; } else { return Err(ServiceError::NotFound); } Ok(()) } pub async fn pause(Path(uuid): Path) -> NoneResult { let mut s = get_star()?; if let Ok(p) = s.find_planet(uuid) { p.pause()?; } else { return Err(ServiceError::NotFound); } Ok(()) } pub async fn reboot(Path(uuid): Path) -> NoneResult { let mut s = get_star()?; if let Ok(p) = s.find_planet(uuid) { p.reboot()?; } else { return Err(ServiceError::NotFound); } Ok(()) }