use axum::routing::get; use axum::Router; use axum_extra::routing::SpaRouter; use std::env; use std::net::SocketAddr; use std::sync::Arc; use tokio::sync::{broadcast, RwLock}; use tower_http::trace::TraceLayer; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use dist_text::text::Text; mod ws; struct AppState { doc: RwLock, tx: broadcast::Sender, } #[tokio::main] async fn main() { tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "woweb_poc=debug,tower_http=debug".into()), ) .with(tracing_subscriber::fmt::layer()) .init(); let (tx, _rx) = broadcast::channel(100); let app_state = Arc::new(AppState { doc: RwLock::new(Text::default()), tx, }); let app = Router::new() .merge(SpaRouter::new("/", "assets").index_file("index.html")) .route("/ws", get(ws::route)) .with_state(app_state) .layer(TraceLayer::new_for_http()); let args: Vec = env::args().collect(); let host = if args.len() > 1 { &args[1] } else { "127.0.0.1:3000" }; let addr = host .parse() .unwrap_or_else(|_| SocketAddr::from(([127, 0, 0, 1], 3000))); tracing::debug!("listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); }