Skip to content

Commit

Permalink
feat: show git commit-hash in /info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 23, 2023
1 parent 6c94150 commit 3526a19
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 86 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# if set to 'dev' env variables from the .env file will be used
MINT_APP_ENV=dev
# connection string for the postgres database
MINT_DB_URL=postgres://postgres:[email protected]/moksha-mint
# the private key of the mint
MINT_PRIVATE_KEY=superprivatesecretkey

# the host and port the mint will listen on int the format https://doc.rust-lang.org/std/net/enum.SocketAddr.html
Expand All @@ -16,6 +18,7 @@ MINT_API_PREFIX=/api
# if set will serve the wallet from the given path
#MINT_SERVE_WALLET_PATH=./flutter/build/web

# optional mint info
MINT_INFO_NAME=moksha-mint
# If set to true the version of the mint crate will be displayed in the mint info
MINT_INFO_VERSION=true
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
POSTGRES_DB: moksha-mint
app:
image: "docker.io/ngutech21/moksha-mint:latest"
#image: "moksha-mint:latest"
#image: "moksha-mint:latest" # for local testing
ports:
- 3338:3338
volumes:
Expand Down
23 changes: 4 additions & 19 deletions moksha-mint/src/bin/moksha-mint.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use mokshamint::{
info::MintInfoSettings,
config::{LightningFeeConfig, MintInfoConfig},
lightning::{
AlbyLightningSettings, LightningType, LnbitsLightningSettings, LndLightningSettings,
StrikeLightningSettings,
},
mint::{LightningFeeConfig, MintBuilder},
mint::MintBuilder,
};
use std::{env, fmt, net::SocketAddr, path::PathBuf};

Expand Down Expand Up @@ -62,18 +62,10 @@ pub async fn main() -> anyhow::Result<()> {
};

let mint_info_settings = envy::prefixed("MINT_INFO_")
.from_env::<MintInfoSettings>()
.from_env::<MintInfoConfig>()
.expect("Please provide mint info");

let fee_config_default = LightningFeeConfig::default();

let fee_config = LightningFeeConfig {
fee_percent: env_or_default("LIGHTNING_FEE_PERCENT", fee_config_default.fee_percent),
fee_reserve_min: env_or_default(
"LIGHTNING_RESERVE_FEE_MIN",
fee_config_default.fee_reserve_min,
),
};
let fee_config = LightningFeeConfig::from_env();

let mint = MintBuilder::new()
.with_mint_info(mint_info_settings)
Expand All @@ -93,13 +85,6 @@ pub async fn main() -> anyhow::Result<()> {
mokshamint::server::run_server(mint?, host_port, serve_wallet_path, api_prefix).await
}

fn env_or_default<T: std::str::FromStr>(key: &str, default: T) -> T {
env::var(key)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(default)
}

#[derive(Debug, PartialEq)]
pub enum AppEnv {
Dev,
Expand Down
90 changes: 90 additions & 0 deletions moksha-mint/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::env;

use serde_derive::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct MintInfoConfig {
pub name: Option<String>,
#[serde(default = "default_version")]
pub version: bool,
pub description: Option<String>,
pub description_long: Option<String>,
pub contact: Option<Vec<Vec<String>>>,
pub motd: Option<String>,
// FIXME add missing fields for v1/info endpoint nut4/nut5 payment_methods, nut4 disabled flag
}

fn default_version() -> bool {
true
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct BuildConfig {
pub commit_hash: Option<String>,
pub build_time: Option<String>,
pub cargo_pkg_version: Option<String>,
}

impl BuildConfig {
pub fn from_env() -> Self {
Self {
commit_hash: env::var("COMMITHASH").ok().map(|s| s.to_string()),
build_time: env::var("BUILDTIME").ok().map(|s| s.to_string()),
cargo_pkg_version: Some(env!("CARGO_PKG_VERSION").to_owned()),
}
}

pub fn full_version(&self) -> String {
format!(
"{}-{}",
self.cargo_pkg_version
.as_ref()
.unwrap_or(&"unknown".to_string()),
self.commit_hash.as_ref().unwrap_or(&"unknown".to_string())
)
}
}

#[derive(Clone, Debug)]
pub struct LightningFeeConfig {
pub fee_percent: f32,
pub fee_reserve_min: u64,
// TODO check if fee_percent is in range
}

impl LightningFeeConfig {
pub fn new(fee_percent: f32, fee_reserve_min: u64) -> Self {
Self {
fee_percent,
fee_reserve_min,
}
}

pub fn from_env() -> Self {
let fee_config_default = LightningFeeConfig::default();

LightningFeeConfig {
fee_percent: env_or_default("LIGHTNING_FEE_PERCENT", fee_config_default.fee_percent),
fee_reserve_min: env_or_default(
"LIGHTNING_RESERVE_FEE_MIN",
fee_config_default.fee_reserve_min,
),
}
}
}

fn env_or_default<T: std::str::FromStr>(key: &str, default: T) -> T {
env::var(key)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(default)
}

impl Default for LightningFeeConfig {
fn default() -> Self {
Self {
fee_percent: 1.0,
fee_reserve_min: 4000,
}
}
}
12 changes: 0 additions & 12 deletions moksha-mint/src/info.rs

This file was deleted.

2 changes: 1 addition & 1 deletion moksha-mint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod config;
pub mod database;
pub mod error;
pub mod info;
pub mod lightning;
pub mod mint;
pub mod model;
Expand Down
44 changes: 13 additions & 31 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use moksha_core::{
};

use crate::{
config::{BuildConfig, LightningFeeConfig, MintInfoConfig},
database::Database,
error::MokshaMintError,
info::MintInfoSettings,
lightning::{AlbyLightning, Lightning, LightningType, LnbitsLightning, StrikeLightning},
model::Invoice,
};
Expand All @@ -25,43 +25,21 @@ pub struct Mint {
pub db: Arc<dyn Database + Send + Sync>,
pub dhke: Dhke,
pub lightning_fee_config: LightningFeeConfig,
pub mint_info: MintInfoSettings, // FIXME use new mint info instead of legacy
}

#[derive(Clone, Debug)]
pub struct LightningFeeConfig {
pub fee_percent: f32,
pub fee_reserve_min: u64,
// TODO check if fee_percent is in range
}

impl LightningFeeConfig {
pub fn new(fee_percent: f32, fee_reserve_min: u64) -> Self {
Self {
fee_percent,
fee_reserve_min,
}
}
}

impl Default for LightningFeeConfig {
fn default() -> Self {
Self {
fee_percent: 1.0,
fee_reserve_min: 4000,
}
}
pub mint_info_config: MintInfoConfig,
pub build_config: BuildConfig,
}

impl Mint {
// FIXME create a new struct for all config-settings
pub fn new(
secret: String,
derivation_path: String,
lightning: Arc<dyn Lightning + Send + Sync>,
lightning_type: LightningType,
db: Arc<dyn Database + Send + Sync>,
lightning_fee_config: LightningFeeConfig,
mint_info: MintInfoSettings,
mint_info_config: MintInfoConfig,
build_config: BuildConfig,
) -> Self {
Self {
lightning,
Expand All @@ -71,7 +49,8 @@ impl Mint {
keyset: MintKeyset::new(&secret, &derivation_path),
db,
dhke: Dhke::new(),
mint_info,
mint_info_config,
build_config,
}
}

Expand Down Expand Up @@ -228,15 +207,15 @@ pub struct MintBuilder {
lightning_type: Option<LightningType>,
db_url: Option<String>,
fee_config: Option<LightningFeeConfig>,
mint_info_settings: Option<MintInfoSettings>,
mint_info_settings: Option<MintInfoConfig>,
}

impl MintBuilder {
pub fn new() -> Self {
Self::default()
}

pub fn with_mint_info(mut self, mint_info: MintInfoSettings) -> MintBuilder {
pub fn with_mint_info(mut self, mint_info: MintInfoConfig) -> MintBuilder {
self.mint_info_settings = Some(mint_info);
self
}
Expand Down Expand Up @@ -304,6 +283,7 @@ impl MintBuilder {
db,
self.fee_config.expect("fee-config not set"),
self.mint_info_settings.unwrap_or_default(),
BuildConfig::from_env(),
))
}
}
Expand Down Expand Up @@ -457,6 +437,7 @@ mod tests {
Arc::new(create_mock_db_get_used_proofs()),
Default::default(),
Default::default(),
Default::default(),
);

let tokens = create_token_from_fixture("token_60.cashu".to_string())?;
Expand Down Expand Up @@ -516,6 +497,7 @@ mod tests {
db,
Default::default(),
Default::default(),
Default::default(),
)
}

Expand Down
Loading

0 comments on commit 3526a19

Please sign in to comment.