taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

main.rs (4738B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 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 clap::Parser as _;
     18 use taler_api::api::TalerRouter as _;
     19 use taler_build::long_version;
     20 use taler_common::{
     21     CommonArgs, cli::ConfigCmd, config::Config, taler_main, types::validate_base_url,
     22 };
     23 use taler_wise::{
     24     api,
     25     config::{ServeCfg, SubscribeWebhooksCfg},
     26     constants::{CONFIG_SOURCE, WEBHOOK_PATH},
     27     db::{dbinit, pool},
     28     setup::{self},
     29     wise_api::client::Client,
     30     worker::run_worker,
     31 };
     32 use url::Url;
     33 
     34 #[derive(clap::Parser, Debug)]
     35 #[command(long_version = long_version(), about, long_about = None)]
     36 struct Args {
     37     #[clap(flatten)]
     38     common: CommonArgs,
     39 
     40     #[command(subcommand)]
     41     cmd: Command,
     42 }
     43 
     44 #[derive(clap::Subcommand, Debug)]
     45 enum Command {
     46     /// Initialize taler-wise database
     47     Dbinit {
     48         /// Reset database (DANGEROUS: All existing data is lost)
     49         #[arg(short, long)]
     50         reset: bool,
     51     },
     52     /// Check configuration and initialize balance state
     53     Setup {
     54         /// Reset all synchronization cursors and schedules to rescan from balance creation
     55         #[arg(long)]
     56         reset: bool,
     57     },
     58     /// Run taler-wise worker
     59     Worker {
     60         /// Execute once and return
     61         #[arg(short, long)]
     62         transient: bool,
     63     },
     64     /// Run taler-wise HTTP server
     65     Serve {
     66         /// Check whether an API is in use (if it's useful to start the HTTP
     67         /// server). Exit with 0 if at least one API is enabled, otherwise 1
     68         #[arg(long)]
     69         check: bool,
     70     },
     71     #[command(subcommand)]
     72     TalerDeployment(TalerDeployment),
     73     #[command(subcommand)]
     74     Config(ConfigCmd),
     75 }
     76 
     77 /// Helpers to deploy taler-wise
     78 #[derive(clap::Subcommand, Debug)]
     79 enum TalerDeployment {
     80     /// Subscribe to webhooks for instant registration
     81     SubscribeWebhooks {
     82         /// Base URL of taler-wise
     83         #[arg(value_name = "BASE_URL", value_parser = webhook_url)]
     84         callback_url: Url,
     85     },
     86 }
     87 
     88 fn webhook_url(prefix: &str) -> Result<Url, String> {
     89     let url = Url::parse(prefix).map_err(|e| e.to_string())?;
     90     validate_base_url(&url)?;
     91     url.join(WEBHOOK_PATH.trim_start_matches('/'))
     92         .map_err(|e| e.to_string())
     93 }
     94 
     95 async fn run(cmd: Command, cfg: &Config) -> anyhow::Result<()> {
     96     match cmd {
     97         Command::Dbinit { reset } => {
     98             dbinit(cfg, reset).await?;
     99         }
    100         Command::Setup { reset } => {
    101             let pool = pool(cfg).await?;
    102             let client = http_client::client();
    103             setup::setup(cfg, &pool, &client, reset).await?
    104         }
    105         Command::Serve { check } => {
    106             if check {
    107                 ServeCfg::parse(cfg)?;
    108             } else {
    109                 let pool = pool(cfg).await?;
    110                 let cfg = ServeCfg::parse(cfg)?;
    111                 api::start(
    112                     pool,
    113                     &cfg.name,
    114                     cfg.balances,
    115                     cfg.api.public_key(),
    116                     cfg.profile_id,
    117                     &cfg.wire_gateway,
    118                     &cfg.revenue,
    119                     &cfg.observability,
    120                 )
    121                 .await
    122                 .serve(&cfg.serve, None)
    123                 .await?;
    124             }
    125         }
    126         Command::Worker { transient } => {
    127             let pool = pool(cfg).await?;
    128             let client = http_client::client();
    129             run_worker(cfg, &pool, &client, transient).await?;
    130         }
    131         Command::Config(cmd) => cmd.run(cfg)?,
    132         Command::TalerDeployment(cmd) => match cmd {
    133             TalerDeployment::SubscribeWebhooks { callback_url } => {
    134                 let cfg = SubscribeWebhooksCfg::parse(cfg)?;
    135                 let http = http_client::client();
    136                 let id = Client::new(&http, cfg.api, &cfg.token)
    137                     .subscribe_balance_updates(cfg.profile_id, &callback_url)
    138                     .await?;
    139                 println!("{id}");
    140             }
    141         },
    142     }
    143     Ok(())
    144 }
    145 
    146 fn main() {
    147     let args = Args::parse();
    148     taler_main(CONFIG_SOURCE, args.common, async |cfg| {
    149         run(args.cmd, cfg).await
    150     });
    151 }