use tiny_http::{Method, Request};
use std::io::Read;
const STORE_PREFIX: &str = "/store/";
const ADMIN_PREFIX: &str = "/admin/";
fn get_key(url: &str, prefix: &str) -> String {
let pfx_len = if url.starts_with(prefix) {
prefix.len()
} else {
0
};
match url.find('?') {
None => String::from(&url[pfx_len..]),
Some(indx) => String::from(&url[pfx_len..indx]),
}
}
#[test]
fn test_get_key() {
let url = "/store/originalkey?q=this&that=that#foo";
assert_eq!(get_key(url, "/store/"), String::from("originalkey"));
}
trait Respond {
fn respond(self, req: Request);
}
trait Service: Sync + Send {
type Response: Respond + Default;
fn get(&self, key: String) -> Self::Response;
fn save(&self, key: String, value: impl Read) -> Self::Response;
fn delete(&self, key: String) -> Self::Response;
fn dispatch(&self, mut req: Request) {
let key = get_key(req.url(), STORE_PREFIX);
let resp = match *req.method() {
Method::Get => self.get(key),
Method::Post | Method::Put => self.save(key, req.as_reader()),
Method::Delete => self.delete(key),
_ => Default::default(),
};
resp.respond(req);
}
}
#[macro_use]
mod macros;
pub mod master;
pub mod volume;