Skip to content

Commit a56821c

Browse files
Add CLI args and env var support
- Added `clap` dependency to `ldk-server` to support passing essential node config via CLI arguments and environment variables. - 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. - Added tests for the new config loading logic. - Updated README with usage instructions and explanation of config precedence. Close #42
1 parent ae2535f commit a56821c

File tree

5 files changed

+594
-174
lines changed

5 files changed

+594
-174
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: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,60 @@ 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):
69+
```
70+
cargo run --bin ldk-server -- \
71+
--node-network regtest \
72+
--node-listening-address localhost:3001 \
73+
--node-rest-service-address 127.0.0.1:3002 \
74+
--node-alias LDK-Server \
75+
--bitcoind-rpc-address 127.0.0.1:18443 \
76+
--bitcoind-rpc-user your-rpc-user \
77+
--bitcoind-rpc-password your-rpc-password \
78+
--storage-dir-path /path/to/storage
79+
```
80+
81+
- Mixed configuration example (config file + overrides):
82+
```
83+
# config file sets listening_address = "localhost:3001"
84+
cargo run --bin ldk-server ./ldk-server/ldk-server-config.toml --node-listening-address localhost:3009
85+
# Result: `localhost:3009` will be used because CLI overrides the config file
86+
```
87+
88+
### Interacting with the Node
89+
90+
Once running, use the CLI client:
5391
```
54-
./target/debug/ldk-server-cli -b localhost:3002 onchain-receive # To generate onchain-receive address.
55-
./target/debug/ldk-server-cli -b localhost:3002 help # To print help/available commands.
92+
# Generate an on-chain receive address
93+
./target/debug/ldk-server-cli -b localhost:3002 onchain-receive
94+
95+
# View commands
96+
./target/debug/ldk-server-cli -b localhost:3002 help
5697
```

ldk-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rusqlite = { version = "0.31.0", features = ["bundled"] }
1818
rand = { version = "0.8.5", default-features = false }
1919
async-trait = { version = "0.1.85", default-features = false }
2020
toml = { version = "0.8.9", default-features = false, features = ["parse"] }
21+
clap = { version = "4.0.5", default-features = false, features = ["derive", "std", "error-context", "suggestions", "help", "env"] }
2122

2223
# Required for RabittMQ based EventPublisher. Only enabled for `events-rabbitmq` feature.
2324
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
@@ -13,6 +13,8 @@ use tokio::signal::unix::SignalKind;
1313
use hyper::server::conn::http1;
1414
use hyper_util::rt::TokioIo;
1515

16+
use clap::Parser;
17+
1618
use crate::io::events::event_publisher::{EventPublisher, NoopEventPublisher};
1719
use crate::io::events::get_event_name;
1820
#[cfg(feature = "events-rabbitmq")]
@@ -24,7 +26,7 @@ use crate::io::persist::{
2426
FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE, PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,
2527
PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
2628
};
27-
use crate::util::config::load_config;
29+
use crate::util::config::{load_config, ArgsConfig};
2830
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
2931
use hex::DisplayHex;
3032
use ldk_node::config::Config;
@@ -36,38 +38,19 @@ use ldk_server_protos::events::{event_envelope, EventEnvelope};
3638
use ldk_server_protos::types::Payment;
3739
use prost::Message;
3840
use rand::Rng;
39-
use std::fs;
40-
use std::path::{Path, PathBuf};
41+
use std::path::PathBuf;
4142
use std::sync::Arc;
4243
use std::time::{SystemTime, UNIX_EPOCH};
4344
use tokio::select;
4445

45-
const USAGE_GUIDE: &str = "Usage: ldk-server <config_path>";
46-
4746
fn main() {
48-
let args: Vec<String> = std::env::args().collect();
49-
50-
if args.len() < 2 {
51-
eprintln!("{USAGE_GUIDE}");
52-
std::process::exit(-1);
53-
}
54-
55-
let arg = args[1].as_str();
56-
if arg == "-h" || arg == "--help" {
57-
println!("{}", USAGE_GUIDE);
58-
std::process::exit(0);
59-
}
60-
61-
if fs::File::open(arg).is_err() {
62-
eprintln!("Unable to access configuration file.");
63-
std::process::exit(-1);
64-
}
47+
let args_config = ArgsConfig::parse();
6548

6649
let mut ldk_node_config = Config::default();
67-
let config_file = match load_config(Path::new(arg)) {
50+
let config_file = match load_config(&args_config) {
6851
Ok(config) => config,
6952
Err(e) => {
70-
eprintln!("Invalid configuration file: {}", e);
53+
eprintln!("Invalid configuration: {}", e);
7154
std::process::exit(-1);
7255
},
7356
};

0 commit comments

Comments
 (0)