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