Skip to content

Commit 664007c

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 7e55f97 commit 664007c

File tree

5 files changed

+737
-195
lines changed

5 files changed

+737
-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
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.
5571
ldk-server-cli -b localhost:3002 --api-key your-secret-api-key --tls-cert /path/to/tls_cert.pem 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ use tokio::net::TcpListener;
3636
use tokio::select;
3737
use tokio::signal::unix::SignalKind;
3838

39+
use clap::Parser;
40+
3941
use crate::io::events::event_publisher::EventPublisher;
4042
use crate::io::events::get_event_name;
4143
#[cfg(feature = "events-rabbitmq")]
@@ -48,7 +50,7 @@ use crate::io::persist::{
4850
PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
4951
};
5052
use crate::service::NodeService;
51-
use crate::util::config::{load_config, ChainSource};
53+
use crate::util::config::{load_config, ArgsConfig, ChainSource};
5254
use crate::util::logger::ServerLogger;
5355
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
5456
use crate::util::tls::get_or_generate_tls_config;
@@ -110,11 +112,13 @@ fn main() {
110112
std::process::exit(-1);
111113
}
112114

115+
let args_config = ArgsConfig::parse();
116+
113117
let mut ldk_node_config = Config::default();
114-
let config_file = match load_config(&config_path) {
118+
let config_file = match load_config(&args_config) {
115119
Ok(config) => config,
116120
Err(e) => {
117-
eprintln!("Invalid configuration file: {}", e);
121+
eprintln!("Invalid configuration: {}", e);
118122
std::process::exit(-1);
119123
},
120124
};

0 commit comments

Comments
 (0)