Skip to content

Commit cc9d26c

Browse files
Add CLI args and env var support
Adds support for configuring the node via CLI arguments and environment variables, allowing runtime overrides of the configuration file. - Added `clap` dependency for argument parsing. - Implemented layered config loading: config file (full set of options) + environment variables + CLI arguments. Env vars and CLI args override values from the config file when present. - Implemented `ConfigBuilder` to handle partial state and merging. - Added comprehensive unit tests for precedence and validation logic. - Updated README with usage instructions and explanation of config precedence. Co-authored-by: moisesPomilio <[email protected]>
1 parent ebef52e commit cc9d26c

File tree

5 files changed

+708
-195
lines changed

5 files changed

+708
-195
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,34 @@ We welcome your feedback and contributions to help shape the future of LDK Serve
3838
### Configuration
3939
Refer `./ldk-server/ldk-server-config.toml` to see available configuration options.
4040

41+
You can configure the node via a TOML file, environment variables, or CLI arguments. All options are optional — values provided via CLI override environment variables, which override the values in the TOML file.
42+
4143
### Building
4244
```
4345
git clone https://github.com/lightningdevkit/ldk-server.git
4446
cargo build
4547
```
4648

4749
### Running
50+
- Using a config file:
4851
```
4952
cargo run --bin ldk-server ./ldk-server/ldk-server-config.toml
5053
```
5154

52-
Interact with the node using CLI:
55+
- Using environment variables (all optional):
56+
```
57+
export LDK_SERVER_NODE_NETWORK=regtest
58+
export LDK_SERVER_NODE_LISTENING_ADDRESS=localhost:3001
59+
export LDK_SERVER_NODE_REST_SERVICE_ADDRESS=127.0.0.1:3002
60+
export LDK_SERVER_NODE_ALIAS=LDK-Server
61+
export LDK_SERVER_BITCOIND_RPC_ADDRESS=127.0.0.1:18443
62+
export LDK_SERVER_BITCOIND_RPC_USER=your-rpc-user
63+
export LDK_SERVER_BITCOIND_RPC_PASSWORD=your-rpc-password
64+
export LDK_SERVER_STORAGE_DIR_PATH=/path/to/storage
65+
cargo run --bin ldk-server
66+
```
67+
68+
- Using CLI arguments (all optional):
5369
```
5470
./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key onchain-receive # To generate onchain-receive address.
5571
./target/debug/ldk-server-cli -b localhost:3002 --api-key your-secret-api-key help # To print help/available commands.

ldk-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ toml = { version = "0.8.9", default-features = false, features = ["parse"] }
2323
chrono = { version = "0.4", default-features = false, features = ["clock"] }
2424
log = "0.4.28"
2525
base64 = { version = "0.21", default-features = false, features = ["std"] }
26+
clap = { version = "4.0.5", default-features = false, features = ["derive", "std", "error-context", "suggestions", "help", "env"] }
2627

2728
# Required for RabittMQ based EventPublisher. Only enabled for `events-rabbitmq` feature.
2829
lapin = { version = "2.4.0", features = ["rustls"], default-features = false, optional = true }

ldk-server/src/main.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use tokio::signal::unix::SignalKind;
2323
use hyper::server::conn::http1;
2424
use hyper_util::rt::TokioIo;
2525

26+
use clap::Parser;
27+
2628
use crate::io::events::event_publisher::EventPublisher;
2729
use crate::io::events::get_event_name;
2830
#[cfg(feature = "events-rabbitmq")]
@@ -34,7 +36,7 @@ use crate::io::persist::{
3436
FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE, PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,
3537
PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
3638
};
37-
use crate::util::config::{load_config, ChainSource};
39+
use crate::util::config::{load_config, ArgsConfig, ChainSource};
3840
use crate::util::logger::ServerLogger;
3941
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
4042
use crate::util::tls::get_or_generate_tls_config;
@@ -47,38 +49,19 @@ use ldk_server_protos::types::Payment;
4749
use log::{error, info};
4850
use prost::Message;
4951
use rand::Rng;
50-
use std::fs;
51-
use std::path::{Path, PathBuf};
52+
use std::path::PathBuf;
5253
use std::sync::Arc;
5354
use std::time::{SystemTime, UNIX_EPOCH};
5455
use tokio::select;
5556

56-
const USAGE_GUIDE: &str = "Usage: ldk-server <config_path>";
57-
5857
fn main() {
59-
let args: Vec<String> = std::env::args().collect();
60-
61-
if args.len() < 2 {
62-
eprintln!("{USAGE_GUIDE}");
63-
std::process::exit(-1);
64-
}
65-
66-
let arg = args[1].as_str();
67-
if arg == "-h" || arg == "--help" {
68-
println!("{}", USAGE_GUIDE);
69-
std::process::exit(0);
70-
}
71-
72-
if fs::File::open(arg).is_err() {
73-
eprintln!("Unable to access configuration file.");
74-
std::process::exit(-1);
75-
}
58+
let args_config = ArgsConfig::parse();
7659

7760
let mut ldk_node_config = Config::default();
78-
let config_file = match load_config(Path::new(arg)) {
61+
let config_file = match load_config(&args_config) {
7962
Ok(config) => config,
8063
Err(e) => {
81-
eprintln!("Invalid configuration file: {}", e);
64+
eprintln!("Invalid configuration: {}", e);
8265
std::process::exit(-1);
8366
},
8467
};

0 commit comments

Comments
 (0)