diff --git a/Cargo.lock b/Cargo.lock index c50ebc25..76e5c1a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,16 @@ name = "antidote" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "atty" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "base64" version = "0.6.0" @@ -96,9 +106,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "env_logger" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "atty 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -286,7 +297,7 @@ dependencies = [ name = "martin" version = "0.1.0" dependencies = [ - "env_logger 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "iron-test 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -538,6 +549,14 @@ name = "redox_syscall" version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.2.5" @@ -664,6 +683,16 @@ dependencies = [ "wincolor 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.5" @@ -832,6 +861,7 @@ dependencies = [ [metadata] "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5" +"checksum atty 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8352656fd42c30a0c3c89d26dea01e3b77c0ab2af18230835c15e2e13cd51859" "checksum base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "96434f987501f0ed4eb336a411e0631ecd1afa11574fe148587adc4ff96143c9" "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" "checksum block-buffer 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1339a1042f5d9f295737ad4d9a6ab6bf81c84a933dba110b9200cd6d1448b814" @@ -844,7 +874,7 @@ dependencies = [ "checksum crypto-mac 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "779015233ac67d65098614aec748ac1c756ab6677fa2e14cf8b37c08dfed1198" "checksum digest 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e5b29bf156f3f4b3c4f610a25ff69370616ae6e0657d416de22645483e72af0a" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" -"checksum env_logger 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5b2c179f215e998e9e22c159f6b43b6fdbeff2e552f61c7d270b1c1a0209b100" +"checksum env_logger 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f98112cf5e30982be3514040871a6f310504a7406ce1936691649353ec0607e9" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum fallible-iterator 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6034a9c9dfce417c7710128d202eef406878cd2fe294e76e2ee05259c9b042d" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" @@ -895,6 +925,7 @@ dependencies = [ "checksum r2d2_postgres 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f8b19a6ea63676566dd11085217fed98866ca833cea7f6f29be8c2244e0560e" "checksum rand 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)" = "512870020642bb8c221bf68baa1b2573da814f6ccfe5c9699b1c303047abe9b1" "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "744554e01ccbd98fff8c457c3b092cd67af62a555a43bfe97ae8a0451f7799fa" "checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e" "checksum rererouter 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6101256368bbe7fcafa77c01b834cebfc41c5832dffb81eee43867abfc4a2621" @@ -910,6 +941,7 @@ dependencies = [ "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum termcolor 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9065bced9c3e43453aa3d56f1e98590b8455b341d2fa191a1090c0dd0b242c75" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963" "checksum tilejson 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2117abf55755175b050f820472ddcd7762d7c60764af8bdfa828734276841847" "checksum time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a15375f1df02096fb3317256ce2cee6a1f42fc84ea5ad5fc8c421cfe40c73098" diff --git a/Cargo.toml b/Cargo.toml index 01393475..5f06b6e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,10 @@ name = "martin" version = "0.1.0" authors = ["Stepan Kuzmin "] +[lib] +name = "martin_lib" +path = "src/martin/lib.rs" + [[bin]] name = "martin" path = "src/main.rs" diff --git a/Dockerfile b/Dockerfile index 8709a779..64b10a2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,5 @@ FROM alpine:latest COPY --from=builder \ /home/rust/src/target/x86_64-unknown-linux-musl/release/martin \ /usr/local/bin/ + CMD /usr/local/bin/martin \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a543c722..1f45c5ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,64 +1,10 @@ extern crate iron; -extern crate regex; -extern crate logger; -extern crate iron_test; -extern crate persistent; -extern crate tilejson; -extern crate rererouter; - -extern crate r2d2; -extern crate r2d2_postgres; - -#[macro_use] extern crate log; extern crate env_logger; - -#[macro_use] extern crate serde_derive; -extern crate serde; -extern crate serde_json; +extern crate martin_lib; +#[macro_use] extern crate log; use std::env; -use logger::Logger; -use persistent::Read; -use rererouter::RouterBuilder; -use iron::prelude::{Iron, Chain}; - -mod db; -mod cors; -mod routes; -mod tileset; - -pub fn app(conn_string: String) -> iron::Chain { - let mut router_builder = RouterBuilder::new(); - router_builder.get(r"/index.json", routes::index); - router_builder.get(r"/(?P[\w|\.]*)\.json", routes::tileset); - router_builder.get(r"/(?P[\w|\.]*)/(?P\d*)/(?P\d*)/(?P\d*).pbf", routes::tile); - let router = router_builder.finalize(); - - let mut chain = Chain::new(router); - - let (logger_before, logger_after) = Logger::new(None); - chain.link_before(logger_before); - - match db::setup_connection_pool(&conn_string, 10) { - Ok(pool) => { - info!("Connected to postgres: {}", conn_string); - let conn = pool.get().unwrap(); - let tilesets = tileset::get_tilesets(conn).unwrap(); - chain.link(Read::::both(tilesets)); - - chain.link(Read::::both(pool)); - }, - Err(error) => { - error!("Can't connect to postgres: {}", error); - std::process::exit(-1); - } - }; - - chain.link_after(cors::Middleware); - chain.link_after(logger_after); - - chain -} +use iron::prelude::Iron; fn main() { env_logger::init(); @@ -66,7 +12,7 @@ fn main() { let conn_string: String = env::var("DATABASE_URL") .expect("DATABASE_URL must be set"); - let chain = app(conn_string); + let chain = martin_lib::chain(conn_string); let port = 3000; let bind_addr = format!("0.0.0.0:{}", port); @@ -75,26 +21,3 @@ fn main() { Err(err) => panic!("{:?}", err), }; } - -#[cfg(test)] -mod tests { - use std::env; - use iron::Headers; - use iron_test::{request, response}; - - use super::app; - - #[test] - fn test_index() { - let conn_string: String = env::var("DATABASE_URL") - .expect("DATABASE_URL must be set"); - - let chain = app(conn_string); - - let headers = Headers::new(); - let response = request::get("http://localhost:3000/index.json", headers, &chain).unwrap(); - - let result_body = response::extract_body_to_bytes(response); - assert_eq!(result_body, b"{}"); - } -} \ No newline at end of file diff --git a/src/cors.rs b/src/martin/cors.rs similarity index 100% rename from src/cors.rs rename to src/martin/cors.rs diff --git a/src/db.rs b/src/martin/db.rs similarity index 100% rename from src/db.rs rename to src/martin/db.rs diff --git a/src/martin/lib.rs b/src/martin/lib.rs new file mode 100644 index 00000000..cdfd97f1 --- /dev/null +++ b/src/martin/lib.rs @@ -0,0 +1,56 @@ +extern crate iron_test; +extern crate iron; +#[macro_use] extern crate log; +extern crate logger; +extern crate persistent; +extern crate r2d2_postgres; +extern crate r2d2; +extern crate regex; +extern crate rererouter; +#[macro_use] extern crate serde_derive; +extern crate serde_json; +extern crate serde; +extern crate tilejson; + +use iron::prelude::Chain; +use logger::Logger; +use persistent::Read; +use rererouter::RouterBuilder; + +mod cors; +mod db; +mod routes; +mod tileset; + +pub fn chain(conn_string: String) -> iron::Chain { + let mut router_builder = RouterBuilder::new(); + router_builder.get(r"/index.json", routes::index); + router_builder.get(r"/(?P[\w|\.]*)\.json", routes::tileset); + router_builder.get(r"/(?P[\w|\.]*)/(?P\d*)/(?P\d*)/(?P\d*).pbf", routes::tile); + let router = router_builder.finalize(); + + let mut chain = Chain::new(router); + + let (logger_before, logger_after) = Logger::new(None); + chain.link_before(logger_before); + + match db::setup_connection_pool(&conn_string, 10) { + Ok(pool) => { + info!("Connected to postgres: {}", conn_string); + let conn = pool.get().unwrap(); + let tilesets = tileset::get_tilesets(conn).unwrap(); + chain.link(Read::::both(tilesets)); + + chain.link(Read::::both(pool)); + }, + Err(error) => { + error!("Can't connect to postgres: {}", error); + std::process::exit(-1); + } + }; + + chain.link_after(cors::Middleware); + chain.link_after(logger_after); + + chain +} diff --git a/src/routes.rs b/src/martin/routes.rs similarity index 100% rename from src/routes.rs rename to src/martin/routes.rs diff --git a/src/tileset.rs b/src/martin/tileset.rs similarity index 96% rename from src/tileset.rs rename to src/martin/tileset.rs index 605fea9d..11ae6f6c 100644 --- a/src/tileset.rs +++ b/src/martin/tileset.rs @@ -85,7 +85,9 @@ pub fn get_tilesets(conn: PostgresConnection) -> Result Ok(tilesets) } -pub fn get_tile(conn: PostgresConnection, tileset: &Tileset, z: &i32, x: &i32, y: &i32) -> Result, Box> { +pub fn get_tile(conn: PostgresConnection, tileset: &Tileset, z: &i32, x: &i32, y: &i32) -> Result, Box> { + debug!("get_tile({}, {}, {})", z, x, y); + let rows = try!(conn.query(&tileset.query, &[&z, &x, &y])); let tile = rows.get(0).get("st_asmvt"); Ok(tile) diff --git a/tests/integration_test.rs b/tests/integration_test.rs new file mode 100644 index 00000000..fea89450 --- /dev/null +++ b/tests/integration_test.rs @@ -0,0 +1,21 @@ +extern crate iron; +extern crate iron_test; +extern crate martin_lib; + +use std::env; +use iron::Headers; +use iron_test::{request, response}; + +#[test] +fn test_index() { + let conn_string: String = env::var("DATABASE_URL") + .expect("DATABASE_URL must be set"); + + let chain = martin_lib::chain(conn_string); + + let headers = Headers::new(); + let response = request::get("http://localhost:3000/index.json", headers, &chain).unwrap(); + + let result_body = response::extract_body_to_bytes(response); + assert_eq!(result_body, b"{}"); +} \ No newline at end of file