diff options
author | Alex Auvolat <alex@adnab.me> | 2021-12-07 18:19:51 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-12-07 18:19:51 +0100 |
commit | 489d364676003fa08130689a9f509de7d4df1602 (patch) | |
tree | c3595a10ef94eead74da41101be7bd42ed292c58 /src/http.rs | |
parent | 0682c74e9d5083b43b3f83f8bb1ca747658d1455 (diff) | |
download | tricot-489d364676003fa08130689a9f509de7d4df1602.tar.gz tricot-489d364676003fa08130689a9f509de7d4df1602.zip |
Add support for custom headers
Diffstat (limited to 'src/http.rs')
-rw-r--r-- | src/http.rs | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/src/http.rs b/src/http.rs index 385456a..4731645 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,4 +1,5 @@ use std::sync::Arc; +use std::net::SocketAddr; use anyhow::Result; use log::*; @@ -11,6 +12,34 @@ use crate::consul::Consul; const CHALLENGE_PREFIX: &str = "/.well-known/acme-challenge/"; +pub async fn serve_http( + bind_addr: SocketAddr, + consul: Consul, +) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { + let consul = Arc::new(consul); + // For every connection, we must make a `Service` to handle all + // incoming HTTP requests on said connection. + let make_svc = make_service_fn(|_conn| { + let consul = consul.clone(); + // This is the `Service` that will handle the connection. + // `service_fn` is a helper to convert a function that + // returns a Response into a `Service`. + async move { + Ok::<_, anyhow::Error>(service_fn(move |req: Request<Body>| { + let consul = consul.clone(); + handle(req, consul) + })) + } + }); + + info!("Listening on http://{}", bind_addr); + let server = Server::bind(&bind_addr).serve(make_svc); + + server.await?; + + Ok(()) +} + async fn handle(req: Request<Body>, consul: Arc<Consul>) -> Result<Response<Body>> { let path = req.uri().path(); info!("HTTP request {}", path); @@ -45,31 +74,3 @@ async fn handle(req: Request<Body>, consul: Arc<Consul>) -> Result<Response<Body .body(Body::from(""))?) } } - -pub async fn serve_http(consul: Consul) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { - let consul = Arc::new(consul); - // For every connection, we must make a `Service` to handle all - // incoming HTTP requests on said connection. - let make_svc = make_service_fn(|_conn| { - let consul = consul.clone(); - // This is the `Service` that will handle the connection. - // `service_fn` is a helper to convert a function that - // returns a Response into a `Service`. - async move { - Ok::<_, anyhow::Error>(service_fn(move |req: Request<Body>| { - let consul = consul.clone(); - handle(req, consul) - })) - } - }); - - let addr = ([0, 0, 0, 0], 1080).into(); - - let server = Server::bind(&addr).serve(make_svc); - - println!("Listening on http://{}", addr); - - server.await?; - - Ok(()) -} |