From 3aee60f52b146bcfc010afab78331872180ecf64 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Thu, 29 Aug 2024 17:34:49 +1000 Subject: initial GSI implementation --- src/main.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1b2c8c7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,99 @@ +use hyper::{Body, Request, Response, Server, service::{make_service_fn, service_fn}}; +use serde_json::Value; +use std::convert::Infallible; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +struct MatchData { + map: Option, + round: Option, + player_id: Option, + player_state: Option, + player_weapons: Option, + player_match_stats: Option, + allplayers_id: Option, +} + +#[derive(Debug, Deserialize)] +struct MapData { + name: Option, + // Add other map-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct RoundData { + phase: Option, + // Add other round-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerIdData { + id: Option, + // Add other player_id-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerStateData { + health: Option, + armor: Option, + flashed: Option, + // Add other player_state-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerWeaponsData { + primary: Option, + secondary: Option, + // Add other player_weapons-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerMatchStatsData { + kills: Option, + deaths: Option, + // Add other player_match_stats-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct AllPlayersIdData { + players: Vec, + // Add other allplayers_id-specific fields as necessary +} + +async fn handle_request(req: Request) -> Result, Infallible> { + if req.method() == hyper::Method::POST { + let whole_body = hyper::body::to_bytes(req.into_body()).await.unwrap(); + let json: Value = serde_json::from_slice(&whole_body).unwrap(); + + // Print the raw JSON for debugging + println!("Received JSON: {}", json); + + // Print out the parsed data for debugging + if let Some(map) = json.as_object() { + for (key, value) in map { + println!("{}: {}", key, value); + } + } else { + println!("Received data is not a JSON object"); + } + + // Respond to the GSI service + Ok(Response::new(Body::from("Received"))) + } else { + Ok(Response::new(Body::from("Unsupported HTTP method"))) + } +} + +#[tokio::main] +async fn main() { + let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) }); + + let addr = ([0, 0, 0, 0], 5000).into(); // Listen on all interfaces on port 3000 + let server = Server::bind(&addr).serve(make_svc); + + println!("Listening on http://{}", addr); + + if let Err(e) = server.await { + eprintln!("server error: {}", e); + } +} \ No newline at end of file -- cgit v1.2.3