added version endpoint
This commit is contained in:
parent
3b3125bb56
commit
140451342a
68
src/main.rs
68
src/main.rs
@ -6,6 +6,7 @@ extern crate snap;
|
|||||||
use chrono::*;
|
use chrono::*;
|
||||||
use iron::method::Method;
|
use iron::method::Method;
|
||||||
use iron::prelude::*;
|
use iron::prelude::*;
|
||||||
|
use iron::modifiers::Redirect;
|
||||||
use iron::url::Url;
|
use iron::url::Url;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@ -95,16 +96,23 @@ const STORAGE_DIR: &str = "/snips";
|
|||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
const STORAGE_DIR: &str = "/tmp";
|
const STORAGE_DIR: &str = "/tmp";
|
||||||
|
|
||||||
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
fn handle(req: &mut Request) -> IronResult<Response> {
|
fn handle(req: &mut Request) -> IronResult<Response> {
|
||||||
println!("{}", req.url);
|
println!("{}", req.url);
|
||||||
let storage = SnippetStorage::new(&Path::new(STORAGE_DIR));
|
let storage = SnippetStorage::new(&Path::new(STORAGE_DIR));
|
||||||
match req.method {
|
let segments: Vec<&str> = req.url.path();
|
||||||
Method::Post => {
|
match (&req.method, segments.first()) {
|
||||||
let segments: Vec<&str> = req.url.path();
|
(Method::Get, Some(&"version")) => Ok(Response::with((iron::status::Ok, VERSION))),
|
||||||
if segments.first().iter().all(|seg| *seg == &"new") {
|
(Method::Post, Some(path)) => {
|
||||||
|
if path == &"new" {
|
||||||
let snip = {
|
let snip = {
|
||||||
let text: String = {
|
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 bytes = ((&mut req.body).bytes().take(1024 * 512).collect::<Result<
|
||||||
|
Vec<u8>,
|
||||||
|
io::Error,
|
||||||
|
>>(
|
||||||
|
)).map_err(|err| IronError::new(err, ""))?;
|
||||||
String::from_utf8(bytes)
|
String::from_utf8(bytes)
|
||||||
.map_err(|err| IronError::new(err, "Invalid utf8"))?
|
.map_err(|err| IronError::new(err, "Invalid utf8"))?
|
||||||
};
|
};
|
||||||
@ -115,39 +123,35 @@ fn handle(req: &mut Request) -> IronResult<Response> {
|
|||||||
snip.map(|snip| {
|
snip.map(|snip| {
|
||||||
let mut snip_url: Url = req.url.clone().into();
|
let mut snip_url: Url = req.url.clone().into();
|
||||||
snip_url.set_path(&*("/".to_string() + &*snip.id));
|
snip_url.set_path(&*("/".to_string() + &*snip.id));
|
||||||
/*Response::with((
|
|
||||||
iron::status::TemporaryRedirect,
|
|
||||||
Redirect(Url::from_generic_url(snip_url).unwrap()),
|
|
||||||
))*/
|
|
||||||
Response::with((
|
Response::with((
|
||||||
|
iron::status::TemporaryRedirect,
|
||||||
|
Redirect(iron::Url::from_generic_url(snip_url).unwrap())),
|
||||||
|
)
|
||||||
|
/*Response::with((
|
||||||
iron::status::Ok,
|
iron::status::Ok,
|
||||||
format!("<meta http-equiv=\"refresh\" content=\"0; url={}/\" />", snip_url)
|
format!(
|
||||||
))
|
"<meta http-equiv=\"refresh\" content=\"0; url={}/\" />",
|
||||||
|
snip_url
|
||||||
|
),
|
||||||
|
))*/
|
||||||
})
|
})
|
||||||
} else {
|
} 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 => {
|
(Method::Get, Some(id)) => {
|
||||||
let segments: Vec<&str> = req
|
let att = storage.open(&id).map(|snip| snip.contents()).map(|res| {
|
||||||
.url
|
Response::with(
|
||||||
.path()
|
res.map(|text| (iron::status::Ok, text))
|
||||||
.into_iter()
|
.unwrap_or((iron::status::InternalServerError, "..".to_string())),
|
||||||
.filter(|seg| seg.len() > 0)
|
)
|
||||||
.collect();
|
});
|
||||||
if segments.len() == 1 {
|
Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry"))))
|
||||||
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, _) => Ok(Response::with((iron::status::NotFound, "Wrong path pal"))),
|
||||||
_ => Ok(Response::with((iron::status::BadRequest, "Give or take"))),
|
_ => Ok(Response::with((iron::status::BadRequest, "Give or take"))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user