From f77729e13a26dac9d1085cf07a1eb182b1a815e0 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 25 Aug 2020 19:24:25 -0700 Subject: use log/env_logger for logging --- Cargo.lock | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 25 +++++++++++++++- src/main.rs | 14 +++++---- 3 files changed, 128 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8f0150..1a17a61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,11 +1,31 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +dependencies = [ + "memchr", +] + [[package]] name = "arc-swap" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", +] + [[package]] name = "autocfg" version = "1.0.0" @@ -36,12 +56,27 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b" +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "es-public-proxy" version = "0.1.0" dependencies = [ + "env_logger", "http", "hyper", + "log", "serde", "serde_json", "serde_urlencoded", @@ -174,6 +209,15 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + [[package]] name = "hyper" version = "0.13.7" @@ -387,6 +431,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.7" @@ -402,6 +452,24 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" + [[package]] name = "ryu" version = "1.0.5" @@ -490,6 +558,24 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + [[package]] name = "time" version = "0.1.43" @@ -668,6 +754,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index d0d860e..ca74ea9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,15 @@ [package] name = "es-public-proxy" version = "0.1.0" -authors = ["Bryan Newbold "] edition = "2018" +authors = ["Bryan Newbold "] +license = "AGPL-3+" +#license-file = "LICENSE.AGPLv3.txt" +description = "simple proxy for exposing a read-only subset of elasticsearch API to the public internet" +readme = "README.md" +repository = "https://gitlab.com/bnewbold/es-public-proxy" +keywords = ["elasticsearch", "proxy"] +categories = ["command-line-utilities"] [dependencies] hyper = "0.13" @@ -12,3 +19,19 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_urlencoded = "0.5" toml = "0.4" +log = "0.4" +env_logger = "0.7" + +[profile.release] +lto = true +codegen-units = 1 + +[package.metadata.deb] +maintainer = "Bryan Newbold " +depends = "$auto" +section = "utility" +priority = "optional" +assets = [ + ["target/release/es-public-proxy", "usr/bin/", "755"], + ["es-public-proxy.1", "usr/share/man/man1/", "644"], +] diff --git a/src/main.rs b/src/main.rs index 0250976..0f94a84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,17 +4,20 @@ use std::env; use std::net::SocketAddr; use toml; +#[macro_use] +extern crate log; + use es_public_proxy::{filter_request, ProxyConfig}; async fn upstream_req( req: Request, config: ProxyConfig, ) -> Result, hyper::Error> { - println!("hit: {}", req.uri()); + info!("hit: {}", req.uri()); let parsed = filter_request(req, &config).await; let resp = match parsed { Ok(upstream_req) => { - println!("sending request..."); + debug!("sending request..."); Client::new().request(upstream_req).await? } Err(other) => Response::builder() @@ -23,7 +26,7 @@ async fn upstream_req( .body(serde_json::to_string(&other.to_json()).unwrap().into()) .unwrap(), }; - println!("resp!"); + debug!("resp!"); Ok(resp) } @@ -40,7 +43,7 @@ async fn run_server(config: ProxyConfig) { Some(addr) => addr.parse().unwrap(), }; - println!("Listening on http://{}", addr); + info!("Listening on http://{}", addr); // TODO: possible to avoid cloning config on every connection? let make_svc = make_service_fn(move |_| { @@ -51,7 +54,7 @@ async fn run_server(config: ProxyConfig) { let graceful = serve_future.with_graceful_shutdown(shutdown_signal()); if let Err(e) = graceful.await { - eprintln!("server error: {}", e); + error!("server error: {}", e); } } @@ -103,5 +106,6 @@ fn load_config() -> ProxyConfig { #[tokio::main] async fn main() { let config = load_config(); + env_logger::init(); run_server(config).await; } -- cgit v1.2.3