some error handling
This commit is contained in:
parent
30d4388063
commit
a1fb258db4
4 changed files with 21 additions and 9 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -26,6 +26,12 @@ dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.82"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.80"
|
version = "0.1.80"
|
||||||
|
@ -153,6 +159,7 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
|
||||||
name = "cards"
|
name = "cards"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
"futures",
|
"futures",
|
||||||
"rand",
|
"rand",
|
||||||
|
|
|
@ -13,3 +13,4 @@ tokio = { version = "1", features = ["full"] }
|
||||||
tower = { version = "0.4", features = ["util"] }
|
tower = { version = "0.4", features = ["util"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
|
anyhow = "1.0.82"
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::message_handler::*; //change this
|
||||||
use rand::prelude::IteratorRandom;
|
use rand::prelude::IteratorRandom;
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
/// A CAH white card
|
/// A CAH white card
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -1,10 +1,9 @@
|
||||||
|
use anyhow::{Context, Result};
|
||||||
use axum::{response::Html, routing::get, Router};
|
use axum::{response::Html, routing::get, Router};
|
||||||
use std::{
|
use std::{
|
||||||
// collections::HashSet,
|
// collections::HashSet,
|
||||||
error::Error,
|
|
||||||
fs,
|
fs,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
result::Result,
|
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
use tokio::sync::broadcast;
|
use tokio::sync::broadcast;
|
||||||
|
@ -16,16 +15,18 @@ use crate::api::*;
|
||||||
use crate::message_handler::*;
|
use crate::message_handler::*;
|
||||||
|
|
||||||
/// Parse json for card data
|
/// Parse json for card data
|
||||||
fn load_json(path: &str) -> Result<Vec<CAHCardSet>, Box<dyn Error>> {
|
fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
|
||||||
let data: String = fs::read_to_string(path)?;
|
let data: String =
|
||||||
let jayson: Vec<CAHCardSet> = serde_json::from_str(&data)?;
|
fs::read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
|
||||||
|
let jayson: Vec<CAHCardSet> =
|
||||||
|
serde_json::from_str(&data).with_context(|| format!("\"{path}\" is invalid json"))?;
|
||||||
|
|
||||||
Ok(jayson)
|
Ok(jayson)
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is still around just for reference
|
// this is still around just for reference
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn test() -> Result<(), Box<dyn Error>> {
|
fn test() -> Result<()> {
|
||||||
// choose decks
|
// choose decks
|
||||||
let cards_input_path: &str = "data/cah-cards-full.json";
|
let cards_input_path: &str = "data/cah-cards-full.json";
|
||||||
|
|
||||||
|
@ -96,7 +97,7 @@ async fn spawnclients() -> Html<&'static str> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<()> {
|
||||||
// stuff for logging
|
// stuff for logging
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(
|
.with(
|
||||||
|
@ -127,7 +128,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.with_state(app_state);
|
.with_state(app_state);
|
||||||
|
|
||||||
// send it
|
// send it
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await?;
|
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))?;
|
||||||
tracing::debug!("listening on {}", listener.local_addr()?);
|
tracing::debug!("listening on {}", listener.local_addr()?);
|
||||||
axum::serve(
|
axum::serve(
|
||||||
listener,
|
listener,
|
||||||
|
|
Loading…
Add table
Reference in a new issue