use std::{ sync::Arc, time::Duration, }; use hyper::{ Body, Method, Request, Response, StatusCode, }; use tokio::{ spawn, stream::StreamExt, sync::mpsc, time::interval, }; use tracing::{ info, trace, }; use tracing_subscriber::{ fmt, fmt::format::FmtSpan, EnvFilter, }; use ulid::Ulid; pub struct RelayState { } pub struct HttpService { state: Arc } impl HttpService { pub fn new () -> Self { Self { state: Arc::new (RelayState {}), } } pub async fn serve (&self, port: u16) -> Result <(), hyper::Error> { use std::net::SocketAddr; use hyper::{ server::Server, service::{ make_service_fn, service_fn, }, }; let make_svc = make_service_fn (|_conn| { let state = self.state.clone (); async { Ok::<_, String> (service_fn (move |req| { let state = state.clone (); Self::handle_all (req, state) })) } }); let addr = SocketAddr::from(([127, 0, 0, 1], port)); let server = Server::bind (&addr) .serve (make_svc) ; server.await } async fn handle_all (req: Request , state: Arc ) -> Result , anyhow::Error> { if req.method () == Method::GET { return Self::handle_gets (req, &*state).await; } if req.method () == Method::POST { return Self::handle_posts (req, &*state).await; } Ok::<_, anyhow::Error> (Response::builder () .body (Body::from ("hello\n"))?) } async fn handle_gets (req: Request , state: &RelayState) -> Result , anyhow::Error> { let (mut tx, rx) = mpsc::channel (1); spawn (async move { let id = Ulid::new ().to_string (); trace! ("Downstream {} started", id); Self::handle_downstream (tx).await.ok (); trace! ("Downstream {} ended", id); }); Ok::<_, anyhow::Error> (Response::builder () .body (Body::wrap_stream (rx))?) } async fn handle_posts (req: Request , state: &RelayState) -> Result , anyhow::Error> { let id = Ulid::new ().to_string (); trace! ("Upstream {} started", id); let mut body = req.into_body (); while let Some (Ok (item)) = body.next ().await { println! ("Chunk: {:?}", item); } trace! ("Upstream {} ended", id); Ok::<_, anyhow::Error> (Response::builder () .body (Body::from ("hello\n"))?) } async fn handle_downstream (mut tx: mpsc::Sender >) -> Result <(), anyhow::Error> { let mut int = interval (Duration::from_secs (1)); let mut counter = 0u64; loop { int.tick ().await; tx.send (Ok::<_, anyhow::Error> (format! ("Counter: {}\n", counter))).await?; counter += 1; } Ok (()) } } #[tokio::main] async fn main () -> Result <(), anyhow::Error> { use std::time::Duration; use tokio::{ spawn, time::interval, }; fmt () .with_env_filter (EnvFilter::from_default_env ()) .with_span_events (FmtSpan::CLOSE) .init () ; let service = HttpService::new (); info! ("Starting relay"); Ok (service.serve (4003).await?) }