use std::net::SocketAddr; use std::sync::Arc; use anyhow::Result; use log::*; use http::uri::Authority; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Request, Response, Server, StatusCode, Uri}; use crate::consul::Consul; const CHALLENGE_PREFIX: &str = "/.well-known/acme-challenge/"; pub async fn serve_http(bind_addr: SocketAddr, consul: Consul) -> Result<()> { 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
| { 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, consul: Arc