From e232d20c46ef29b8e4b205a87e2a64775e71c3bf Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 4 Dec 2015 04:01:58 -0500 Subject: [PATCH] Adding Iron/Hyper server (initial work) --- Cargo.toml | 18 ++++------ src/main.rs | 18 ++++++++++ src/vocab_list_handler.rs | 71 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 src/vocab_list_handler.rs diff --git a/Cargo.toml b/Cargo.toml index eb984d9d0a..f84386a8a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,7 @@ [package] name = "trumpet" version = "0.0.1" - authors = [ - "Brandon Thomas ", - "Scott Adams ", - ] + authors = [ "Brandon Thomas " ] [[bin]] name = "trumpet" @@ -12,11 +9,10 @@ [dependencies] #cpal = "*" # Hardware audio output (for testing); works well with Hound. hound = "1.0.0" # WAV codec. - #iron = "*" # HTTP/S server. - #log = "0.3.*" # Error and debug logging. - #regex = "0.1.*" - #rustc-serialize = "0.3.15" # JSON parser, etc. - #sample = "*" # Audio sampling tools - #time = "0.1.*" # Duration, and other time types. - #toml = "0.1.*" # TOML parser. + iron = "0.2.*" + log = "0.3.*" + hyper = "0.7.*" + router = "0.0.16" + rustc-serialize = "0.3.15" + time = "0.1.*" diff --git a/src/main.rs b/src/main.rs index 82546c6697..d6d05ca468 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,19 @@ extern crate hound; +extern crate iron; +extern crate router; + +pub mod vocab_list_handler; use std::fs::File; use std::io::BufReader; use std::path::{Path, PathBuf}; +use iron::prelude::*; +use router::Router; use hound::{WavReader, WavSpec, WavWriter}; +use vocab_list_handler::VocabListHandler; + fn main() { let dictionary = [ "absolutely", @@ -36,6 +44,8 @@ fn main() { let spec = get_spec(dictionary[0]); write_file("output.wav", &spec, all_samples); + + //start_server(); } fn get_filename(word: &str) -> PathBuf { @@ -58,3 +68,11 @@ fn write_file(filename: &str, spec: &WavSpec, samples: Vec) { writer.write_sample(s).unwrap(); } } + +fn start_server() { + println!("starting server"); + let mut router = Router::new(); + router.get("/vocab_list", VocabListHandler::new("./sounds/trump")); + Iron::new(router).http("0.0.0.0:9000").unwrap(); +} + diff --git a/src/vocab_list_handler.rs b/src/vocab_list_handler.rs new file mode 100644 index 0000000000..da75d06741 --- /dev/null +++ b/src/vocab_list_handler.rs @@ -0,0 +1,71 @@ +// Copyright (c) 2015 Brandon Thomas + +use iron::Handler; +use iron::mime::Mime; +use iron::prelude::*; +use iron::status; +use router::Router; + +use std::fs::File; +use std::fs; +use std::io::Read; +use std::path::{Path, PathBuf}; + +/// Returns a list of the available vocabulary. +pub struct VocabListHandler { + /// Root of where files can be served from. + /// A PathBuf since `Path` can only be created as a borrow. + directory: PathBuf, +} + +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)) + } +} + +impl VocabListHandler { + pub fn new(directory: &str) -> VocabListHandler { + VocabListHandler { directory: Path::new(directory).to_path_buf() } + } + + fn list_files(&self) { + println!("listing files..."); + let paths = match fs::read_dir(self.directory.as_path()) { + Err(_) => { return; }, + Ok(r) => r, + }; + + for path in paths { + match path { + Err(_) => { continue; }, + Ok(r) => { + println!("Path: {:?}", r.file_name()); + }, + } + } + } +}