cards/server/src/main.rs

71 lines
2.1 KiB
Rust
Raw Normal View History

2024-07-30 03:22:32 -04:00
#![feature(if_let_guard)]
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-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;
2024-07-21 00:52:47 -04:00
use tower_http::services::ServeDir;
2024-04-28 04:53:00 -04:00
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2024-04-27 18:22:35 -04:00
pub mod api;
use crate::api::*;
2024-08-04 18:14:10 -04:00
use server::*;
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 application state for use with with_state().
2024-08-01 23:47:28 -04:00
// Main Broadcast Channel
2024-04-28 04:53:00 -04:00
let (tx, _rx) = broadcast::channel(100);
2024-08-05 00:08:29 -04:00
let reserved_names = RwLock::new(HashSet::<String>::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-03 00:42:32 -04:00
let games = RwLock::new(vec![]);
2024-07-29 00:34:17 -04:00
let first_names = load_names("data/first.txt")?;
let last_names = load_names("data/last.txt")?;
2024-04-30 02:28:43 -04:00
let app_state = Arc::new(AppState {
2024-08-05 00:08:29 -04:00
reserved_names,
2024-07-31 21:19:29 -04:00
online_users,
offline_users,
2024-04-30 02:28:43 -04:00
tx,
2024-08-04 03:13:34 -04:00
packs,
packs_meta,
2024-04-30 02:28:43 -04:00
games,
2024-07-27 23:23:26 -04:00
first_names,
last_names,
2024-04-30 02:28:43 -04:00
});
2024-05-04 03:52:53 -04:00
// set routes and apply state
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-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
}