Skip to content

Commit caf35e1

Browse files
feat(cli): default the base URL to the REST service address
Use the same default REST address in the CLI when config omits `node.rest_service_address` and when `--base-url` is not provided.
1 parent 50d7854 commit caf35e1

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

ldk-server-cli/src/config.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use serde::{Deserialize, Serialize};
1414
const DEFAULT_CONFIG_FILE: &str = "config.toml";
1515
const DEFAULT_CERT_FILE: &str = "tls.crt";
1616
const API_KEY_FILE: &str = "api_key";
17+
pub const DEFAULT_REST_SERVICE_ADDRESS: &str = "127.0.0.1:3536";
1718

1819
pub fn get_default_data_dir() -> Option<PathBuf> {
1920
#[cfg(target_os = "macos")]
@@ -66,6 +67,7 @@ pub struct TlsConfig {
6667

6768
#[derive(Debug, Deserialize)]
6869
pub struct NodeConfig {
70+
#[serde(default = "default_rest_service_address")]
6971
pub rest_service_address: String,
7072
network: String,
7173
}
@@ -99,3 +101,53 @@ pub fn load_config(path: &PathBuf) -> Result<Config, String> {
99101
toml::from_str(&contents)
100102
.map_err(|e| format!("Failed to parse config file '{}': {}", path.display(), e))
101103
}
104+
105+
pub fn resolve_base_url(cli_base_url: Option<String>, config: Option<&Config>) -> String {
106+
cli_base_url
107+
.or_else(|| config.map(|config| config.node.rest_service_address.clone()))
108+
.unwrap_or_else(default_rest_service_address)
109+
}
110+
111+
fn default_rest_service_address() -> String {
112+
DEFAULT_REST_SERVICE_ADDRESS.to_string()
113+
}
114+
115+
#[cfg(test)]
116+
mod tests {
117+
use super::{resolve_base_url, Config, DEFAULT_REST_SERVICE_ADDRESS};
118+
119+
#[test]
120+
fn config_defaults_rest_service_address() {
121+
let config: Config = toml::from_str(
122+
r#"
123+
[node]
124+
network = "regtest"
125+
"#,
126+
)
127+
.unwrap();
128+
129+
assert_eq!(config.node.rest_service_address, DEFAULT_REST_SERVICE_ADDRESS);
130+
}
131+
132+
#[test]
133+
fn resolve_base_url_uses_cli_arg_first() {
134+
let config: Config = toml::from_str(
135+
r#"
136+
[node]
137+
network = "regtest"
138+
rest_service_address = "127.0.0.1:3002"
139+
"#,
140+
)
141+
.unwrap();
142+
143+
assert_eq!(
144+
resolve_base_url(Some("127.0.0.1:4000".to_string()), Some(&config)),
145+
"127.0.0.1:4000"
146+
);
147+
}
148+
149+
#[test]
150+
fn resolve_base_url_falls_back_to_default() {
151+
assert_eq!(resolve_base_url(None, None), DEFAULT_REST_SERVICE_ADDRESS);
152+
}
153+
}

ldk-server-cli/src/main.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use clap::{CommandFactory, Parser, Subcommand};
1313
use clap_complete::{generate, Shell};
1414
use config::{
1515
api_key_path_for_storage_dir, cert_path_for_storage_dir, get_default_api_key_path,
16-
get_default_cert_path, get_default_config_path, load_config,
16+
get_default_cert_path, get_default_config_path, load_config, resolve_base_url,
17+
DEFAULT_REST_SERVICE_ADDRESS,
1718
};
1819
use hex_conservative::DisplayHex;
1920
use ldk_server_client::client::LdkServerClient;
@@ -79,7 +80,13 @@ const DEFAULT_DIR: &str = if cfg!(target_os = "macos") {
7980
override_usage = "ldk-server-cli [OPTIONS] <COMMAND>"
8081
)]
8182
struct Cli {
82-
#[arg(short, long, help = "Base URL of the server. If not provided, reads from config file")]
83+
#[arg(
84+
short,
85+
long,
86+
help = format!(
87+
"Base URL of the server. Defaults to config file or {DEFAULT_REST_SERVICE_ADDRESS}"
88+
)
89+
)]
8390
base_url: Option<String>,
8491

8592
#[arg(short, long, help = format!("API key for authentication. Defaults by reading {DEFAULT_DIR}/[network]/api_key"))]
@@ -570,12 +577,7 @@ async fn main() {
570577
});
571578

572579
// Get base URL from argument then from config file
573-
let base_url =
574-
cli.base_url.or_else(|| config.as_ref().map(|c| c.node.rest_service_address.clone()))
575-
.unwrap_or_else(|| {
576-
eprintln!("Base URL not provided. Use --base-url or ensure config file exists at {DEFAULT_DIR}/config.toml");
577-
std::process::exit(1);
578-
});
580+
let base_url = resolve_base_url(cli.base_url, config.as_ref());
579581

580582
// Get TLS cert path from argument, then from config tls.cert_path, then from storage dir,
581583
// then try default location.

0 commit comments

Comments
 (0)