cln-grpc-plugin: Add basic grpc-plugin

This commit is contained in:
Christian Decker 2022-01-20 13:44:28 +01:00 committed by Rusty Russell
parent f3d95530f4
commit a17edeb839
2 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,27 @@
[package]
edition = "2021"
name = "grpc-plugin"
version = "0.1.0"
[dependencies]
anyhow = "1.0"
log = "0.4"
prost = "0.8"
rcgen = { version = "0.8", features = ["pem", "x509-parser"] }
[dependencies.cln-grpc]
path = "../../cln-grpc"
[dependencies.cln-plugin]
path = "../../plugins"
[dependencies.cln-rpc]
path = "../../cln-rpc"
[dependencies.tokio]
features = ["net", "rt-multi-thread"]
version = "1"
[dependencies.tonic]
features = ["tls", "transport"]
version = "^0.5"

View File

@ -0,0 +1,52 @@
use anyhow::{Context, Result};
use cln_grpc::pb::node_server::NodeServer;
use cln_plugin::Builder;
use log::{debug, warn};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
struct PluginState {
rpc_path: PathBuf,
bind_address: SocketAddr,
}
#[tokio::main]
async fn main() -> Result<()> {
debug!("Starting grpc plugin");
let path = Path::new("lightning-rpc");
let addr: SocketAddr = "0.0.0.0:50051".parse().unwrap();
let state = PluginState {
rpc_path: path.into(),
bind_address: addr,
};
let (plugin, i) = Builder::new(state.clone(), tokio::io::stdin(), tokio::io::stdout()).build();
tokio::spawn(async move {
if let Err(e) = run_interface(state).await {
warn!("Error running the grpc interface: {}", e);
}
});
plugin.join().await
}
async fn run_interface(state: PluginState) -> Result<()> {
debug!(
"Connecting to {:?} and serving grpc on {:?}",
&state.rpc_path, &state.bind_address
);
tonic::transport::Server::builder()
.add_service(NodeServer::new(
cln_grpc::Server::new(&state.rpc_path)
.await
.context("creating NodeServer instance")?,
))
.serve(state.bind_address)
.await
.context("serving requests")?;
Ok(())
}