From 140451342a5238dcfb19ca823ec79980846d6623 Mon Sep 17 00:00:00 2001 From: shim_ <> Date: Sun, 4 Nov 2018 21:00:48 +0100 Subject: [PATCH] added version endpoint --- src/main.rs | 68 ++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index a7d89ed..754cc6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate snap; use chrono::*; use iron::method::Method; use iron::prelude::*; +use iron::modifiers::Redirect; use iron::url::Url; use rand::Rng; use std::fs; @@ -95,16 +96,23 @@ const STORAGE_DIR: &str = "/snips"; #[cfg(debug_assertions)] const STORAGE_DIR: &str = "/tmp"; +const VERSION: &str = env!("CARGO_PKG_VERSION"); + fn handle(req: &mut Request) -> IronResult { println!("{}", req.url); let storage = SnippetStorage::new(&Path::new(STORAGE_DIR)); - match req.method { - Method::Post => { - let segments: Vec<&str> = req.url.path(); - if segments.first().iter().all(|seg| *seg == &"new") { + let segments: Vec<&str> = req.url.path(); + match (&req.method, segments.first()) { + (Method::Get, Some(&"version")) => Ok(Response::with((iron::status::Ok, VERSION))), + (Method::Post, Some(path)) => { + if path == &"new" { let snip = { - let text: String = { - let bytes = ((&mut req.body).bytes().take(1024 * 512).collect::,io::Error>>()).map_err(|err| IronError::new(err, ""))?; + let text: String = { + let bytes = ((&mut req.body).bytes().take(1024 * 512).collect::, + io::Error, + >>( + )).map_err(|err| IronError::new(err, ""))?; String::from_utf8(bytes) .map_err(|err| IronError::new(err, "Invalid utf8"))? }; @@ -115,39 +123,35 @@ fn handle(req: &mut Request) -> IronResult { snip.map(|snip| { let mut snip_url: Url = req.url.clone().into(); snip_url.set_path(&*("/".to_string() + &*snip.id)); - /*Response::with(( - iron::status::TemporaryRedirect, - Redirect(Url::from_generic_url(snip_url).unwrap()), - ))*/ Response::with(( + iron::status::TemporaryRedirect, + Redirect(iron::Url::from_generic_url(snip_url).unwrap())), + ) + /*Response::with(( iron::status::Ok, - format!("", snip_url) - )) + format!( + "", + snip_url + ), + ))*/ }) } else { - Ok(Response::with((iron::status::BadRequest, "Post to /new or die"))) + Ok(Response::with(( + iron::status::BadRequest, + "Post to /new or die", + ))) } } - Method::Get => { - let segments: Vec<&str> = req - .url - .path() - .into_iter() - .filter(|seg| seg.len() > 0) - .collect(); - if segments.len() == 1 { - let id = &segments[0]; - let att = storage.open(&id).map(|snip| snip.contents()).map(|res| { - Response::with( - res.map(|text| (iron::status::Ok, text)) - .unwrap_or((iron::status::InternalServerError, "..".to_string())), - ) - }); - Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry")))) - } else { - Ok(Response::with((iron::status::NotFound, "Wrong path pal"))) - } + (Method::Get, Some(id)) => { + let att = storage.open(&id).map(|snip| snip.contents()).map(|res| { + Response::with( + res.map(|text| (iron::status::Ok, text)) + .unwrap_or((iron::status::InternalServerError, "..".to_string())), + ) + }); + Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry")))) } + (Method::Get, _) => Ok(Response::with((iron::status::NotFound, "Wrong path pal"))), _ => Ok(Response::with((iron::status::BadRequest, "Give or take"))), } }