cards/server/src/main.rs

107 lines
3.4 KiB
Rust
Raw Normal View History

2024-08-08 05:29:32 -04:00
use crate::game_handler::*;
use crate::message_handler::*;
2024-08-05 01:55:05 -04:00
use crate::websocket::*;
2024-05-05 05:32:16 -04:00
use anyhow::{Context, Result};
2024-07-28 02:50:26 -04:00
use axum::{routing::get, Router};
2024-08-05 01:55:05 -04:00
use server::*;
2024-04-28 04:53:00 -04:00
use std::{
2024-08-05 00:08:29 -04:00
collections::{HashMap, HashSet},
2024-05-03 23:17:39 -04:00
net::SocketAddr,
2024-08-03 00:42:32 -04:00
sync::{Arc, RwLock},
2024-04-28 04:53:00 -04:00
};
use tokio::sync::{broadcast, mpsc};
2024-08-14 21:39:17 -04:00
use tower::ServiceBuilder;
use tower_http::{compression::CompressionLayer, services::ServeDir};
2024-04-28 04:53:00 -04:00
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2024-08-07 05:30:30 -04:00
use user_handler::UserHandler;
2024-08-11 19:51:30 -04:00
use uuid::Uuid;
2024-04-30 02:28:43 -04:00
2024-04-27 02:34:28 -04:00
#[tokio::main]
2024-05-05 05:32:16 -04:00
async fn main() -> Result<()> {
2024-05-04 03:52:53 -04:00
// stuff for logging
2024-04-28 04:53:00 -04:00
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
2024-07-21 02:35:13 -04:00
.unwrap_or_else(|_| "server=trace,tower_http=trace,lib=trace".into()),
2024-04-28 04:53:00 -04:00
)
.with(tracing_subscriber::fmt::layer())
.init();
// Set up state
let (broadcast_tx, _rx) = broadcast::channel(100);
let (users_tx, mut users_rx) = mpsc::channel(100);
let (messages_tx, mut messages_rx) = mpsc::channel(100);
2024-08-08 05:29:32 -04:00
let (games_tx, mut games_rx) = mpsc::channel(100);
let first_names = load_names("data/first.txt")?;
let last_names = load_names("data/last.txt")?;
2024-08-05 00:08:29 -04:00
let reserved_names = RwLock::new(HashSet::<String>::new());
2024-08-17 21:55:15 -04:00
let users_by_id = RwLock::new(HashMap::<Uuid, Arc<RwLock<User>>>::new());
2024-08-03 00:42:32 -04:00
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
let offline_users = RwLock::new(HashMap::<String, Arc<RwLock<User>>>::new());
2024-08-04 03:13:34 -04:00
let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?;
2024-08-06 00:33:37 -04:00
let games = RwLock::new(HashMap::new());
2024-07-29 00:34:17 -04:00
2024-04-30 02:28:43 -04:00
let app_state = Arc::new(AppState {
broadcast_tx,
users_tx,
messages_tx,
2024-08-08 05:29:32 -04:00
games_tx,
first_names,
last_names,
2024-08-05 00:08:29 -04:00
reserved_names,
2024-08-17 21:55:15 -04:00
users_by_id,
2024-07-31 21:19:29 -04:00
online_users,
offline_users,
2024-08-04 03:13:34 -04:00
packs,
packs_meta,
2024-04-30 02:28:43 -04:00
games,
});
2024-08-08 05:29:32 -04:00
// Spawn task to handle incoming messages, also handles outging messages
let message_handler = MessageHandler::new(app_state.clone());
tokio::spawn(async move {
while let Some((addr, message)) = messages_rx.recv().await {
message_handler.handle(addr, message).await;
}
});
2024-08-12 17:14:27 -04:00
// TODO: Make an outgoing message handler handler
2024-08-08 05:29:32 -04:00
// Spawn task to handle User things
2024-08-07 05:30:30 -04:00
let user_handler = UserHandler::new(app_state.clone());
2024-08-07 21:21:33 -04:00
tokio::spawn(async move {
while let Some(message) = users_rx.recv().await {
user_handler.handle(message).await;
}
});
2024-08-08 05:29:32 -04:00
// Spawn task to handle Game things
let game_handler = GameHandler::new(app_state.clone());
tokio::spawn(async move {
while let Some(message) = games_rx.recv().await {
game_handler.handle(message).await;
}
});
// Router
2024-04-28 04:53:00 -04:00
let app = Router::new()
2024-07-22 01:32:09 -04:00
.route("/websocket", get(websocket_connection_handler))
2024-07-20 23:00:19 -04:00
.nest_service("/", ServeDir::new("dist"))
2024-08-14 21:39:17 -04:00
.layer(ServiceBuilder::new().layer(CompressionLayer::new()))
2024-04-28 04:53:00 -04:00
.with_state(app_state);
2024-05-04 03:52:53 -04:00
// send it
2024-05-05 05:32:16 -04:00
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))?;
2024-08-02 02:41:42 -04:00
tracing::info!("listening on {}", listener.local_addr()?);
2024-05-03 23:17:39 -04:00
axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await?;
2024-04-30 02:28:43 -04:00
Ok(())
2024-04-28 04:53:00 -04:00
}