iocaine 2.0.0

The deadliest poison known to AI
Documentation
// SPDX-FileCopyrightText: 2025 Gergely Nagy
// SPDX-FileContributor: Gergely Nagy
//
// SPDX-License-Identifier: MIT

use anyhow::Result;
use clap::Parser;
use figment::{
    providers::{Env, Format, Toml},
    Figment,
};
use figment_file_provider_adapter::FileAdapter;

use iocaine::{
    app::Iocaine,
    cli,
    config::{Config, MetricsLabel},
};

#[tokio::main]
async fn main() -> Result<()> {
    #[cfg(all(feature = "tokio-console", tokio_unstable))]
    console_subscriber::init();
    #[cfg(all(feature = "tokio-console", not(tokio_unstable)))]
    compile_error!("`tokio-console` requires manually enabling the `--cfg tokio_unstable` rust flag during compilation!");

    #[cfg(not(feature = "tokio-console"))]
    tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .with_max_level(tracing::Level::WARN)
        .init();

    let args = cli::Args::parse();
    tracing::debug!(config_file = &args.config_file, "loading configuration");

    let mut config: Config = Figment::new()
        .merge(FileAdapter::wrap(Env::prefixed("IOCAINE_").split("__")))
        .merge(FileAdapter::wrap(Env::prefixed("IOCAINE__").split("__")))
        .merge(FileAdapter::wrap(Toml::file(&args.config_file)))
        .extract()
        .unwrap_or_else(|err| {
            tracing::error!(err = format!("{}", err), "Failed to load configuration");
            panic!(
                "Failed to load configuration from {} (and environment variables): {}",
                &args.config_file, err
            );
        });
    if config.metrics.enable
        && config
            .metrics
            .labels
            .contains(&MetricsLabel::UserAgentGroup)
        && config.metrics.agent_group.is_empty()
    {
        tracing::warn!("UserAgentGroup metrics labels are enabled, but no grouping has been configured. Disabling the label.");
        config
            .metrics
            .labels
            .retain(|label| label != &MetricsLabel::UserAgentGroup);
    }

    let app = Iocaine::new(config)?;
    app.run().await?;

    Ok(())
}