diff options
author | Cara Salter <cara@devcara.com> | 2022-06-09 10:28:42 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-06-09 10:28:42 -0400 |
commit | 276255f5a20fa05a005e765287239ca5ffe439e4 (patch) | |
tree | 67e325727fe49004d54deee702fb556a7d5ce1f1 /src/handlers/planets.rs | |
parent | bb545403fed8af5b746778c3336a4f3a371148d6 (diff) | |
download | solard-276255f5a20fa05a005e765287239ca5ffe439e4.tar.gz solard-276255f5a20fa05a005e765287239ca5ffe439e4.zip |
planets: State manipulation
Diffstat (limited to 'src/handlers/planets.rs')
-rw-r--r-- | src/handlers/planets.rs | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/src/handlers/planets.rs b/src/handlers/planets.rs index e80e7fe..a57f605 100644 --- a/src/handlers/planets.rs +++ b/src/handlers/planets.rs @@ -1,11 +1,11 @@ -use axum::{response::IntoResponse, Json}; +use axum::{response::IntoResponse, Json, extract::Path}; use tracing::{error, instrument}; use solarlib::star::Star; use solarlib::planet::Planet; -use crate::errors::*; +use crate::{errors::*, get_star}; pub async fn list() -> JsonResult<Json<Vec<Planet>>> { let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); @@ -16,3 +16,51 @@ pub async fn list() -> JsonResult<Json<Vec<Planet>>> { Ok(Json(inhabitants)) } +pub async fn get(Path(uuid): Path<String>) -> JsonResult<Json<Planet>> { + + 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<String>) -> 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<String>) -> 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<String>) -> NoneResult { + let mut s = get_star()?; + + if let Ok(p) = s.find_planet(uuid) { + p.pause()?; + } else { + return Err(ServiceError::NotFound); + } + + Ok(()) +} |