use salvo::prelude::*;
mod commons;
mod db;
mod protocols;
mod router;
use commons::utils::CONFIG as config;
use db::init_db;

#[tokio::main]
async fn main() {
    if let Err(err) = init_db().await {
        panic!("init db failed: {err}");
    }
    let mut router = Router::new().push(router::config_router());
    if cfg!(debug_assertions) {
        #[derive(rust_embed::RustEmbed)]
        #[folder = "src/web"]
        struct AssetsWeb;
        router = router.push(
            Router::with_path("/web/{**path}")
                .get(salvo::serve_static::static_embed::<AssetsWeb>().fallback("index.html")),
        );
    }
    let host = config.host.clone();
    dbg!(&config);
    println!("http://{}", host);

    let service = Service::new(router).catcher(router::config_catcher());
    let acceptor = TcpListener::new(host).bind().await;
    Server::new(acceptor).serve(service).await;
}
