works(isch)
This commit is contained in:
158
src/main.rs
Normal file
158
src/main.rs
Normal file
@@ -0,0 +1,158 @@
|
||||
extern crate chrono;
|
||||
extern crate iron;
|
||||
extern crate rand;
|
||||
extern crate snap;
|
||||
|
||||
use chrono::*;
|
||||
use iron::method::Method;
|
||||
use iron::modifiers::Redirect;
|
||||
use iron::prelude::*;
|
||||
use iron::Url;
|
||||
use rand::Rng;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::iter::Iterator;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
struct Snippet<'a> {
|
||||
id: String,
|
||||
storage: &'a SnippetStorage<'a>,
|
||||
}
|
||||
|
||||
struct SnippetMeta {
|
||||
created: DateTime<UTC>,
|
||||
compression: Option<String>,
|
||||
}
|
||||
|
||||
struct SnippetStorage<'a> {
|
||||
directory: &'a Path,
|
||||
}
|
||||
|
||||
impl<'a> Snippet<'a> {
|
||||
pub fn random(storage: &'a SnippetStorage) -> Snippet<'a> {
|
||||
Snippet::new(
|
||||
&rand::thread_rng()
|
||||
.gen_ascii_chars()
|
||||
.take(6)
|
||||
.collect::<String>(),
|
||||
storage,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new(id: &str, storage: &'a SnippetStorage) -> Snippet<'a> {
|
||||
Snippet {
|
||||
id: id.to_string(),
|
||||
storage,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn path(&self) -> PathBuf {
|
||||
self.storage.directory.join(&self.id)
|
||||
}
|
||||
|
||||
fn metadata(&self) -> Result<SnippetMeta, io::Error> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn contents(&self) -> Result<String, io::Error> {
|
||||
let mut file = File::open(self.path())?;
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text)?;
|
||||
Ok(text)
|
||||
}
|
||||
|
||||
fn write(self, content: &str) -> Result<Snippet<'a>, io::Error> {
|
||||
fs::write(self.path(), content).map(|_| self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> SnippetStorage<'a> {
|
||||
pub fn new(directory: &'a Path) -> SnippetStorage<'a> {
|
||||
SnippetStorage {
|
||||
directory: directory,
|
||||
}
|
||||
}
|
||||
|
||||
fn has(&self, id: &str) -> bool {
|
||||
self.directory.join(id).exists()
|
||||
}
|
||||
|
||||
fn open(&self, id: &str) -> Option<Snippet> {
|
||||
if self.has(id) {
|
||||
Some(Snippet::new(id, self))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const STORAGE_DIR: &str = "/snips";
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const STORAGE_DIR: &str = "/tmp";
|
||||
|
||||
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 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, ""))?;
|
||||
String::from_utf8(bytes)
|
||||
.map_err(|err| IronError::new(err, "Invalid utf8"))?
|
||||
};
|
||||
Snippet::random(&storage)
|
||||
.write(&*text)
|
||||
.map_err(|err| IronError::new(err, "Failed to save snippet"))
|
||||
};
|
||||
snip.map(|snip| {
|
||||
let mut snip_url = req.url.clone().into_generic_url();
|
||||
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::Ok,
|
||||
format!("<meta http-equiv=\"refresh\" content=\"0; url={}/\" />", snip_url)
|
||||
))
|
||||
})
|
||||
} else {
|
||||
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")))
|
||||
}
|
||||
}
|
||||
_ => Ok(Response::with((iron::status::BadRequest, "Give or take"))),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let chain = Chain::new(handle);
|
||||
println!("Starting brownpaper: {}", STORAGE_DIR);
|
||||
Iron::new(chain).http("0.0.0.0:3000").unwrap();
|
||||
}
|
Reference in New Issue
Block a user