summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs12
-rw-r--r--src/session.rs4
-rw-r--r--src/template.rs3
-rw-r--r--templates/index.html4
4 files changed, 11 insertions, 12 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
diff --git a/templates/index.html b/templates/index.html
index b97f082..8b531d3 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -7,10 +7,10 @@
<body>
<h2>Sessions</h2>
<ul>
-{% for session in sessions %}
+{% for (id, session) in sessions %}
<li>
<span>{{session.steam_name}}'s game</span>
- <form action="/session/{{session.steam_id}}">
+ <form action="/session/{{id}}">
<input name="passcode" placeholder="Passcode">
<input type="submit">
</form>