ironcrypt 0.1.0

A Rust library for secure password hashing, RSA key generation, and managing the encryption and verification of passwords and binary files.
Documentation
use std::env;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Once;
use std::time::Instant;

use metrics::{histogram, counter};
use metrics_exporter_prometheus::PrometheusBuilder;

static INIT: Once = Once::new();

/// Initialise l’exporter Prometheus si activé via les variables d’environnement.
///
/// - `IRONCRYPT_METRICS_ENABLED=true` pour activer
/// - `IRONCRYPT_METRICS_PORT=9000` pour définir le port
pub fn init_metrics() {
    INIT.call_once(|| {
        let enabled = env::var("IRONCRYPT_METRICS_ENABLED")
            .map(|v| v.eq_ignore_ascii_case("true"))
            .unwrap_or(false);

        if !enabled {
            return;
        }

        let port = env::var("IRONCRYPT_METRICS_PORT")
            .ok()
            .and_then(|s| s.parse::<u16>().ok())
            .unwrap_or(9000);

        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), port);

        match PrometheusBuilder::new().with_http_listener(addr).install() {
            Ok(_) => println!("✅ Prometheus exporter actif sur {addr}"),
            Err(e) => eprintln!("❌ Échec du démarrage de l’exporter Prometheus : {e}"),
        }
    });
}

/// Démarre un timer pour mesurer la durée d’une commande.
pub fn metrics_start() -> Instant {
    Instant::now()
}

/// Enregistre les métriques à la fin de l’exécution d’une commande.
pub fn metrics_finish(command: &str, payload_bytes: u64, start: Instant, success: bool) {
    let elapsed = start.elapsed().as_secs_f64();
    let status = if success { "ok" } else { "error" };

    let labels = [("command", command.to_string()), ("status", status.to_string())];
    histogram!("command_duration_seconds", &labels).record(elapsed);
    histogram!("payload_size_bytes", &labels).record(payload_bytes as f64);
    counter!("commands_executed_total", &labels).increment(1);
}