diff --git a/src/server.rs b/src/server.rs index 9dc76d2..246ece2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,14 +1,76 @@ -extern crate approx; +extern crate rocket; + +use rocket::http::ContentType; +use rocket::http::Header; + +use crate::mushroom; +use crate::mushroom::Mushroom; +use crate::render; +use crate::translate; pub async fn run_server() { rocket::build() - .mount("/", routes![hello]) + .mount("/", routes![hello, generate_default]) .launch() .await .unwrap(); } -#[get("/")] -pub fn hello() -> &'static str { +#[get("/hello")] +fn hello() -> &'static str { "Hello, world!" } + +#[derive(Responder)] +enum AsyncBinaryStelResponder { + #[response(status = 500)] + Error(&'static str), + BinaryStl { + inner: Vec, + header: ContentType, + filename: Header<'static>, + }, +} + +async fn generate_helper(mushroom: Mushroom) -> AsyncBinaryStelResponder { + let result = rocket::tokio::task::spawn_blocking(move || { + // Describe our mushroom + + // Generate the binary STL in a byte array. + let layers = translate::mushroom(&mushroom, 0.1); + let mesh = render::render(&layers, 0.1); + let mut binary_stl = Vec::::new(); + stl_io::write_stl(&mut binary_stl, mesh.iter()).unwrap(); + + // Return the byte array. + return binary_stl; + }) + .await; + + return if result.is_ok() { + AsyncBinaryStelResponder::BinaryStl { + inner: result.unwrap(), + header: ContentType::parse_flexible("model/x.stl-binary").unwrap(), + filename: Header { + name: "Content-Disposition".into(), + value: "inline; filename=\"mushroom.stl\"".into(), + }, + } + } else { + AsyncBinaryStelResponder::Error("Internal JoinError") + }; +} + +#[get("/generate")] +async fn generate_default() -> AsyncBinaryStelResponder { + // Default mushroom to generate + let mushroom = mushroom::Mushroom { + stalk_h: 75.0, + stalk_r: 10.0, + cap_h: 25.0, + cap_r: 100.0, + cap_style: mushroom::CapStyle::Dome, + }; + + return generate_helper(mushroom).await; +}