mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 09:54:16 +01:00
cln-grpc-plugin: Add basic grpc-plugin
This commit is contained in:
parent
f3d95530f4
commit
a17edeb839
27
plugins/grpc-plugin/Cargo.toml
Normal file
27
plugins/grpc-plugin/Cargo.toml
Normal 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"
|
52
plugins/grpc-plugin/src/main.rs
Normal file
52
plugins/grpc-plugin/src/main.rs
Normal 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(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user