Skip to content

Commit 9ff0939

Browse files
authored
Merge pull request #98 from Anyitechs/config-env
2 parents 04e914a + 2ea23f5 commit 9ff0939

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;
@@ -47,15 +48,14 @@ use crate::io::persist::{
4748
PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
4849
};
4950
use crate::service::NodeService;
50-
use crate::util::config::{load_config, ChainSource};
51+
use crate::util::config::{load_config, ArgsConfig, ChainSource};
5152
use crate::util::logger::ServerLogger;
5253
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
5354
use crate::util::tls::get_or_generate_tls_config;
5455

55-
const DEFAULT_CONFIG_FILE: &str = "config.toml";
5656
const API_KEY_FILE: &str = "api_key";
5757

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

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

11278
let mut ldk_node_config = Config::default();
113-
let config_file = match load_config(&config_path) {
79+
let config_file = match load_config(&args_config) {
11480
Ok(config) => config,
11581
Err(e) => {
116-
eprintln!("Invalid configuration file: {}", e);
82+
eprintln!("Invalid configuration: {e}");
11783
std::process::exit(-1);
11884
},
11985
};

0 commit comments

Comments
 (0)