Adding Iron/Hyper server (initial work)

This commit is contained in:
Brandon Thomas committed 2015-12-04 04:01:58 -05:00
1 parent 014c15e793
commit e232d20c46
3 files changed
+96 -11

No files matched your search

+7 -11
View File
@@ -1,10 +1,7 @@
[package]
name = "trumpet"
version = "0.0.1"
authors = [
"Brandon Thomas <bt@brand.io,echelon@gmail.com>",
"Scott Adams <scottadams@scottadams.me>",
]
authors = [ "Brandon Thomas <bt@brand.io,echelon@gmail.com>" ]
[[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.*"
+18
View File
@@ -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<i16>) {
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();
}
+71
View File
@@ -0,0 +1,71 @@
// 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 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<Response> {
/*let not_found = Ok(Response::with((status::NotFound, "File not found.")));
let filename = req.extensions.get::<Router>().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::<Mime>().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());
},
}
}
}
}