use std::{convert::Infallible, sync::Arc}; use hyper::{ service::{make_service_fn, service_fn}, Body, Request, Response, Server, StatusCode, }; use tokio::sync::broadcast; use crate::config::HttpConfig; pub struct HttpContext where SF: Fn(String, String) -> anyhow::Result<()>, { pub cfg: HttpConfig, pub sendmsg: SF, } async fn handle(_ctx: Arc>, _req: Request) -> anyhow::Result> where SF: Fn(String, String) -> anyhow::Result<()> + Send + Sync + 'static, { let resp = Response::builder() .status(StatusCode::OK) .body(Body::empty())?; Ok(resp) } pub async fn run(context: HttpContext, mut shutdown: broadcast::Receiver<()>) -> hyper::Result<()> where SF: Fn(String, String) -> anyhow::Result<()> + Send + Sync + 'static, { let ctx = Arc::new(context); let make_service = make_service_fn({ let ctx = ctx.clone(); move |_conn| { let ctx = ctx.clone(); let service = service_fn(move |req| handle(ctx.clone(), req)); async move { Ok::<_, Infallible>(service) } } }); let server = Server::bind(&ctx.cfg.listen).serve(make_service); server .with_graceful_shutdown(async { shutdown.recv().await.unwrap(); }) .await }