58 lines
1.4 KiB
Rust
58 lines
1.4 KiB
Rust
|
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<String>,
|
||
|
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("".to_string()),
|
||
|
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();
|
||
|
}
|