#![feature(if_let_guard)] use anyhow::{Context, Result}; use axum::{routing::get, Router}; use std::{ collections::HashMap, net::SocketAddr, sync::{Arc, RwLock}, }; use tokio::sync::broadcast; use tower_http::services::ServeDir; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; pub mod api; use crate::api::*; use server::*; #[tokio::main] async fn main() -> Result<()> { // stuff for logging tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "server=trace,tower_http=trace,lib=trace".into()), ) .with(tracing_subscriber::fmt::layer()) .init(); // Set up application state for use with with_state(). // Main Broadcast Channel let (tx, _rx) = broadcast::channel(100); let online_users = RwLock::new(HashMap::>>::new()); let offline_users = RwLock::new(HashMap::>>::new()); let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?; let games = RwLock::new(vec![]); let first_names = load_names("data/first.txt")?; let last_names = load_names("data/last.txt")?; let app_state = Arc::new(AppState { online_users, offline_users, tx, packs, packs_meta, games, first_names, last_names, }); // set routes and apply state let app = Router::new() .route("/websocket", get(websocket_connection_handler)) .nest_service("/", ServeDir::new("dist")) .with_state(app_state); // send it let address = "0.0.0.0:3030"; let listener = tokio::net::TcpListener::bind(address) .await .with_context(|| format!("{} is not a valid bind address.", address))?; tracing::info!("listening on {}", listener.local_addr()?); axum::serve( listener, app.into_make_service_with_connect_info::(), ) .await?; Ok(()) }