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

Configuration cleanup #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 14 additions & 4 deletions config_spec.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
[general]
env_prefix = "HELIPAD"
conf_file_param = "conf"
conf_dir_param = "conf_dir"

[[param]]
name = "database_dir"
type = "String"
type = "std::path::PathBuf"
doc = "The location of the database file."
default = "std::path::PathBuf::from(\"database.db\")"

[[param]]
name = "macaroon"
type = "String"
type = "std::path::PathBuf"
doc = "The location of the macaroon file."
default = "std::path::PathBuf::from(\"/lnd/data/chain/bitcoin/mainnet/admin.macaroon\")"

[[param]]
name = "cert"
type = "String"
type = "std::path::PathBuf"
doc = "The location of the tls certificate file."
default = "std::path::PathBuf::from(\"/lnd/tls.cert\")"

[[param]]
name = "listen_port"
type = "u16"
doc = "The port to listen on."
default = "2112"

[[param]]
name = "lnd_url"
type = "String"
doc = "The url and port of the LND grpc api."
doc = "The url and port of the LND grpc api."
default = "\"https://127.0.0.1:10009\".to_owned()"
31 changes: 16 additions & 15 deletions dbif/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::error::Error;
use std::fmt;
use serde::{Deserialize, Serialize};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;


#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -31,50 +32,50 @@ impl fmt::Display for HydraError {
impl Error for HydraError {}


fn connect_to_database(init: bool, filepath: &String) -> Result<Connection, Box<dyn Error>> {
if let Ok(conn) = Connection::open(filepath.as_str()) {
fn connect_to_database(init: bool, filepath: &Path) -> Result<Connection, Box<dyn Error>> {
if let Ok(conn) = Connection::open(filepath) {
if init {
match set_database_file_permissions(filepath.as_str()) {
match set_database_file_permissions(filepath) {
Ok(_) => {},
Err(e) => {
eprintln!("{:#?}", e);
}
}
println!("Using database file: [{}]", filepath.as_str());
println!("Using database file: [{}]", filepath.display());
}
Ok(conn)
} else {
return Err(Box::new(HydraError(format!("Could not open a database file at: [{}].", filepath).into())))
return Err(Box::new(HydraError(format!("Could not open a database file at: [{}].", filepath.display()).into())))
}
}


//Set permissions on the database file
fn set_database_file_permissions(filepath: &str) -> Result<bool, Box<dyn Error>> {
fn set_database_file_permissions(filepath: &Path) -> Result<bool, Box<dyn Error>> {

match std::fs::File::open(filepath) {
Ok(fh) => {
match fh.metadata() {
Ok(metadata) => {
let mut perms = metadata.permissions();
perms.set_mode(0o666);
println!("Set file permission to: [666] on database file: [{}]", filepath);
println!("Set file permission to: [666] on database file: [{}]", filepath.display());
Ok(true)
},
Err(e) => {
return Err(Box::new(HydraError(format!("Error getting metadata from database file handle: [{}]. Error: {:#?}.", filepath, e).into())))
return Err(Box::new(HydraError(format!("Error getting metadata from database file handle: [{}]. Error: {:#?}.", filepath.display(), e).into())))
}
}
},
Err(e) => {
return Err(Box::new(HydraError(format!("Error opening database file handle: [{}] for permissions setting. Error: {:#?}.", filepath, e).into())))
return Err(Box::new(HydraError(format!("Error opening database file handle: [{}] for permissions setting. Error: {:#?}.", filepath.display(), e).into())))
}
}
}


//Create a new database file if needed
pub fn create_database(filepath: &String) -> Result<bool, Box<dyn Error>> {
pub fn create_database(filepath: &Path) -> Result<bool, Box<dyn Error>> {
let conn = connect_to_database(true, filepath)?;

match conn.execute(
Expand All @@ -98,14 +99,14 @@ pub fn create_database(filepath: &String) -> Result<bool, Box<dyn Error>> {
}
Err(e) => {
eprintln!("{}", e);
return Err(Box::new(HydraError(format!("Failed to create database: [{}].", filepath).into())))
return Err(Box::new(HydraError(format!("Failed to create database: [{}].", filepath.display()).into())))
}
}
}


//Add an invoice to the database
pub fn add_invoice_to_db(filepath: &String, boost: BoostRecord) -> Result<bool, Box<dyn Error>> {
pub fn add_invoice_to_db(filepath: &Path, boost: BoostRecord) -> Result<bool, Box<dyn Error>> {
let conn = connect_to_database(false, filepath)?;

match conn.execute("INSERT INTO boosts (idx, time, value_msat, value_msat_total, action, sender, app, message, podcast, episode, tlv) \
Expand Down Expand Up @@ -134,7 +135,7 @@ pub fn add_invoice_to_db(filepath: &String, boost: BoostRecord) -> Result<bool,


//Get all of the boosts from the database
pub fn get_boosts_from_db(filepath: &String, index: u64, max: u64, direction: bool) -> Result<Vec<BoostRecord>, Box<dyn Error>> {
pub fn get_boosts_from_db(filepath: &Path, index: u64, max: u64, direction: bool) -> Result<Vec<BoostRecord>, Box<dyn Error>> {
let conn = connect_to_database(false, filepath)?;
let mut boosts: Vec<BoostRecord> = Vec::new();

Expand Down Expand Up @@ -189,7 +190,7 @@ pub fn get_boosts_from_db(filepath: &String, index: u64, max: u64, direction: bo


//Get the last boost index number from the database
pub fn get_last_boost_index_from_db(filepath: &String) -> Result<u64, Box<dyn Error>> {
pub fn get_last_boost_index_from_db(filepath: &Path) -> Result<u64, Box<dyn Error>> {
let conn = connect_to_database(false, filepath)?;
let mut boosts: Vec<BoostRecord> = Vec::new();
let max = 1;
Expand Down Expand Up @@ -225,4 +226,4 @@ pub fn get_last_boost_index_from_db(filepath: &String) -> Result<u64, Box<dyn Er
}

Ok(0)
}
}
Loading