Add support for generating a default mushroom.
This commit is contained in:
1 parent
a54e7a341b
commit
8fa25de7bb
1 file changed
+66
-4
+66
-4
@@ -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() {
|
pub async fn run_server() {
|
||||||
rocket::build()
|
rocket::build()
|
||||||
.mount("/", routes![hello])
|
.mount("/", routes![hello, generate_default])
|
||||||
.launch()
|
.launch()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/hello")]
|
||||||
pub fn hello() -> &'static str {
|
fn hello() -> &'static str {
|
||||||
"Hello, world!"
|
"Hello, world!"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Responder)]
|
||||||
|
enum AsyncBinaryStelResponder {
|
||||||
|
#[response(status = 500)]
|
||||||
|
Error(&'static str),
|
||||||
|
BinaryStl {
|
||||||
|
inner: Vec<u8>,
|
||||||
|
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::<u8>::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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user