lib.rs (2584B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024, 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 //! Configuration parsing, logging and socket binding shared with the rest of 18 //! the GNU Taler Rust tree. See the README next to this file for what was 19 //! vendored from where. 20 21 use std::path::PathBuf; 22 23 use config::{Config, parser::ConfigSource}; 24 use tracing::error; 25 use tracing_subscriber::util::SubscriberInitExt; 26 27 use crate::log::taler_logger; 28 29 pub mod config; 30 pub mod log; 31 pub mod serve; 32 33 /// Command line arguments every Taler component understands. Flatten this 34 /// into the component's own `clap::Parser` struct with `#[command(flatten)]`. 35 #[derive(clap::Parser, Debug, Clone)] 36 pub struct CommonArgs { 37 /// Specifies the configuration file 38 #[arg(short, long, global = true)] 39 pub config: Option<PathBuf>, 40 41 /// Configure logging to use LOGLEVEL 42 #[arg(short('L'), long, global = true)] 43 pub log: Option<tracing::Level>, 44 45 /// Show logs from all sources 46 #[arg(short, long, global = true)] 47 pub verbose: bool, 48 } 49 50 /// Set up logging, load the configuration and run `app` on a multi-threaded 51 /// tokio runtime. Exits with status 1 after logging the error if either the 52 /// configuration fails to load or `app` returns one. 53 pub fn taler_main( 54 src: ConfigSource, 55 args: CommonArgs, 56 app: impl AsyncFnOnce(&Config) -> Result<(), anyhow::Error>, 57 ) { 58 taler_logger(args.log, args.verbose).init(); 59 let cfg = match Config::load(src, args.config) { 60 Ok(cfg) => cfg, 61 Err(err) => { 62 error!(target: "config", "{}", err); 63 std::process::exit(1); 64 } 65 }; 66 67 // Setup async runtime 68 let runtime = tokio::runtime::Builder::new_multi_thread() 69 .enable_all() 70 .build() 71 .unwrap(); 72 73 // Run app 74 let result = runtime.block_on(app(&cfg)); 75 if let Err(err) = result { 76 error!(target: "cli", "{}", err); 77 std::process::exit(1); 78 } 79 }