Skip to content

Commit 2ea23f5

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 <93723302+moisesPompilio@users.noreply.github.com>
1 parent 7e55f97 commit 2ea23f5

5 files changed

Lines changed: 772 additions & 229 deletions

File tree

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,33 @@ 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

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+
5268
Interact with the node using CLI:
5369
```
5470
ldk-server-cli -b localhost:3002 --api-key your-secret-api-key --tls-cert /path/to/tls_cert.pem onchain-receive # To generate onchain-receive address.

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: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::path::{Path, PathBuf};
1818
use std::sync::Arc;
1919
use std::time::{SystemTime, UNIX_EPOCH};
2020

21+
use clap::Parser;
2122
use hex::DisplayHex;
2223
use hyper::server::conn::http1;
2324
use hyper_util::rt::TokioIo;
@@ -48,15 +49,14 @@ use crate::io::persist::{
4849
PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
4950
};
5051
use crate::service::NodeService;
51-
use crate::util::config::{load_config, ChainSource};
52+
use crate::util::config::{load_config, ArgsConfig, ChainSource};
5253
use crate::util::logger::ServerLogger;
5354
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
5455
use crate::util::tls::get_or_generate_tls_config;
5556

56-
const DEFAULT_CONFIG_FILE: &str = "config.toml";
5757
const API_KEY_FILE: &str = "api_key";
5858

59-
fn get_default_data_dir() -> Option<PathBuf> {
59+
pub fn get_default_data_dir() -> Option<PathBuf> {
6060
#[cfg(target_os = "macos")]
6161
{
6262
#[allow(deprecated)] // todo can remove once we update MSRV to 1.87+
@@ -73,48 +73,14 @@ fn get_default_data_dir() -> Option<PathBuf> {
7373
}
7474
}
7575

76-
fn get_default_config_path() -> Option<PathBuf> {
77-
get_default_data_dir().map(|data_dir| data_dir.join(DEFAULT_CONFIG_FILE))
78-
}
79-
80-
const USAGE_GUIDE: &str = "Usage: ldk-server [config_path]
81-
82-
If no config path is provided, ldk-server will look for a config file at:
83-
Linux: ~/.ldk-server/config.toml
84-
macOS: ~/Library/Application Support/ldk-server/config.toml
85-
Windows: %APPDATA%\\ldk-server\\config.toml";
86-
8776
fn main() {
88-
let args: Vec<String> = std::env::args().collect();
89-
90-
let config_path: PathBuf = if args.len() < 2 {
91-
match get_default_config_path() {
92-
Some(path) => path,
93-
None => {
94-
eprintln!("Unable to determine home directory for default config path.");
95-
eprintln!("{USAGE_GUIDE}");
96-
std::process::exit(-1);
97-
},
98-
}
99-
} else {
100-
let arg = args[1].as_str();
101-
if arg == "-h" || arg == "--help" {
102-
println!("{USAGE_GUIDE}");
103-
std::process::exit(0);
104-
}
105-
PathBuf::from(arg)
106-
};
107-
108-
if fs::File::open(&config_path).is_err() {
109-
eprintln!("Unable to access configuration file: {}", config_path.display());
110-
std::process::exit(-1);
111-
}
77+
let args_config = ArgsConfig::parse();
11278

11379
let mut ldk_node_config = Config::default();
114-
let config_file = match load_config(&config_path) {
80+
let config_file = match load_config(&args_config) {
11581
Ok(config) => config,
11682
Err(e) => {
117-
eprintln!("Invalid configuration file: {}", e);
83+
eprintln!("Invalid configuration: {e}");
11884
std::process::exit(-1);
11985
},
12086
};

0 commit comments

Comments
 (0)