Parse and route browse and flatten requests

This commit is contained in:
Antoine Gersant 2016-08-14 19:22:45 -07:00
parent 749ec12c24
commit 259699a3e8
7 changed files with 91 additions and 18 deletions

20
Cargo.lock generated
View file

@ -6,6 +6,7 @@ dependencies = [
"mount 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"router 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"staticfile 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.2.0 (git+https://github.com/servo/rust-url)",
]
[[package]]
@ -67,6 +68,16 @@ dependencies = [
"url 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "idna"
version = "0.1.0"
source = "git+https://github.com/servo/rust-url#ed1395ec242c7fc929a7821698b901ad174d90a5"
dependencies = [
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "idna"
version = "0.1.0"
@ -286,6 +297,15 @@ dependencies = [
"traitobject 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "url"
version = "1.2.0"
source = "git+https://github.com/servo/rust-url#ed1395ec242c7fc929a7821698b901ad174d90a5"
dependencies = [
"idna 0.1.0 (git+https://github.com/servo/rust-url)",
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "url"
version = "1.2.0"

View file

@ -6,6 +6,9 @@ authors = ["Antoine Gersant <antoine.gersant@lesforges.org>"]
[dependencies.iron]
version = "0.4.0"
[dependencies.url]
git = "https://github.com/servo/rust-url"
[dependencies]
router = "*"
staticfile = "*"

View file

@ -0,0 +1,41 @@
use core::str::Utf8Error;
use std::path::PathBuf;
use iron::prelude::*;
use iron::status;
use mount::Mount;
use url::percent_encoding::percent_decode;
use collection::browse as collection_browse;
use collection::flatten as collection_flatten;
pub fn get_api_handler() -> Mount {
let mut mount = Mount::new();
mount.mount("/browse/", self::browse)
.mount("/flatten/", self::flatten);
mount
}
fn path_from_request(request: &Request) -> Result<PathBuf, Utf8Error> {
let path_string = request.url.path().join("/");
let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8();
decoded_path.map(|s| PathBuf::from(s.into_owned()))
}
fn browse(request: &mut Request) -> IronResult<Response> {
let path = path_from_request(request);
if path.is_err() {
return Ok(Response::with((status::BadRequest)));
}
collection_browse(&path.unwrap());
Ok(Response::with((status::Ok, "TODO browse data here")))
}
fn flatten(request: &mut Request) -> IronResult<Response> {
let path = path_from_request(request);
if path.is_err() {
return Ok(Response::with((status::BadRequest)));
}
collection_flatten(&path.unwrap());
Ok(Response::with((status::Ok, "TODO Flatten data here")))
}

18
src/collection/mod.rs Normal file
View file

@ -0,0 +1,18 @@
use std::path::PathBuf;
pub enum SFile {
// Directory,
// Song,
}
pub fn browse(path: &PathBuf) -> Vec<SFile> {
println!("Browse {:?}", path);
let out = vec![];
out
}
pub fn flatten(path: &PathBuf) -> Vec<SFile> {
println!("Flatten {:?}", path);
let out = vec![];
out
}

View file

View file

@ -1,30 +1,21 @@
extern crate core;
extern crate iron;
extern crate router;
extern crate mount;
extern crate staticfile;
extern crate url;
use iron::prelude::*;
use iron::status;
use router::Router;
use mount::Mount;
use staticfile::Static;
mod api;
mod collection;
fn main() {
let mut mount = Mount::new();
mount.mount( "/static/", Static::new("samplemusic/") );
let api_handler = api::get_api_handler();
mount.mount("/static/", Static::new("samplemusic/"))
.mount("/api/", api_handler);
let mut router = Router::new();
router.get("/static/*", mount );
router.get("/api/*", |_: &mut Request| {
Ok(Response::with((status::Ok, "API")))
} );
router.get("/web/*", |_: &mut Request| {
Ok(Response::with((status::Ok, "Web")))
} );
router.get("/", |_: &mut Request| {
Ok(Response::with((status::Ok, "Home")))
} );
Iron::new(router).http("localhost:3000").unwrap();
}
Iron::new(mount).http("localhost:3000").unwrap();
}

View file