cards/server/src/main.rs

105 lines
2.9 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-07-21 02:35:13 -04:00
use lib::models::*;
2024-04-28 04:53:00 -04:00
use std::{
2024-07-27 02:51:04 -04:00
collections::HashMap,
2024-07-27 23:23:26 -04:00
fs::{read_to_string, File},
io::{BufRead, BufReader},
2024-05-03 23:17:39 -04:00
net::SocketAddr,
2024-04-28 04:53:00 -04:00
sync::{Arc, Mutex},
};
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-04-06 22:38:00 -04:00
/// Parse json for card data
2024-07-29 00:34:17 -04:00
fn load_cards_from_json(path: &str) -> Result<Vec<CardSet>> {
2024-05-05 05:32:16 -04:00
let data: String =
2024-07-27 23:23:26 -04:00
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
2024-07-21 22:48:15 -04:00
let jayson: Vec<CardSet> =
2024-05-05 05:32:16 -04:00
serde_json::from_str(&data).with_context(|| format!("\"{path}\" is invalid json"))?;
2024-04-05 22:38:41 -04:00
Ok(jayson)
}
2024-07-29 00:34:17 -04:00
/// Parse name list
fn load_names(path: &str) -> Result<Vec<String>> {
let f = File::open(path).with_context(|| format!("Invalid names path: \"{}\"", path))?;
2024-07-27 23:23:26 -04:00
let f = BufReader::new(f);
let mut buf = vec![];
for line in f.lines() {
2024-07-29 00:34:17 -04:00
buf.push(line?)
2024-07-27 23:23:26 -04:00
}
2024-07-29 00:34:17 -04:00
Ok(buf)
2024-04-27 01:03:20 -04:00
}
2024-04-30 02:28:43 -04:00
// Our shared state
pub struct AppState {
// We require unique usernames. This tracks which usernames have been taken.
2024-07-27 02:51:04 -04:00
users: Mutex<HashMap<SocketAddr, User>>,
2024-04-30 02:28:43 -04:00
// Channel used to send messages to all connected clients.
tx: broadcast::Sender<String>,
// Master card decks
2024-07-29 00:34:17 -04:00
all_cards: Vec<CardSet>,
2024-04-30 02:28:43 -04:00
// Games list
2024-07-21 22:48:15 -04:00
games: Mutex<Vec<Game>>,
2024-07-27 23:23:26 -04:00
first_names: Vec<String>,
last_names: Vec<String>,
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().
let (tx, _rx) = broadcast::channel(100);
2024-07-27 02:51:04 -04:00
let users = Mutex::new(HashMap::<SocketAddr, User>::new());
2024-07-29 00:34:17 -04:00
let all_cards = load_cards_from_json("data/cah-cards-full.json")?;
2024-04-30 02:28:43 -04:00
let games = Mutex::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-07-22 01:32:09 -04:00
users,
2024-04-30 02:28:43 -04:00
tx,
2024-07-29 00:34:17 -04:00
all_cards,
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-05-02 01:00:47 -04:00
tracing::debug!("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
}