diff options
| author | Jomar Milan <jomarm@jomarm.com> | 2026-05-30 00:09:59 -0700 |
|---|---|---|
| committer | Jomar Milan <jomarm@jomarm.com> | 2026-05-30 00:09:59 -0700 |
| commit | 453ba51e78024fed7d22f984959b050182badffd (patch) | |
| tree | 0dfb75f0a8c86ee800b62154ce270af720dbe775 /src/main.rs | |
| parent | 144e183280dae877d331acac819c46ef698fe800 (diff) | |
Store sessions in a hash map
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 422e784..cc8cc34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ mod template; use std::collections::HashMap; use std::net::SocketAddr; -use std::ops::{Deref, Index}; use std::sync::{Arc, Mutex}; use std::time::{SystemTime, UNIX_EPOCH}; use askama::Template; @@ -21,13 +20,13 @@ use crate::template::IndexTemplate; struct Asset; struct AppState { - sessions: Mutex<Vec<Session>> + sessions: Mutex<HashMap<String, Session>> } impl AppState { fn new() -> Self { AppState { - sessions: Mutex::new(Vec::new()) + sessions: Mutex::new(HashMap::new()) } } } @@ -75,7 +74,7 @@ async fn visit_session(Path(id): Path<String>, Query(query): Query<HashMap<Strin let sessions = state.sessions.lock().unwrap(); - match sessions.iter().find(|session| session.steam_id == id) { + match sessions.get(&id) { Some(session) => { if passcode.map(|passcode| passcode.as_str() == session.passcode).unwrap_or(false) { (StatusCode::OK, "hi").into_response() @@ -93,14 +92,11 @@ async fn create_session(Path(id): Path<String>, Query(query): Query<HashMap<Stri let mut sessions = state.sessions.lock().unwrap(); - sessions.iter().position(|session| session.steam_id == id).map(|idx| sessions.swap_remove(idx)); - let session = Session { steam_name: name, - steam_id: id, passcode: passcode.clone() }; - sessions.push(session); + sessions.insert(id, session); (StatusCode::CREATED, passcode).into_response() }
\ No newline at end of file |
