config.rs (4082B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025, 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::time::Duration; 18 19 use jiff::civil::Date; 20 use taler_api::{ 21 Serve, 22 config::{ApiCfg, DbCfg, WireGatewayCfg}, 23 }; 24 use taler_common::{ 25 config::{CfgErr, Config}, 26 map_config, 27 }; 28 use url::Url; 29 30 use crate::{FullHuPayto, HuIban, magnet_api::oauth::Token}; 31 32 pub fn parse_db_cfg(cfg: &Config) -> Result<DbCfg, CfgErr> { 33 DbCfg::parse(cfg.section("magnet-bankdb-postgres")) 34 } 35 36 pub fn parse_account_payto(cfg: &Config) -> Result<FullHuPayto, CfgErr> { 37 let s = cfg.section("magnet-bank"); 38 let iban: HuIban = s.parse("iban", "IBAN").require()?; 39 let name = s.str("NAME").require()?; 40 41 Ok(FullHuPayto::new(iban, &name)) 42 } 43 44 /// taler-magnet-bank httpd config 45 pub struct ServeCfg { 46 pub payto: FullHuPayto, 47 pub serve: Serve, 48 pub wire_gateway: Option<WireGatewayCfg>, 49 pub revenue: Option<ApiCfg>, 50 pub observability: Option<ApiCfg>, 51 } 52 53 impl ServeCfg { 54 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 55 let payto = parse_account_payto(cfg)?; 56 57 let s = cfg.section("magnet-bank-httpd"); 58 59 let serve = Serve::parse(&s)?; 60 61 let wire_gateway = 62 WireGatewayCfg::parse(cfg.section("magnet-bank-httpd-wire-gateway-api"))?; 63 let revenue = ApiCfg::parse(cfg.section("magnet-bank-httpd-revenue-api"))?; 64 let observability = ApiCfg::parse(cfg.section("cyclos-httpd-observability-api"))?; 65 66 Ok(Self { 67 payto, 68 serve, 69 wire_gateway, 70 revenue, 71 observability, 72 }) 73 } 74 } 75 76 #[derive(Debug, Clone, Copy)] 77 pub enum AccountType { 78 Exchange, 79 Normal, 80 } 81 82 /// taler-magnet-bank worker config 83 pub struct WorkerCfg { 84 pub payto: FullHuPayto, 85 pub frequency: Duration, 86 pub api_url: Url, 87 pub consumer: Token, 88 pub keys_path: String, 89 pub account_type: AccountType, 90 pub ignore_tx_before: Option<Date>, 91 pub ignore_bounces_before: Option<Date>, 92 } 93 94 impl WorkerCfg { 95 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 96 let payto = parse_account_payto(cfg)?; 97 let s = cfg.section("magnet-bank-worker"); 98 Ok(Self { 99 payto, 100 frequency: s.duration("FREQUENCY").require()?, 101 account_type: map_config!(s, "account type", "ACCOUNT_TYPE", 102 "exchange" => { AccountType::Exchange }, 103 "normal" => { AccountType::Normal } 104 ) 105 .require()?, 106 api_url: s.base_url("API_URL").require()?, 107 consumer: Token { 108 key: s.str("CONSUMER_KEY").require()?, 109 secret: s.str("CONSUMER_SECRET").require()?, 110 }, 111 keys_path: s.path("KEYS_FILE").require()?, 112 ignore_tx_before: s.date("IGNORE_TRANSACTIONS_BEFORE").opt()?, 113 ignore_bounces_before: s.date("IGNORE_BOUNCES_BEFORE").opt()?, 114 }) 115 } 116 } 117 118 /// magnet-bank-harness config 119 pub struct HarnessCfg { 120 pub worker: WorkerCfg, 121 pub client_payto: FullHuPayto, 122 } 123 124 impl HarnessCfg { 125 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 126 let worker = WorkerCfg::parse(cfg)?; 127 128 let s = cfg.section("magnet-bank-harness"); 129 let iban: HuIban = s.parse("iban", "IBAN").require()?; 130 let name = s.str("NAME").require()?; 131 132 Ok(Self { 133 worker, 134 client_payto: FullHuPayto::new(iban, &name), 135 }) 136 } 137 }