diff --git a/src/main.rs b/src/main.rs index b8addc154a..e859d32be8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate hound; extern crate iron; extern crate router; +extern crate rustc_serialize; pub mod vocab_list_handler; @@ -43,7 +44,7 @@ fn main() { write_file("output.wav", &spec, all_samples); - //start_server(); + start_server(); } fn words_from_argv() -> Vec { diff --git a/src/vocab_list_handler.rs b/src/vocab_list_handler.rs index da75d06741..92190d5b9c 100644 --- a/src/vocab_list_handler.rs +++ b/src/vocab_list_handler.rs @@ -5,6 +5,7 @@ use iron::mime::Mime; use iron::prelude::*; use iron::status; use router::Router; +use rustc_serialize::json; use std::fs::File; use std::fs; @@ -20,30 +21,9 @@ pub struct VocabListHandler { impl Handler for VocabListHandler { fn handle(&self, req: &mut Request) -> IronResult { - /*let not_found = Ok(Response::with((status::NotFound, "File not found."))); - - let filename = req.extensions.get::().unwrap() - .find("filename").unwrap_or(""); - - let mime = match filename { - s if s.ends_with(".html") => { "text/html" }, - s if s.ends_with(".js") => { "application/javascript" }, - _ => { "text/plain" }, - }; - - match self.open_file(filename) { - None => { not_found }, - Some(contents) => { - let content_type = mime.parse::().unwrap(); - Ok(Response::with((content_type, status::Ok, contents))) - }, - }*/ - - //let not_found = Ok(Response::with((status::NotFound, "File not found."))); - - self.list_files(); - - Ok(Response::with(status::BadRequest)) + let words = self.list_files(); + let response = json::encode(&words).unwrap(); + Ok(Response::with((status::Ok, response))) } } @@ -52,10 +32,13 @@ impl VocabListHandler { VocabListHandler { directory: Path::new(directory).to_path_buf() } } - fn list_files(&self) { - println!("listing files..."); + // TODO: Return errors. + /// Return a list of words from the audio files in the directory. + fn list_files(&self) -> Vec { + let mut words = Vec::new(); + let paths = match fs::read_dir(self.directory.as_path()) { - Err(_) => { return; }, + Err(_) => { return words; }, Ok(r) => r, }; @@ -63,9 +46,19 @@ impl VocabListHandler { match path { Err(_) => { continue; }, Ok(r) => { - println!("Path: {:?}", r.file_name()); + match r.file_name().into_string() { + Err(_) => { continue; }, + Ok(s) => { + let word = s.replace(".wav", ""); + words.push(word); + } + } }, } } + + words.sort(); + words } } +