Extremely barebones IRC server project for learning Rust.
#![forbid(unsafe_code)]

use crate::server::Server;
use crate::shutdown::Shutdown;
use crate::signal_guard::SignalGuard;
use log::error;
use log::info;
use std::process::exit;
use tokio::sync::broadcast;

mod server;
mod service;
mod shutdown;
mod signal_guard;

#[tokio::main]
async fn main() {
    log4rs::init_file("log4rs.config.yml", Default::default()).unwrap();

    let version = env!("CARGO_PKG_VERSION");

    info!("MAIN: v{}", version);
    info!("MAIN: START");

    let (shutdown_tx, shutdown_rx) = broadcast::channel(256);

    let run = tokio::spawn(async move {
        let control = Shutdown::new(shutdown_rx);
        let mut server = Server::new(control);

        server.run().await;
    });

    info!("MAIN: PARKED");
    SignalGuard::wait_for_stop(&shutdown_tx).await;

    info!("MAIN: STOP SIGNAL RECEIVED FROM OS");
    info!("MAIN: SEND STOP CONTROL SIGNAL TO SERVER");
    info!("MAIN: WAITING FOR EXIT");

    let run_result = run.await;

    match run_result {
        Ok(_) => {
            info!("MAIN: EXIT SUCCESS");
            exit(exitcode::OK);
        }
        Err(server_fault) => {
            error!("MAIN: EXIT FAILURE = {}", server_fault);
            exit(exitcode::SOFTWARE);
        }
    }
}