api::browse now returns json response

This commit is contained in:
Antoine Gersant 2016-08-18 23:29:27 -07:00
parent 63d10fa695
commit 474710a6a9
5 changed files with 37 additions and 13 deletions

1
Cargo.lock generated
View file

@ -5,6 +5,7 @@ dependencies = [
"iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"mount 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "router 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"staticfile 0.3.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)", "url 1.2.0 (git+https://github.com/servo/rust-url)",
] ]

View file

@ -9,6 +9,9 @@ version = "0.4.0"
[dependencies.url] [dependencies.url]
git = "https://github.com/servo/rust-url" git = "https://github.com/servo/rust-url"
[dependencies]
rustc-serialize = "0.3"
[dependencies] [dependencies]
router = "*" router = "*"
staticfile = "*" staticfile = "*"

View file

@ -5,10 +5,19 @@ use std::ops::Deref;
use iron::prelude::*; use iron::prelude::*;
use iron::status; use iron::status;
use mount::Mount; use mount::Mount;
use rustc_serialize::json;
use url::percent_encoding::percent_decode; use url::percent_encoding::percent_decode;
use collection; use collection;
impl From<collection::CollectionError> for IronError {
fn from(err: collection::CollectionError) -> IronError {
match err {
collection::CollectionError::Io(e) => IronError::new(e, status::NotFound)
}
}
}
pub fn get_api_handler() -> Mount { pub fn get_api_handler() -> Mount {
let mut mount = Mount::new(); let mut mount = Mount::new();
mount.mount("/browse/", self::browse) mount.mount("/browse/", self::browse)
@ -25,11 +34,19 @@ fn path_from_request(request: &Request) -> Result<PathBuf, Utf8Error> {
fn browse(request: &mut Request) -> IronResult<Response> { fn browse(request: &mut Request) -> IronResult<Response> {
let path = path_from_request(request); let path = path_from_request(request);
if path.is_err() { if path.is_err() {
return Ok(Response::with((status::BadRequest))); return Ok(Response::with(status::BadRequest));
} }
let browse_result = collection::browse(&path.unwrap()); let path = path.unwrap();
println!("{:?}", browse_result.unwrap_or(vec![])); // TMP let browse_result = try!(collection::browse(&path));
Ok(Response::with((status::Ok, "TODO browse data here")))
let result_json = json::encode(&browse_result);
if result_json.is_err() {
return Ok(Response::with(status::InternalServerError));
}
let result_json = result_json.unwrap();
println!("{:?}", browse_result); // TMP
Ok(Response::with((status::Ok, result_json)))
} }
fn flatten(request: &mut Request) -> IronResult<Response> { fn flatten(request: &mut Request) -> IronResult<Response> {

View file

@ -3,14 +3,16 @@ use std::io;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Debug)]
pub struct Song {
path: PathBuf,
}
#[derive(Debug)] #[derive(Debug, RustcEncodable)]
pub struct Song(PathBuf);
#[derive(Debug, RustcEncodable)]
pub struct Directory(PathBuf);
#[derive(Debug, RustcEncodable)]
pub enum CollectionFile { pub enum CollectionFile {
Directory(PathBuf), Directory(Directory),
Song(Song), Song(Song),
} }
@ -36,14 +38,14 @@ pub fn browse(path: &Path) -> Result<Vec<CollectionFile>, CollectionError> {
let file_meta = try!(file.metadata()); let file_meta = try!(file.metadata());
let file_path = file.path().to_owned(); let file_path = file.path().to_owned();
if file_meta.is_file() { if file_meta.is_file() {
let collection_file = CollectionFile::Song(Song {path: file_path }); let collection_file = CollectionFile::Song(Song(file_path));
out.push(collection_file); out.push(collection_file);
} else if file_meta.is_dir() { } else if file_meta.is_dir() {
let collection_file = CollectionFile::Directory(file_path); let collection_file = CollectionFile::Directory(Directory(file_path));
out.push(collection_file); out.push(collection_file);
} }
} }
Ok(out) Ok(out)
} }

View file

@ -1,6 +1,7 @@
extern crate core; extern crate core;
extern crate iron; extern crate iron;
extern crate mount; extern crate mount;
extern crate rustc_serialize;
extern crate staticfile; extern crate staticfile;
extern crate url; extern crate url;