Skip to content

Commit a4da3ef

Browse files
authored
Merge pull request #145 from joostjager/cli-resolve-api-key-and-cert-from-storage-dir
2 parents 9b33ccd + 1ae3314 commit a4da3ef

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

ldk-server-cli/src/config.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,19 @@ pub fn get_default_api_key_path(network: &str) -> Option<PathBuf> {
4444
get_default_data_dir().map(|path| path.join(network).join(API_KEY_FILE))
4545
}
4646

47+
pub fn api_key_path_for_storage_dir(storage_dir: &str, network: &str) -> PathBuf {
48+
PathBuf::from(storage_dir).join(network).join(API_KEY_FILE)
49+
}
50+
51+
pub fn cert_path_for_storage_dir(storage_dir: &str) -> PathBuf {
52+
PathBuf::from(storage_dir).join(DEFAULT_CERT_FILE)
53+
}
54+
4755
#[derive(Debug, Deserialize)]
4856
pub struct Config {
4957
pub node: NodeConfig,
5058
pub tls: Option<TlsConfig>,
59+
pub storage: Option<StorageConfig>,
5160
}
5261

5362
#[derive(Debug, Deserialize, Serialize)]
@@ -61,6 +70,16 @@ pub struct NodeConfig {
6170
network: String,
6271
}
6372

73+
#[derive(Debug, Deserialize)]
74+
pub struct StorageConfig {
75+
pub disk: Option<DiskConfig>,
76+
}
77+
78+
#[derive(Debug, Deserialize)]
79+
pub struct DiskConfig {
80+
pub dir_path: Option<String>,
81+
}
82+
6483
impl Config {
6584
pub fn network(&self) -> Result<String, String> {
6685
match self.node.network.as_str() {

ldk-server-cli/src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use std::path::PathBuf;
1212
use clap::{CommandFactory, Parser, Subcommand};
1313
use clap_complete::{generate, Shell};
1414
use config::{
15-
get_default_api_key_path, get_default_cert_path, get_default_config_path, load_config,
15+
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,
1617
};
1718
use hex_conservative::DisplayHex;
1819
use ldk_server_client::client::LdkServerClient;
@@ -433,15 +434,22 @@ async fn main() {
433434

434435
let config_path = cli.config.map(PathBuf::from).or_else(get_default_config_path);
435436
let config = config_path.as_ref().and_then(|p| load_config(p).ok());
437+
let storage_dir =
438+
config.as_ref().and_then(|c| c.storage.as_ref()?.disk.as_ref()?.dir_path.as_deref());
436439

437-
// Get API key from argument, then from api_key file
440+
// Get API key from argument, then from api_key file in storage dir, then from default location
438441
let api_key = cli
439442
.api_key
440443
.or_else(|| {
441-
// Try to read from api_key file based on network (file contains raw bytes)
442-
let network = config.as_ref().and_then(|c| c.network().ok()).unwrap_or("bitcoin".to_string());
443-
get_default_api_key_path(&network)
444+
let network =
445+
config.as_ref().and_then(|c| c.network().ok()).unwrap_or("bitcoin".to_string());
446+
storage_dir
447+
.map(|dir| api_key_path_for_storage_dir(dir, &network))
444448
.and_then(|path| std::fs::read(&path).ok())
449+
.or_else(|| {
450+
get_default_api_key_path(&network)
451+
.and_then(|path| std::fs::read(&path).ok())
452+
})
445453
.map(|bytes| bytes.to_lower_hex_string())
446454
})
447455
.unwrap_or_else(|| {
@@ -457,11 +465,15 @@ async fn main() {
457465
std::process::exit(1);
458466
});
459467

460-
// Get TLS cert path from argument, then from config file, then try default location
468+
// Get TLS cert path from argument, then from config tls.cert_path, then from storage dir,
469+
// then try default location.
461470
let tls_cert_path = cli.tls_cert.map(PathBuf::from).or_else(|| {
462471
config
463472
.as_ref()
464473
.and_then(|c| c.tls.as_ref().and_then(|t| t.cert_path.as_ref().map(PathBuf::from)))
474+
.or_else(|| {
475+
storage_dir.map(cert_path_for_storage_dir).filter(|path| path.exists())
476+
})
465477
.or_else(get_default_cert_path)
466478
})
467479
.unwrap_or_else(|| {

0 commit comments

Comments
 (0)