use futures::future; use gotham::handler::{HandlerFuture, IntoHandlerError}; use gotham::helpers::http::response::create_response; use gotham::state::{FromState, State}; use gotham_derive::{StateData, StaticResponseExtender}; use hyper::StatusCode; use mime; use serde_derive::Deserialize; use crate::config::Config; use crate::paste::Paste; #[derive(Deserialize, StateData, StaticResponseExtender)] pub struct Params { id: String, } pub fn get(mut state: State) -> Box { Box::new({ let config = Config::take_from(&mut state); let Params { id } = Params::take_from(&mut state); let mut path = config.data_directory; path.push(id); match Paste::from_file(path) { Ok(paste) => { let res = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, paste.text); future::ok((state, res)) } Err(e) => { let io_error = std::io::Error::new(std::io::ErrorKind::Other, e.description()); future::err((state, io_error.into_handler_error())) } } }) }