use std::process::exit;

#[allow(unused)]
use log::{info, debug, warn, error, trace};
use sigma::error::Error;

mod tiny;

const PORT: u16 = 3000;
const BIND: &str = "0.0.0.0";

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .with_ansi(true)
        .init();
    
    info!("Starting TinySigma...");
    let app = tiny::router();
    debug!("Loaded app routes");
    
    let interface = format!("{BIND}:{PORT}");
    debug!("Trying to listen on {}", &interface);
    let listener = match tokio::net::TcpListener::bind(&interface).await {
        Ok(listener) => {
            info!("Listening on {}", &interface);
            listener
        },
        Err(e) => {
            error!("{e:?}");
            error!("Could not listen on {}", &interface);
            exit(Error::TcpBindFailed as i32)
        },
    };
    info!("Starting Axum app server");
    if let Err(e) = axum::serve(listener, app).await {
        error!("{e}");
        error!("Axum app halted unexpectedly");
        exit(Error::AxumAppExited as i32)
    }
}
