wxbox/crates/tiler/src/main.rs
core dd2f25a5e8
Some checks are pending
Verify Latest Dependencies / Verify Latest Dependencies (push) Waiting to run
build and test / wxbox - latest (push) Waiting to run
client work
2025-05-16 19:25:51 -04:00

81 lines
2.3 KiB
Rust

mod config;
mod error;
mod grib2;
mod nexrad;
mod tiles;
mod nexrad_list_response;
use crate::config::Config;
use crate::grib2::{Grib2DataCache, Grib2TileCache, grib2_handler, grib2_metadata};
use crate::nexrad::{NexradDataCache, NexradTileCache, nexrad_handler};
use crate::tiles::{DataId, TileId};
use axum::Router;
use axum::routing::get;
use moka::future::Cache;
use std::env::args;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
use axum::http::Method;
use tower_http::cors::{Any, CorsLayer};
use tracing_subscriber::EnvFilter;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::util::SubscriberInitExt;
use wxbox_grib2::GribMessage;
#[derive(Clone)]
pub struct AppState {
grib2_data_cache: Grib2DataCache,
grib2_tile_cache: Grib2TileCache,
nexrad_data_cache: NexradDataCache,
nexrad_tile_cache: NexradTileCache,
config: Arc<Config>,
}
impl Debug for AppState {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "<AppState>")
}
}
#[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("/nexrad/{source}/{site}/{z}/{x}/{y}", get(nexrad_handler))
.with_state(state)
.layer(cors);
let listener = tokio::net::TcpListener::bind("[::]:3000").await?;
axum::serve(listener, app).await?;
Ok(())
}