use net_pool::{Pool, trace};
use net_relay::RelayExt;
#[tokio::main]
async fn main() {
    init_log();

    if let Err(_e) = run().await {
        trace!("run error:{:?}", _e);
    }
}

async fn run() -> Result<(), net_relay::Error> {
    let mut relay = tcp_relay::Relay::build(|b| {
        let p = tcp_pool::Pool::default();
        p.add_backend("127.0.0.1:3002".parse().unwrap());

        b.pool(p)
            .bind("0.0.0.0:3001")
            .relay_fn(tcp_relay::default_relay_fn)
    })
    .map(|r| Box::new(r) as Box<dyn net_relay::Relay>)?;

    loop {
        tokio::select! {
            res = relay.run() => {
                return res;
            }
        };
    }
}

fn init_log() {
    #[cfg(feature = "tracing")]
    {
        tracing_subscriber::fmt()
            .with_max_level(tracing::Level::TRACE)
            .init();
    }
}
