88// licenses.
99
1010use std:: net:: SocketAddr ;
11+ use std:: path:: PathBuf ;
1112use std:: str:: FromStr ;
1213use std:: { fs, io} ;
1314
@@ -19,6 +20,14 @@ use ldk_node::liquidity::LSPS2ServiceConfig;
1920use log:: LevelFilter ;
2021use 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 ) ]
2433pub 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 ) ]
4857pub 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+ ) ]
461475pub struct ArgsConfig {
462476 #[ arg( required = false ) ]
463477 config_file : Option < String > ,
@@ -493,9 +507,15 @@ pub struct ArgsConfig {
493507pub 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) ]
539546mod 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