aboutsummaryrefslogtreecommitdiff
path: root/src/http.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/http.rs')
-rw-r--r--src/http.rs57
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(())
-}