works(isch)
This commit is contained in:
commit
145f7cfd40
5
.dockerignore
Normal file
5
.dockerignore
Normal file
@ -0,0 +1,5 @@
|
||||
.*
|
||||
target/*/deps
|
||||
target/*/build
|
||||
target/*/.fingerprint
|
||||
target/*/incremental
|
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||
Cargo.lock
|
||||
|
||||
#Intellj
|
||||
.iml
|
||||
.idea
|
44
.idea/libraries/Cargo__brownpaper_.xml
generated
Normal file
44
.idea/libraries/Cargo__brownpaper_.xml
generated
Normal file
@ -0,0 +1,44 @@
|
||||
<component name="libraryTable">
|
||||
<library name="Cargo <brownpaper>">
|
||||
<CLASSES>
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/unicase-1.4.0" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/unicode-normalization-0.1.2" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/solicit-0.4.4" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/plugin-0.2.6" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/conduit-mime-types-0.7.3" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.3.6" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc_version-0.1.7" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/language-tags-0.2.2" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/typemap-0.3.3" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/unsafe-any-0.4.1" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.15" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-serialize-0.3.19" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-0.2.8" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/traitobject-0.0.3" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/matches-0.1.2" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/hpack-0.2.0" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/iron-0.4.0" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-0.1.16" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/traitobject-0.0.1" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/mime-0.2.2" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/unicode-bidi-0.2.3" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-build-0.1.1" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/modifier-0.1.0" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/url-1.2.0" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/snappy-0.3.0" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/hyper-0.9.10" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/typeable-0.1.2" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/kernel32-sys-0.2.2" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/semver-0.1.20" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/num_cpus-0.2.13" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/error-0.1.9" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/pkg-config-0.3.8" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/cookie-0.2.5" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/httparse-1.1.2" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/time-0.1.35" />
|
||||
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/idna-0.1.0" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
16
Cargo.toml
Normal file
16
Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "brownpaper"
|
||||
version = "0.1.0"
|
||||
authors = ["shim_ <shimun@shimun.net>"]
|
||||
|
||||
[dependencies]
|
||||
|
||||
snap = "0.1"
|
||||
|
||||
rustc-serialize = "0.3.19"
|
||||
|
||||
iron = "0.6.0"
|
||||
|
||||
rand = "0.4.2"
|
||||
|
||||
chrono = { version = "0.2", features = ["rustc-serialize"] }
|
9
Dockerfile
Normal file
9
Dockerfile
Normal file
@ -0,0 +1,9 @@
|
||||
FROM scratch
|
||||
|
||||
VOLUME /snips
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
COPY target/x86_64-unknown-linux-musl/release/brownpaper /bin/
|
||||
|
||||
ENTRYPOINT [ "/bin/brownpaper" ]
|
17
brownpaper.iml
Normal file
17
brownpaper.iml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="RUST_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Cargo <brownpaper>" level="project" />
|
||||
<orderEntry type="library" name="Rust <brownpaper>" level="project" />
|
||||
</component>
|
||||
</module>
|
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();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user