Work in progress on handlers

This commit is contained in:
Brandon Thomas committed 2015-12-08 01:59:59 -05:00
1 parent e2ebba0aca
commit 77f0c504df
7 files changed
+147 -13

No files matched your search

Generated
+32
View File
@@ -9,6 +9,7 @@ dependencies = [
"router 0.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
"urlencoded 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -25,6 +26,17 @@ name = "bitflags"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bodyparser"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"iron 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"persistent 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "conduit-mime-types"
version = "0.7.3"
@@ -228,6 +240,15 @@ dependencies = [
"openssl-sys 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "persistent"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"iron 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pkg-config"
version = "0.3.6"
@@ -359,6 +380,17 @@ dependencies = [
"uuid 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "urlencoded"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bodyparser 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"iron 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"url 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "uuid"
version = "0.1.18"
+4 -2
View File
@@ -1,7 +1,8 @@
[package]
name = "trumpet"
version = "0.0.1"
authors = [ "Brandon Thomas <bt@brand.io,echelon@gmail.com>" ]
authors = [ "Brandon Thomas <bt@brand.io>",
"Brandon Thomas <echelon@gmail.com>" ]
[[bin]]
name = "trumpet"
@@ -9,10 +10,11 @@
[dependencies]
#cpal = "*" # Hardware audio output (for testing); works well with Hound.
hound = "1.0.0" # WAV codec.
hyper = "0.7.*"
iron = "0.2.*"
log = "0.3.*"
hyper = "0.7.*"
router = "0.0.16"
rustc-serialize = "0.3.15"
time = "0.1.*"
urlencoded = "0.2.0"
+66
View File
@@ -0,0 +1,66 @@
// Copyright (c) 2015 Brandon Thomas <bt@brand.io>
use iron::Handler;
use iron::mime::Mime;
use iron::prelude::*;
use iron::status;
use router::Router;
use rustc_serialize::json;
use urlencoded::UrlEncodedQuery;
use std::fs::File;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use words::split_sentence;
const QUERY_PARAM : &'static str = "q";
/// Synthesizes audio from input.
pub struct AudioSynthHandler {
/// Root of where files can be served from.
/// A PathBuf since `Path` can only be created as a borrow.
directory: PathBuf,
}
impl Handler for AudioSynthHandler {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let error = Ok(Response::with((status::Ok, "error")));
let sentence = match req.get_ref::<UrlEncodedQuery>() {
Err(_) => { return error; },
Ok(ref map) => {
match map.get(QUERY_PARAM) {
None => { return error; },
Some(list) => {
match list.get(0) {
None => { return error; },
Some(s) => { s },
}
},
}
},
};
let words = split_sentence(sentence);
/*let words = self.list_files();
let response = json::encode(&words).unwrap();*/
Ok(Response::with((status::Ok, "todo")))
}
}
impl AudioSynthHandler {
pub fn new(directory: &str) -> AudioSynthHandler {
AudioSynthHandler { directory: Path::new(directory).to_path_buf() }
}
// TODO: Return errors.
/// Return a list of words from the audio files in the directory.
fn list_files(&self) -> Vec<String> {
let mut words = Vec::new();
words
}
}
+5
View File
@@ -0,0 +1,5 @@
// Copyright (c) 2015 Brandon Thomas <bt@brand.io>
pub mod audio_synth_handler;
pub mod vocab_list_handler;
File renamed without changes.
+21 -11
View File
@@ -2,8 +2,10 @@ extern crate hound;
extern crate iron;
extern crate router;
extern crate rustc_serialize;
extern crate urlencoded;
pub mod vocab_list_handler;
pub mod handlers;
pub mod words;
use std::env;
use std::fs::File;
@@ -14,9 +16,26 @@ use iron::prelude::*;
use router::Router;
use hound::{WavReader, WavSpec, WavWriter};
use vocab_list_handler::VocabListHandler;
use handlers::vocab_list_handler::VocabListHandler;
use handlers::audio_synth_handler::AudioSynthHandler;
fn main() {
//create_from_argv();
start_server();
}
fn start_server() {
let audio_path = "./sounds/trump";
let mut router = Router::new();
router.get("/speak", AudioSynthHandler::new(audio_path));
router.get("/words", VocabListHandler::new(audio_path));
println!("Starting server...");
Iron::new(router).http("0.0.0.0:9000").unwrap();
}
fn create_from_argv() {
let dictionary = words_from_argv();
// Note: Keeping a list of buffered file readers is stupid and is simply
@@ -43,8 +62,6 @@ fn main() {
let spec = get_spec(&dictionary[0]);
write_file("output.wav", &spec, all_samples);
start_server();
}
fn words_from_argv() -> Vec<String> {
@@ -84,10 +101,3 @@ fn write_file(filename: &str, spec: &WavSpec, samples: Vec<i16>) {
}
}
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();
}
+19
View File
@@ -0,0 +1,19 @@
// Copyright (c) 2015 Brandon Thomas <bt@brand.io>
/// Split a sentence into words. Remove extra padding, etc.
pub fn split_sentence(sentence: &str) -> Vec<String> {
let mut words = Vec::new();
let mut split = sentence.split(" ");
// TODO: Keep only \w, '-', '.', and apostrophe.
for s in split {
let trim = s.trim();
if trim.len() == 0 {
continue;
}
words.push(trim.to_lowercase());
}
words
}