| 16 | |
| 17 | #[tokio::main(flavor = "current_thread")] |
| 18 | async fn main() -> Result<()> { |
| 19 | debug!("Starting grpc plugin"); |
| 20 | let path = Path::new("lightning-rpc"); |
| 21 | |
| 22 | let directory = std::env::current_dir()?; |
| 23 | let (identity, ca_cert) = tls::init(&directory)?; |
| 24 | |
| 25 | let plugin = match Builder::new(tokio::io::stdin(), tokio::io::stdout()) |
| 26 | .option(options::ConfigOption::new( |
| 27 | "grpc-port", |
| 28 | options::Value::Integer(-1), |
| 29 | "Which port should the grpc plugin listen for incoming connections?", |
| 30 | )) |
| 31 | .configure() |
| 32 | .await? |
| 33 | { |
| 34 | Some(p) => p, |
| 35 | None => return Ok(()), |
| 36 | }; |
| 37 | |
| 38 | let bind_port = match plugin.option("grpc-port") { |
| 39 | Some(options::Value::Integer(-1)) => { |
| 40 | log::info!("`grpc-port` option is not configured, exiting."); |
| 41 | plugin |
| 42 | .disable("`grpc-port` option is not configured.") |
| 43 | .await?; |
| 44 | return Ok(()); |
| 45 | } |
| 46 | Some(options::Value::Integer(i)) => i, |
| 47 | None => return Err(anyhow!("Missing 'grpc-port' option")), |
| 48 | Some(o) => return Err(anyhow!("grpc-port is not a valid integer: {:?}", o)), |
| 49 | }; |
| 50 | |
| 51 | let state = PluginState { |
| 52 | rpc_path: path.into(), |
| 53 | identity, |
| 54 | ca_cert, |
| 55 | }; |
| 56 | |
| 57 | let plugin = plugin.start(state.clone()).await?; |
| 58 | |
| 59 | let bind_addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap(); |
| 60 | |
| 61 | tokio::select! { |
| 62 | _ = plugin.join() => { |
| 63 | // This will likely never be shown, if we got here our |
| 64 | // parent process is exiting and not processing out log |
| 65 | // messages anymore. |
| 66 | debug!("Plugin loop terminated") |
| 67 | } |
| 68 | e = run_interface(bind_addr, state) => { |
| 69 | warn!("Error running grpc interface: {:?}", e) |
| 70 | } |
| 71 | } |
| 72 | Ok(()) |
| 73 | } |
| 74 | |
| 75 | async fn run_interface(bind_addr: SocketAddr, state: PluginState) -> Result<()> { |