summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs38
1 files changed, 16 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs
index 2b25f32..0250976 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,13 +1,15 @@
-
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Client, Request, Response, Server, StatusCode};
-use std::net::SocketAddr;
use std::env;
+use std::net::SocketAddr;
use toml;
-use es_public_proxy::{ProxyConfig, filter_request};
+use es_public_proxy::{filter_request, ProxyConfig};
-async fn upstream_req(req: Request<Body>, config: ProxyConfig) -> Result<Response<Body>, hyper::Error> {
+async fn upstream_req(
+ req: Request<Body>,
+ config: ProxyConfig,
+) -> Result<Response<Body>, hyper::Error> {
println!("hit: {}", req.uri());
let parsed = filter_request(req, &config).await;
let resp = match parsed {
@@ -15,13 +17,11 @@ async fn upstream_req(req: Request<Body>, config: ProxyConfig) -> Result<Respons
println!("sending request...");
Client::new().request(upstream_req).await?
}
- Err(other) => {
- Response::builder()
- .status(StatusCode::INTERNAL_SERVER_ERROR)
- .header("Content-Type", "application/json; charset=UTF-8")
- .body(serde_json::to_string(&other.to_json()).unwrap().into())
- .unwrap()
- },
+ Err(other) => Response::builder()
+ .status(StatusCode::INTERNAL_SERVER_ERROR)
+ .header("Content-Type", "application/json; charset=UTF-8")
+ .body(serde_json::to_string(&other.to_json()).unwrap().into())
+ .unwrap(),
};
println!("resp!");
Ok(resp)
@@ -35,7 +35,6 @@ async fn shutdown_signal() {
}
async fn run_server(config: ProxyConfig) {
-
let addr = match &config.bind_addr {
None => SocketAddr::from(([127, 0, 0, 1], 9292)),
Some(addr) => addr.parse().unwrap(),
@@ -46,11 +45,7 @@ async fn run_server(config: ProxyConfig) {
// TODO: possible to avoid cloning config on every connection?
let make_svc = make_service_fn(move |_| {
let inner = config.clone();
- async move {
- Ok::<_, hyper::Error>(service_fn(move |req| {
- upstream_req(req, inner.clone())
- }))
- }
+ async move { Ok::<_, hyper::Error>(service_fn(move |req| upstream_req(req, inner.clone()))) }
});
let serve_future = Server::bind(&addr).serve(make_svc);
let graceful = serve_future.with_graceful_shutdown(shutdown_signal());
@@ -65,7 +60,6 @@ fn usage() -> String {
}
fn load_config() -> ProxyConfig {
-
let args: Vec<String> = env::args().collect();
let args: Vec<&str> = args.iter().map(|x| x.as_str()).collect();
let mut config_path: Option<String> = None;
@@ -73,13 +67,13 @@ fn load_config() -> ProxyConfig {
// first parse CLI arg
match args.as_slice() {
- [_] | [] => {},
+ [_] | [] => {}
[_, "-h"] | [_, "--help"] => {
println!("{}", usage());
std::process::exit(0);
- },
- [_, "--config", p] => { config_path = Some(p.to_string()) },
- [_, "--allow-all-indices"] => { allow_all_indices = true },
+ }
+ [_, "--config", p] => config_path = Some(p.to_string()),
+ [_, "--allow-all-indices"] => allow_all_indices = true,
_ => {
eprintln!("{}", usage());
eprintln!("couldn't parse arguments");