config.rs (4038B)
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}, 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<ApiCfg>, 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 = ApiCfg::parse(cfg.section("magnet-bank-httpd-wire-gateway-api"))?; 62 let revenue = ApiCfg::parse(cfg.section("magnet-bank-httpd-revenue-api"))?; 63 let observability = ApiCfg::parse(cfg.section("cyclos-httpd-observability-api"))?; 64 65 Ok(Self { 66 payto, 67 serve, 68 wire_gateway, 69 revenue, 70 observability, 71 }) 72 } 73 } 74 75 #[derive(Debug, Clone, Copy)] 76 pub enum AccountType { 77 Exchange, 78 Normal, 79 } 80 81 /// taler-magnet-bank worker config 82 pub struct WorkerCfg { 83 pub payto: FullHuPayto, 84 pub frequency: Duration, 85 pub api_url: Url, 86 pub consumer: Token, 87 pub keys_path: String, 88 pub account_type: AccountType, 89 pub ignore_tx_before: Option<Date>, 90 pub ignore_bounces_before: Option<Date>, 91 } 92 93 impl WorkerCfg { 94 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 95 let payto = parse_account_payto(cfg)?; 96 let s = cfg.section("magnet-bank-worker"); 97 Ok(Self { 98 payto, 99 frequency: s.duration("FREQUENCY").require()?, 100 account_type: map_config!(s, "account type", "ACCOUNT_TYPE", 101 "exchange" => { AccountType::Exchange }, 102 "normal" => { AccountType::Normal } 103 ) 104 .require()?, 105 api_url: s.base_url("API_URL").require()?, 106 consumer: Token { 107 key: s.str("CONSUMER_KEY").require()?, 108 secret: s.str("CONSUMER_SECRET").require()?, 109 }, 110 keys_path: s.path("KEYS_FILE").require()?, 111 ignore_tx_before: s.date("IGNORE_TRANSACTIONS_BEFORE").opt()?, 112 ignore_bounces_before: s.date("IGNORE_BOUNCES_BEFORE").opt()?, 113 }) 114 } 115 } 116 117 /// magnet-bank-harness config 118 pub struct HarnessCfg { 119 pub worker: WorkerCfg, 120 pub client_payto: FullHuPayto, 121 } 122 123 impl HarnessCfg { 124 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 125 let worker = WorkerCfg::parse(cfg)?; 126 127 let s = cfg.section("magnet-bank-harness"); 128 let iban: HuIban = s.parse("iban", "IBAN").require()?; 129 let name = s.str("NAME").require()?; 130 131 Ok(Self { 132 worker, 133 client_payto: FullHuPayto::new(iban, &name), 134 }) 135 } 136 }