added version endpoint

This commit is contained in:
shim_ 2018-11-04 21:00:48 +01:00
parent 3b3125bb56
commit 140451342a

View File

@ -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<Response> {
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::<Result<Vec<u8>,io::Error>>()).map_err(|err| IronError::new(err, ""))?;
let text: String = {
let bytes = ((&mut req.body).bytes().take(1024 * 512).collect::<Result<
Vec<u8>,
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<Response> {
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!("<meta http-equiv=\"refresh\" content=\"0; url={}/\" />", snip_url)
))
format!(
"<meta http-equiv=\"refresh\" content=\"0; url={}/\" />",
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"))),
}
}