diff --git a/Cargo.lock b/Cargo.lock index 2d7862b5a2..812ef4008b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "trumpet" -version = "0.7.0" +version = "0.8.0" dependencies = [ "chrono 0.2.25 (registry+https://github.com/rust-lang/crates.io-index)", "clap 1.5.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -9,12 +9,14 @@ dependencies = [ "iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mount 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "params 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.77 (registry+https://github.com/rust-lang/crates.io-index)", "resolve 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "router 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (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)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)", "urlencoded 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -309,6 +311,15 @@ name = "modifier" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "mount" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sequence_trie 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "multipart" version = "0.7.0" @@ -600,6 +611,11 @@ name = "semver" version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "sequence_trie" +version = "0.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "serde" version = "0.8.11" @@ -625,6 +641,18 @@ dependencies = [ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "staticfile" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mount 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strsim" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index 6a0c48b058..44197a454e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trumpet" - version = "0.7.0" + version = "0.8.0" authors = [ "Brandon Thomas ", "Brandon Thomas " ] @@ -16,12 +16,14 @@ iron = "0.4.*" lazy_static = "0.2.*" log = "0.3.*" + mount = "0.2.*" # Path mounting middleware for Iron params = "0.3.*" regex = "0.1.*" resolve = "0.1.1" router = "0.2.*" rust-crypto = "0.2.*" rustc-serialize = "0.3.15" + staticfile = "0.3.0" # Iron static file serving plugin time = "0.1.*" toml = "0.1.*" urlencoded = "0.4.*" diff --git a/src/main.rs b/src/main.rs index 824a9b35ea..c31006b4df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,12 @@ extern crate clap; extern crate crypto; extern crate hound; extern crate iron; +extern crate mount; extern crate regex; extern crate resolve; extern crate router; extern crate rustc_serialize; +extern crate staticfile; extern crate time; extern crate toml; extern crate urlencoded; @@ -33,7 +35,6 @@ use clap::{App, Arg, ArgMatches}; use config::Config; use handlers::audio_synth_handler::AudioSynthHandler; use handlers::error_filter::ErrorFilter; -use handlers::file_server_handler::FileServerHandler; use handlers::html_handler::HtmlHandler; use handlers::vocab_list_handler::VocabListHandler; use iron::prelude::*; @@ -43,9 +44,11 @@ use lang::dictionary::UniversalDictionary; use lang::parser::Parser; use lang::tokenizer::Tokenizer; use logger::SimpleLogger; +use mount::Mount; use old_dictionary::VocabularyLibrary; use resolve::hostname; use router::Router; +use staticfile::Static; use std::path::Path; use std::sync::Arc; use std::sync::RwLock; @@ -93,33 +96,40 @@ fn get_port(matches: &ArgMatches, default_port: u16) -> u16 { fn start_server(config: &Config, port: u16, synthesizer: Synthesizer) { let audio_path = &config.sound_path.clone().unwrap(); let file_path = "./web"; - let index = "index.html"; let async_synth = Arc::new(RwLock::new(synthesizer)); // TODO: Cross-cutting filter installation - let mut router = Router::new(); - let mut chain = Chain::new(AudioSynthHandler::new(async_synth.clone(), - config.clone())); - chain.link_after(ErrorFilter); - router.get("/speak", chain); - router.get("/words", VocabListHandler::new(audio_path)); + let main_router = { + let synth_handler = AudioSynthHandler::new(async_synth.clone(), config.clone()); + let mut chain = Chain::new(synth_handler); + chain.link_after(ErrorFilter); - // Public index - router.get("/", HtmlHandler::new(config.clone())); + let mut router = Router::new(); - // Old index - router.get("/old", HtmlHandler::new(config.clone())); + // Json Endpoints + router.get("/speak", chain); + router.get("/words", VocabListHandler::new(audio_path)); - // Demo pages - router.get("/demo/:name", HtmlHandler::new(config.clone())); + // Html Pages + router.get("/", HtmlHandler::new(config.clone())); + router.get("/old", HtmlHandler::new(config.clone())); + router.get("/demo/:name", HtmlHandler::new(config.clone())); - // Assets - router.get("/assets/:filename", FileServerHandler::new(file_path, index)); - router.get("/assets/*", FileServerHandler::new(file_path, index)); + router + }; + + let asset_router = { + let handler = Static::new(Path::new(file_path)); + Chain::new(handler) + }; + + let mut mount = Mount::new(); + mount.mount("/assets", asset_router); + mount.mount("/", main_router); info!("Starting server on port {}...", port); - Iron::new(router).http(("0.0.0.0", port)).unwrap(); + Iron::new(mount).http(("0.0.0.0", port)).unwrap(); } fn get_hostname() {