Skip to content

Commit

Permalink
refactor: allow db to be choosen in config
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Jun 8, 2024
1 parent 63c185e commit 31b901c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ clap = { version = "4.3.14", features = ["env", "default", "derive"]}
anyhow = "1.0.71"
cdk = { git = "https://github.com/cashubtc/cdk", rev = "e1506c4", default-features = false, features = ["mint"] }
cdk-sqlite = { git = "https://github.com/cashubtc/cdk", rev = "e1506c4", default-features = false, features = ["mint"] }
cdk-redb = { git = "https://github.com/cashubtc/cdk", rev = "e1506c4", default-features = false, features = ["mint"] }
# cdk = { path = "../../cdk/sql/crates/cdk", default-features = false, features = ["mint"] }
#cdk-sqlite = { path = "../../cdk/sql/crates/cdk-sqlite", default-features = false, features = ["mint"] }
# cdk-redb = { path = "../../cdk/nut14/crates/cdk-redb" }
# cdk-sqlite = { path = "../../cdk/sql/crates/cdk-sqlite", default-features = false, features = ["mint"] }
# cdk-redb = { path = "../../cdk/nut14/crates/cdk-redb", default-features = false, features = ["mint"] }

bitcoin = { version = "0.30.0", features = ["no-std"] }
serde = "1.0.164"
Expand Down
8 changes: 6 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ last_pay_path = "/tmp/cashu-rs-mint/last_pay.txt"
[contact]
email = "[email protected]"

[database]
# Database engine (sqlite/redb) defaults to sqlite
# engine = "sqlite"

[ln]

# Required ln backend `Cln`, `Ldk`, `Greenlight`
ln_backend = "Cln"
# Required ln backend `cln`, `ldk`, `greenlight`
ln_backend = "cln"

# CLN
cln_path = "/tmp/cashu-rs-mint/lighting/ln_1/regtest/lightning-rpc"
Expand Down
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn last_pay_path() -> String {
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum LnBackend {
#[default]
Cln,
Expand All @@ -86,11 +87,25 @@ pub struct Ln {
pub reserve_fee_min: Amount,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum DatabaseEngine {
#[default]
Sqlite,
Redb,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Database {
pub engine: DatabaseEngine,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Settings {
pub info: Info,
// pub mint_info: MintInfo,
pub ln: Ln,
pub database: Database,
}

impl Settings {
Expand Down
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ use axum::routing::{get, post};
use axum::Router;
use bip39::Mnemonic;
use cdk::amount::Amount;
use cdk::cdk_database::{self, MintDatabase};
use cdk::mint::Mint;
use cdk::nuts::nut02::Id;
use cdk::nuts::{
CheckStateRequest, CheckStateResponse, MeltBolt11Request, MeltBolt11Response,
MintBolt11Request, MintBolt11Response, SwapRequest, SwapResponse, *,
};
use cdk::types::MintQuote;
use cdk_redb::MintRedbDatabase;
use cdk_sqlite::MintSqliteDatabase;
use clap::Parser;
use error::into_response;
Expand All @@ -33,9 +35,10 @@ use tower_http::cors::CorsLayer;
use tracing::{debug, warn};
use utils::unix_time;

pub const CARGO_PKG_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");

use crate::cli::CLIArgs;
use crate::config::DatabaseEngine;

pub const CARGO_PKG_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");

mod cli;
mod config;
Expand Down Expand Up @@ -68,15 +71,21 @@ async fn main() -> anyhow::Result<()> {
None => settings.info.clone().db_path,
};

let localstore = MintSqliteDatabase::new(db_path.to_str().unwrap()).await?;
let localstore: Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync> =
match settings.database.engine {
DatabaseEngine::Sqlite => {
Arc::new(MintSqliteDatabase::new(db_path.to_str().unwrap()).await?)
}
DatabaseEngine::Redb => Arc::new(MintRedbDatabase::new(db_path.to_str().unwrap())?),
};
let mint_info = MintInfo::default();

let mnemonic = Mnemonic::from_str(&settings.info.mnemonic)?;

let mint = Mint::new(
&mnemonic.to_seed_normalized(""),
mint_info,
Arc::new(localstore),
localstore,
Amount::ZERO,
0.0,
)
Expand Down

0 comments on commit 31b901c

Please sign in to comment.