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