Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added data_dir configuration options. #192

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions config_spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ optional = true
doc = "The hex encoded macaroon to pass directly into LNDK"

[[param]]
name = "log_dir"
name = "data_dir"
type = "String"
doc = "The path to the lndk log file. By default this is stored in ~/.lndk"
doc = "The path to the lndk data directory. By default this is stored in ~/.lndk"

[[param]]
name = "log_file"
type = "String"
doc = "The path to the lndk log file. If not specified, defaults to `<data_dir>/lndk.log`."

[[param]]
name = "log_level"
Expand Down
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use log4rs::config::{Appender, Config as LogConfig, Logger, Root};
use log4rs::encode::pattern::PatternEncoder;
use rate_limit::RateLimiterCfg;
use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::{Arc, Mutex, Once};
use tokio::time::{sleep, timeout, Duration};
Expand All @@ -62,13 +63,14 @@ pub const DEFAULT_SERVER_HOST: &str = "127.0.0.1";
pub const DEFAULT_SERVER_PORT: u16 = 7000;
pub const LDK_LOGGER_NAME: &str = "ldk";
pub const DEFAULT_DATA_DIR: &str = ".lndk";
pub const DEFAULT_LOG_FILE: &str = "lndk.log";

pub const TLS_CERT_FILENAME: &str = "tls-cert.pem";
pub const TLS_KEY_FILENAME: &str = "tls-key.pem";
pub const DEFAULT_RESPONSE_INVOICE_TIMEOUT: u32 = 15;

#[allow(clippy::result_unit_err)]
pub fn setup_logger(log_level: Option<String>, log_dir: Option<String>) -> Result<(), ()> {
pub fn setup_logger(log_level: Option<String>, log_file: Option<PathBuf>) -> Result<(), ()> {
let log_level = match log_level {
Some(level_str) => match LevelFilter::from_str(&level_str) {
Ok(level) => level,
Expand All @@ -85,22 +87,18 @@ pub fn setup_logger(log_level: Option<String>, log_dir: Option<String>) -> Resul
None => LevelFilter::Trace,
};

let log_dir = log_dir.unwrap_or_else(|| {
let log_file = log_file.unwrap_or_else(|| {
home_dir()
.unwrap()
.join(".lndk")
.join("lndk.log")
.as_path()
.to_str()
.unwrap()
.to_string()
.join(DEFAULT_DATA_DIR)
.join(DEFAULT_LOG_FILE)
});

// Log both to stdout and a log file.
let stdout = ConsoleAppender::builder().build();
let lndk_logs = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new("{d} - {m}{n}")))
.build(log_dir)
.build(log_file)
.unwrap();

let config = LogConfig::builder()
Expand Down
23 changes: 16 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use lndk::lnd::{get_lnd_client, validate_lnd_creds, LndCfg};
use lndk::server::{generate_tls_creds, read_tls, LNDKServer};
use lndk::{
lndkrpc, setup_logger, Cfg, LifecycleSignals, LndkOnionMessenger, OfferHandler,
DEFAULT_DATA_DIR, DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT,
DEFAULT_DATA_DIR, DEFAULT_LOG_FILE, DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT,
};
use lndkrpc::offers_server::OffersServer;
use log::{error, info};
Expand All @@ -37,9 +37,14 @@ async fn main() -> Result<(), ()> {
.unwrap_or_exit()
.0;

let data_dir =
create_data_dir().map_err(|e| println!("Error creating LNDK's data dir {e:?}"))?;
setup_logger(config.log_level, config.log_dir)?;
let data_dir = create_data_dir(&config.data_dir)
.map_err(|e| println!("Error creating LNDK's data dir {e:?}"))?;

let log_file = config.log_file.map(PathBuf::from).or(config
.data_dir
.map(|data_dir| PathBuf::from(data_dir).join(DEFAULT_LOG_FILE)));

setup_logger(config.log_level, log_file)?;

let creds = validate_lnd_creds(
config.cert_path,
Expand Down Expand Up @@ -156,9 +161,13 @@ async fn main() -> Result<(), ()> {
Ok(())
}

// Creates lndk's data directory at ~/.lndk.
fn create_data_dir() -> Result<PathBuf, std::io::Error> {
let path = home_dir().unwrap().join(DEFAULT_DATA_DIR);
// Creates lndk's data directory at the specified directory, or ~/.lndk if not specified.
fn create_data_dir(data_dir: &Option<String>) -> Result<PathBuf, std::io::Error> {
let path = match data_dir {
Some(dir) => PathBuf::from(&dir),
None => home_dir().unwrap().join(DEFAULT_DATA_DIR),
};

create_dir_all(&path)?;

Ok(path)
Expand Down
10 changes: 2 additions & 8 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,8 @@ pub async fn setup_lndk(
let handler = Arc::new(lndk::OfferHandler::default());
let messenger = lndk::LndkOnionMessenger::new();

let log_dir = Some(
lndk_dir
.join(format!("lndk-logs.txt"))
.to_str()
.unwrap()
.to_string(),
);
setup_logger(None, log_dir).unwrap();
let log_file = Some(lndk_dir.join(format!("lndk-logs.txt")));
setup_logger(None, log_file).unwrap();

return (lndk_cfg, handler, messenger, shutdown);
}
Expand Down
20 changes: 4 additions & 16 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,8 @@ async fn test_lndk_send_invoice_request() {
}
}

let log_dir = Some(
lndk_dir
.join(format!("lndk-logs.txt"))
.to_str()
.unwrap()
.to_string(),
);
setup_logger(None, log_dir).unwrap();
let log_file = Some(lndk_dir.join(format!("lndk-logs.txt")));
setup_logger(None, log_file).unwrap();

// Make sure lndk successfully sends the invoice_request.
let handler = Arc::new(lndk::OfferHandler::default());
Expand Down Expand Up @@ -266,14 +260,8 @@ async fn test_lndk_send_invoice_request() {
rate_limit_period_secs: 1,
};

let log_dir = Some(
lndk_dir
.join(format!("lndk-logs.txt"))
.to_str()
.unwrap()
.to_string(),
);
setup_logger(None, log_dir).unwrap();
let log_file = Some(lndk_dir.join(format!("lndk-logs.txt")));
setup_logger(None, log_file).unwrap();

let handler = Arc::new(lndk::OfferHandler::default());
let messenger = lndk::LndkOnionMessenger::new();
Expand Down