Skip to content

Commit

Permalink
wip: cont on http interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead committed May 31, 2024
1 parent e8a0795 commit 7eb32de
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/http.rs
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()
}

0 comments on commit 7eb32de

Please sign in to comment.