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 | |
| parent | 144e183280dae877d331acac819c46ef698fe800 (diff) | |
Store sessions in a hash map
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 12 | ||||
| -rw-r--r-- | src/session.rs | 4 | ||||
| -rw-r--r-- | src/template.rs | 3 |
3 files changed, 9 insertions, 10 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 diff --git a/src/session.rs b/src/session.rs index 7b0d1d1..b07623a 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,5 +1,7 @@ +use std::collections::HashMap; +use std::slice::Iter; + pub struct Session { pub steam_name: String, - pub steam_id: String, pub passcode: String, }
\ No newline at end of file diff --git a/src/template.rs b/src/template.rs index aa13215..d8b3511 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,8 +1,9 @@ +use std::collections::HashMap; use askama::Template; use crate::session::Session; #[derive(Template)] #[template(path = "index.html")] pub struct IndexTemplate<'a> { - pub sessions: &'a Vec<Session> + pub sessions: &'a HashMap<String, Session> }
\ No newline at end of file |
