From 4c044c28559f356883e4ab3cec437bbb20ce7f89 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Fri, 10 Jun 2022 14:40:56 -0400 Subject: planets: Create new planets Star systems in the palm of your keyboard! --- src/handlers/planets.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'src/handlers/planets.rs') diff --git a/src/handlers/planets.rs b/src/handlers/planets.rs index 06773bc..2c66324 100644 --- a/src/handlers/planets.rs +++ b/src/handlers/planets.rs @@ -1,11 +1,17 @@ +use axum::Extension; +use axum_macros::debug_handler; use axum::{response::IntoResponse, Json, extract::Path}; +use solarlib::ship::{DbShip, Ship}; +use tokio::process::Command; use tracing::{error, instrument}; -use solarlib::star::Star; +use solarlib::star::{Star, NewPlanet}; use solarlib::planet::Planet; +use solarlib::errors::Error as SolarlibError; -use crate::{errors::*, get_star}; +use crate::{errors::*, get_star, State}; +use std::sync::Arc; pub async fn list() -> JsonResult>> { let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); @@ -100,3 +106,51 @@ pub async fn force_shutdown(Path(uuid): Path) -> NoneResult { Ok(()) } + +#[debug_handler] +pub async fn new_planet(Json(new_planet): Json) -> JsonResult> { + let hw_url = std::env::var("HOMEWORLD_URL").unwrap(); + + let ship_shasum = new_planet.clone().ship; + + let res: DbShip = reqwest::get(format!("http://{}/ships/get/{}", hw_url, ship_shasum)).await?.json().await?; + let ship: Ship = res.into(); + + let mut s = get_star()?; + + println!("{:?}", s); + + // Try to create right away, if the Ship already exists on the system, it'll go through. If + // not, we can download it by using the shasum + let r = s.planet(new_planet.clone().name, new_planet.max_mem, new_planet.max_cpus, new_planet.disk_size_mb, ship.clone()); + match r { + Err(e) => { + match e { + SolarlibError::MissingImage(_) => { + ship.download(&s)?; + return Ok(Json(s.planet(new_planet.name, new_planet.max_mem, new_planet.max_cpus, new_planet.disk_size_mb, ship.clone())?)); + }, + _ => { + return Err(ServiceError::Solarlib(e)); + } + } + }, + Ok(r) => { + let tmp = s.inhabitants()?[0].clone(); + return Ok(Json(tmp)); + } + }; +} + + +pub async fn no_planet(Path(uuid): Path) -> NoneResult { + let mut s = get_star()?; + + if let Ok(p) = s.find_planet(uuid) { + p.deathstar()?; + } else { + return Err(ServiceError::NotFound); + } + + Ok(()) +} -- cgit v1.2.3