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

use common::log;
use service::DbgateService;

mod config;
mod network;
mod service;

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

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

    Ok(())
}

fn dbgate_service() -> &'static DbgateService {
    SERVICE.get().unwrap()
}
