diff --git a/Cargo.lock b/Cargo.lock index 43c292aa8..89272ee81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -317,6 +317,7 @@ dependencies = [ "fst 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "fst-levenshtein 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index b179172c9..0fe3f16a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ env_logger = { version = "0.3", default-features = false } fst = "0.3" fst-levenshtein = "0.2" futures = "0.1" +lazy_static = "1.0" serde = "1.0" serde_derive = "1.0" serde_json = "1.0" diff --git a/src/bin/raptor.rs b/src/bin/raptor.rs index 99e07af5c..8bbb77fe2 100644 --- a/src/bin/raptor.rs +++ b/src/bin/raptor.rs @@ -2,6 +2,7 @@ extern crate env_logger; extern crate fst; extern crate fst_levenshtein; extern crate futures; +#[macro_use] extern crate lazy_static; extern crate raptor; extern crate tokio_minihttp; extern crate tokio_proto; @@ -22,8 +23,17 @@ use tokio_service::Service; use raptor::MultiMap; +lazy_static! { + static ref MAP: MultiMap = { + let map = read_to_vec("map.fst").unwrap(); + let values = read_to_vec("values.vecs").unwrap(); + + MultiMap::from_bytes(map, &values).unwrap() + }; +} + struct MainService { - map: MultiMap, + map: &'static MultiMap, } impl Service for MainService { @@ -47,13 +57,15 @@ impl Service for MainService { let lev = Levenshtein::new(&key, 2).unwrap(); let mut body = String::new(); + body.push_str(""); let mut stream = self.map.search(lev).into_stream(); while let Some((key, values)) = stream.next() { let values = &values[..values.len().min(10)]; - body.push_str(&format!("{:?} {:?}\n", key, values)); + body.push_str(&format!("{:?} {:?}
", key, values)); } + body.push_str(""); resp.body(&body); } @@ -75,19 +87,5 @@ fn main() { drop(env_logger::init()); let addr = "0.0.0.0:8080".parse().unwrap(); - TcpServer::new(Http, addr).serve(|| { - - // TODO move the MultiMap construction out of this - // closure, make it global. - // It will permit the server to be multithreaded. - - let map = read_to_vec("map.fst").unwrap(); - let values = read_to_vec("values.vecs").unwrap(); - - let map = MultiMap::from_bytes(map, &values).unwrap(); - - println!("Called Fn here !"); - - Ok(MainService { map }) - }) + TcpServer::new(Http, addr).serve(|| Ok(MainService { map: &MAP })) }