novax_grpc/
lib.rs

1use tokio::sync::oneshot;
2use tokio::task::JoinHandle;
3use std::error::Error;
4use tonic::transport::server::Router;
5
6
7pub async fn grpc_svc<F>(srv_addr: String, f: F, 
8    router: Router) -> Result<(), Box::<dyn Error> > 
9    where F: core::future::Future<Output = ()>, 
10{
11    let addr: std::net::SocketAddr = srv_addr.parse()?;
12    match router.serve_with_shutdown(addr, f).await {
13        Ok(_) => Ok(()),
14        Err(e) => Err(  e.into() )
15    }
16}
17
18pub fn ctrl_c_handler() -> Result::<(JoinHandle::<bool>, oneshot::Receiver::<bool>), std::io::Error> {
19    let (tx, rx) = oneshot::channel::<bool>();
20    Ok (
21        (
22            tokio::task::spawn_blocking(
23                move || {
24                    let _ = tokio::spawn(async move{
25                        tokio::signal::ctrl_c().await?;
26                        let _ = tx.send(true);
27                        Ok::<(), std::io::Error>(())
28                    });
29                    true
30                }
31            ),
32            rx
33        )
34    )
35}
36
37// re-export
38pub use tokio;
39pub use tonic;