use fire_http as fire; use fire::extractor::Res; use fire::header::Mime; use fire::{get, post, Error, Request, Resource, Response, Result}; use std::sync::Mutex; #[derive(Resource)] struct LastPost(Mutex); #[get("/")] fn hello_world(last_post: Res) -> Response { let body = { let last_post = last_post.0.lock().unwrap(); format!( "Hello, World! Post Something:

Last Post

{}

", &last_post ) }; Response::builder() .content_type(Mime::HTML) .body(body) .build() } #[post("/")] async fn hello_world_post( req: &mut Request, last_post: &LastPost, ) -> Result { // we need to update the size limit req.set_size_limit(Some(256)); let body = req .take_body() .into_string() .await .map_err(Error::from_client_io)?; let res = format!("Posted Body: {}", body); *last_post.0.lock().unwrap() = body; Ok(res) } #[tokio::main] async fn main() { let mut server = fire::build("0.0.0.0:3000") .await .expect("Address could not be parsed"); server.add_data(LastPost(Mutex::new(String::new()))); server.request_size_limit(1); server.add_route(hello_world); server.add_route(hello_world_post); server.ignite().await.unwrap(); }