Skip to content

Commit fcbaca3

Browse files
committed
fixup!: check default dir if no config file in args
1 parent 664007c commit fcbaca3

3 files changed

Lines changed: 33 additions & 68 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export LDK_SERVER_STORAGE_DIR_PATH=/path/to/storage
6565
cargo run --bin ldk-server
6666
```
6767

68-
- Using CLI arguments (all optional):
68+
Interact with the node using CLI:
6969
```
7070
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.
7171
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/src/main.rs

Lines changed: 3 additions & 41 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;
@@ -36,8 +37,6 @@ use tokio::net::TcpListener;
3637
use tokio::select;
3738
use tokio::signal::unix::SignalKind;
3839

39-
use clap::Parser;
40-
4140
use crate::io::events::event_publisher::EventPublisher;
4241
use crate::io::events::get_event_name;
4342
#[cfg(feature = "events-rabbitmq")]
@@ -55,10 +54,9 @@ use crate::util::logger::ServerLogger;
5554
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
5655
use crate::util::tls::get_or_generate_tls_config;
5756

58-
const DEFAULT_CONFIG_FILE: &str = "config.toml";
5957
const API_KEY_FILE: &str = "api_key";
6058

61-
fn get_default_data_dir() -> Option<PathBuf> {
59+
pub fn get_default_data_dir() -> Option<PathBuf> {
6260
#[cfg(target_os = "macos")]
6361
{
6462
#[allow(deprecated)] // todo can remove once we update MSRV to 1.87+
@@ -75,50 +73,14 @@ fn get_default_data_dir() -> Option<PathBuf> {
7573
}
7674
}
7775

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

11779
let mut ldk_node_config = Config::default();
11880
let config_file = match load_config(&args_config) {
11981
Ok(config) => config,
12082
Err(e) => {
121-
eprintln!("Invalid configuration: {}", e);
83+
eprintln!("Invalid configuration: {e}");
12284
std::process::exit(-1);
12385
},
12486
};

ldk-server/src/util/config.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// licenses.
99

1010
use std::net::SocketAddr;
11+
use std::path::PathBuf;
1112
use std::str::FromStr;
1213
use std::{fs, io};
1314

@@ -19,6 +20,14 @@ use ldk_node::liquidity::LSPS2ServiceConfig;
1920
use log::LevelFilter;
2021
use serde::{Deserialize, Serialize};
2122

23+
use crate::get_default_data_dir;
24+
25+
const DEFAULT_CONFIG_FILE: &str = "config.toml";
26+
27+
fn get_default_config_path() -> Option<PathBuf> {
28+
get_default_data_dir().map(|data_dir| data_dir.join(DEFAULT_CONFIG_FILE))
29+
}
30+
2231
/// Configuration for LDK Server.
2332
#[derive(Debug)]
2433
pub struct Config {
@@ -44,7 +53,7 @@ pub struct TlsConfig {
4453
pub hosts: Vec<String>,
4554
}
4655

47-
#[derive(Debug, PartialEq)]
56+
#[derive(Debug, PartialEq, Eq)]
4857
pub enum ChainSource {
4958
Rpc { rpc_address: SocketAddr, rpc_user: String, rpc_password: String },
5059
Electrum { server_url: String },
@@ -457,7 +466,12 @@ impl From<LSPS2ServiceTomlConfig> for LSPS2ServiceConfig {
457466
}
458467

459468
#[derive(Parser, Debug)]
460-
#[command(version, about = "LDK Server Configuration", long_about = None)]
469+
#[command(
470+
version,
471+
about = "LDK Server Configuration",
472+
long_about = None,
473+
override_usage = "ldk-server [config_path]"
474+
)]
461475
pub struct ArgsConfig {
462476
#[arg(required = false)]
463477
config_file: Option<String>,
@@ -493,9 +507,15 @@ pub struct ArgsConfig {
493507
pub fn load_config(args: &ArgsConfig) -> io::Result<Config> {
494508
let mut builder = ConfigBuilder::default();
495509

496-
if let Some(path) = &args.config_file {
497-
let content = fs::read_to_string(path).map_err(|e| {
498-
io::Error::new(e.kind(), format!("Failed to read config file '{}': {}", path, e))
510+
let config_file = if let Some(path) = &args.config_file {
511+
Some(PathBuf::from(path))
512+
} else {
513+
get_default_config_path().filter(|path| path.exists())
514+
};
515+
516+
if let Some(path) = config_file {
517+
let content = fs::read_to_string(&path).map_err(|e| {
518+
io::Error::new(e.kind(), format!("Failed to read config file '{:?}': {}", path, e))
499519
})?;
500520
let toml_config: TomlConfig = toml::from_str(&content).map_err(|e| {
501521
io::Error::new(
@@ -505,19 +525,6 @@ pub fn load_config(args: &ArgsConfig) -> io::Result<Config> {
505525
})?;
506526

507527
builder.merge_toml(toml_config);
508-
} else {
509-
#[cfg(any(feature = "events-rabbitmq", feature = "experimental-lsps2-support"))]
510-
return Err(io::Error::new(
511-
io::ErrorKind::InvalidInput,
512-
format!(
513-
"To use the `{}` feature, you must provide a configuration file.",
514-
if cfg!(feature = "events-rabbitmq") {
515-
"events-rabbitmq"
516-
} else {
517-
"experimental-lsps2-support"
518-
}
519-
),
520-
));
521528
}
522529

523530
builder.merge_args(args);
@@ -537,12 +544,13 @@ fn missing_field_err(field: &str) -> io::Error {
537544

538545
#[cfg(test)]
539546
mod tests {
547+
use std::str::FromStr;
548+
540549
use ldk_node::bitcoin::Network;
541550
use ldk_node::lightning::ln::msgs::SocketAddress;
542551

543552
use super::*;
544553
use crate::util::config::{load_config, ArgsConfig};
545-
use std::str::FromStr;
546554
const DEFAULT_CONFIG: &str = r#"
547555
[node]
548556
network = "regtest"
@@ -1064,7 +1072,7 @@ mod tests {
10641072

10651073
#[test]
10661074
#[cfg(feature = "events-rabbitmq")]
1067-
fn test_error_if_rabbitmq_feature_without_config_file() {
1075+
fn test_error_if_rabbitmq_feature_without_valid_config_file() {
10681076
let args_config = ArgsConfig {
10691077
config_file: None,
10701078
node_network: None,
@@ -1081,15 +1089,11 @@ mod tests {
10811089
assert!(result.is_err());
10821090
let err = result.unwrap_err();
10831091
assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1084-
assert_eq!(
1085-
err.to_string(),
1086-
"To use the `events-rabbitmq` feature, you must provide a configuration file."
1087-
);
10881092
}
10891093

10901094
#[test]
10911095
#[cfg(feature = "experimental-lsps2-support")]
1092-
fn test_error_if_lsps2_feature_without_config_file() {
1096+
fn test_error_if_lsps2_feature_without_valid_config_file() {
10931097
let args_config = ArgsConfig {
10941098
config_file: None,
10951099
node_network: None,
@@ -1106,6 +1110,5 @@ mod tests {
11061110
assert!(result.is_err());
11071111
let err = result.unwrap_err();
11081112
assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1109-
assert_eq!(err.to_string(), "To use the `experimental-lsps2-support` feature, you must provide a configuration file.");
11101113
}
11111114
}

0 commit comments

Comments
 (0)