mod config; mod error; mod grib2; mod nexrad; mod nexrad_list_response; mod tiles; use crate::config::Config; use crate::grib2::{Grib2DataCache, Grib2TileCache, grib2_handler, grib2_metadata}; use crate::nexrad::{NexradDataCache, NexradTileCache}; use axum::Router; use axum::http::Method; use axum::routing::get; use moka::future::Cache; use nexrad::nexrad_data_handler; use std::env::args; use std::fmt::{Debug, Formatter}; use std::sync::Arc; use tower_http::cors::{Any, CorsLayer}; use tracing_subscriber::EnvFilter; use tracing_subscriber::fmt::format::FmtSpan; #[derive(Clone)] pub struct AppState { grib2_data_cache: Grib2DataCache, grib2_tile_cache: Grib2TileCache, nexrad_data_cache: NexradDataCache, nexrad_tile_cache: NexradTileCache, config: Arc, } impl Debug for AppState { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "") } } #[tokio::main] async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt::fmt() .with_span_events(FmtSpan::CLOSE) .with_env_filter(EnvFilter::from_default_env()) .init(); let config_file = args().nth(1).expect("usage: wxbox-tiler config.toml"); let config_str = tokio::fs::read_to_string(config_file).await?; let config: Config = toml::from_str(&config_str)?; let grib2_data_cache: Grib2DataCache = Cache::new(10_000); let grib2_tile_cache: Grib2TileCache = Cache::new(10_000); let nexrad_data_cache: NexradDataCache = Cache::new(10_000); let nexrad_tile_cache: NexradTileCache = Cache::new(10_000); let state = AppState { grib2_tile_cache, grib2_data_cache, nexrad_tile_cache, nexrad_data_cache, config: Arc::new(config), }; let cors = CorsLayer::new() .allow_methods([Method::GET]) .allow_origin(Any); let app = Router::new() .route("/grib2/{source}/{z}/{x}/{y}", get(grib2_handler)) .route("/grib2/{source}/metadata", get(grib2_metadata)) .route( "/v2/nexrad/{source}/{site}/{sweep}/{product}", get(nexrad_data_handler), ) .with_state(state) .layer(cors); let listener = tokio::net::TcpListener::bind("[::]:3000").await?; axum::serve(listener, app).await?; Ok(()) }