woweb-poc/src/main.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

2023-02-23 11:09:27 +00:00
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};
mod action;
mod actions;
mod ws;
struct AppState {
doc: RwLock<Vec<u16>>,
2023-02-23 11:09:27 +00:00
tx: broadcast::Sender<String>,
}
#[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(Vec::new()),
2023-02-23 11:09:27 +00:00
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<String> = 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();
}