use ogcapi_proxy::{CollectionsProxy, routes};
use tower_http::trace::TraceLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
async fn main() {
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
                // axum logs rejections from built-in extractors with the `axum::rejection`
                // target, at `TRACE` level. `axum::rejection=trace` enables showing those events
                format!(
                    "{}=debug,tower_http=debug,axum::rejection=trace",
                    env!("CARGO_CRATE_NAME")
                )
                .into()
            }),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    let collections_proxy = CollectionsProxy::from(vec![
        (
            "proxied_lakes".to_string(),
            "https://demo.pygeoapi.io/stable/collections/lakes".to_string(),
        ),
        (
            "proxied_dutch_windmills_id".to_string(),
            "https://demo.pygeoapi.io/stable/collections/dutch_windmills".to_string(),
        ),
    ]);

    let router = routes(collections_proxy)
        .await
        .layer(TraceLayer::new_for_http());

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("listening on {}", listener.local_addr().unwrap());
    axum::serve(listener, router).await.unwrap();
}
