-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::error::Error; | ||
|
||
use http_body_util::{combinators::BoxBody, BodyExt, Full}; | ||
use hyper::body::Bytes; | ||
use hyper::server::conn::http1; | ||
use hyper::service::service_fn; | ||
use hyper::{Request, Response, StatusCode}; | ||
use hyper_util::rt::TokioIo; | ||
|
||
use crate::listener::Listener; | ||
use crate::store::Store; | ||
|
||
type BoxError = Box<dyn std::error::Error + Send + Sync>; | ||
type HTTPResult = Result<Response<BoxBody<Bytes, BoxError>>, BoxError>; | ||
|
||
async fn handle(_store: Store, req: Request<hyper::body::Incoming>) -> HTTPResult { | ||
eprintln!("\n\nreq: {:?}", &req); | ||
Ok(Response::builder() | ||
.status(StatusCode::OK) | ||
.header("Content-Type", "application/json") | ||
.body(full("Hello world.\n".to_string()))?) | ||
} | ||
|
||
pub async fn serve( | ||
store: Store, | ||
addr: &str, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
println!("starting http interface: {:?}", addr); | ||
let mut listener = Listener::bind(addr).await?; | ||
loop { | ||
let (stream, _) = listener.accept().await?; | ||
let io = TokioIo::new(stream); | ||
let store = store.clone(); | ||
tokio::task::spawn(async move { | ||
if let Err(err) = http1::Builder::new() | ||
.serve_connection(io, service_fn(move |req| handle(store.clone(), req))) | ||
.await | ||
{ | ||
// Match against the error kind to selectively ignore `NotConnected` errors | ||
if let Some(std::io::ErrorKind::NotConnected) = err.source().and_then(|source| { | ||
source | ||
.downcast_ref::<std::io::Error>() | ||
.map(|io_err| io_err.kind()) | ||
}) { | ||
// Silently ignore the NotConnected error | ||
} else { | ||
// Handle or log other errors | ||
println!("Error serving connection: {:?}", err); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, BoxError> { | ||
Full::new(chunk.into()) | ||
.map_err(|never| match never {}) | ||
.boxed() | ||
} |