use std::{error::Error, sync::OnceLock};

use common::log;
use dispatch_service::DispatchService;

mod config;
mod dispatch_service;
mod gateserver_mgr;
mod network;

static SERVICE: OnceLock<DispatchService> = OnceLock::new();

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    log::init_tracing();
    let config = config::load().unwrap();
    let service = DispatchService::new(config).await?;
    let service = SERVICE.get_or_init(|| service);
    service.run().await;

    Ok(())
}

fn dispatch_service() -> &'static DispatchService {
    SERVICE.get().unwrap()
}
