summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/session.js5
-rw-r--r--src/main.rs40
-rw-r--r--src/session.rs15
-rw-r--r--src/template.rs6
-rw-r--r--templates/index.html4
-rw-r--r--templates/session.html22
6 files changed, 71 insertions, 21 deletions
diff --git a/assets/session.js b/assets/session.js
new file mode 100644
index 0000000..b4f95dd
--- /dev/null
+++ b/assets/session.js
@@ -0,0 +1,5 @@
+const colorSelect = document.getElementById('color-select');
+
+colorSelect.addEventListener('change', () => {
+ colorSelect.disabled = true;
+}); \ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index cc8cc34..82c4f19 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};
-use askama::Template;
+use askama::{Template};
use axum::extract::{Path, Query, State};
use axum::http::{header, StatusCode};
use axum::response::{Html, IntoResponse, Response};
@@ -13,7 +13,7 @@ use axum::Router;
use axum::routing::{get};
use rust_embed::Embed;
use crate::session::{Session};
-use crate::template::IndexTemplate;
+use crate::template::{IndexTemplate, SessionTemplate};
#[derive(Embed)]
#[folder = "assets/"]
@@ -44,23 +44,28 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}
-async fn serve_index(State(state): State<Arc<AppState>>) -> Response {
- let sessions = state.sessions.lock().unwrap();
- let template = IndexTemplate {
- sessions: &sessions
- };
-
+fn serve_template(template: impl Template) -> Response {
match template.render() {
Ok(html) => Html(html).into_response(),
- Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Render error").into_response()
+ Err(err) => {
+ eprintln!("An error occurred while rendering a template: {}", err);
+ (StatusCode::INTERNAL_SERVER_ERROR, "Render error").into_response()
+ }
}
}
+async fn serve_index(State(state): State<Arc<AppState>>) -> Response {
+ let sessions = state.sessions.lock().unwrap();
+ serve_template(IndexTemplate {
+ sessions: &sessions
+ })
+}
+
async fn serve_static(Path(path): Path<String>) -> Response {
match Asset::get(path.as_str()) {
Some(content) => {
let mime = match path.split('.').last() {
- Some("html") => "text/html",
+ Some("js") => "application/javascript",
_ => "application/octet-stream"
};
([(header::CONTENT_TYPE, mime)], content.data).into_response()
@@ -76,10 +81,12 @@ async fn visit_session(Path(id): Path<String>, Query(query): Query<HashMap<Strin
match sessions.get(&id) {
Some(session) => {
- if passcode.map(|passcode| passcode.as_str() == session.passcode).unwrap_or(false) {
- (StatusCode::OK, "hi").into_response()
- } else {
- (StatusCode::FORBIDDEN, "Incorrect session passcode").into_response()
+ match passcode {
+ Some(passcode) if passcode.as_str() == session.passcode =>
+ serve_template(SessionTemplate {
+ session: &session
+ }),
+ _ => (StatusCode::FORBIDDEN, "Incorrect session passcode").into_response()
}
},
None => (StatusCode::NOT_FOUND, "Session does not exist").into_response()
@@ -92,10 +99,7 @@ async fn create_session(Path(id): Path<String>, Query(query): Query<HashMap<Stri
let mut sessions = state.sessions.lock().unwrap();
- let session = Session {
- steam_name: name,
- passcode: passcode.clone()
- };
+ let session = Session::new(name, passcode.clone());
sessions.insert(id, session);
(StatusCode::CREATED, passcode).into_response()
diff --git a/src/session.rs b/src/session.rs
index b07623a..cac100e 100644
--- a/src/session.rs
+++ b/src/session.rs
@@ -1,7 +1,20 @@
use std::collections::HashMap;
-use std::slice::Iter;
pub struct Session {
pub steam_name: String,
pub passcode: String,
+ pub hands: HashMap<String, Hand>
+}
+
+pub struct Hand {
+}
+
+impl Session {
+ pub fn new(steam_name: String, passcode: String) -> Self {
+ Session {
+ steam_name,
+ passcode,
+ hands: HashMap::new()
+ }
+ }
} \ No newline at end of file
diff --git a/src/template.rs b/src/template.rs
index d8b3511..c02a3df 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -6,4 +6,10 @@ use crate::session::Session;
#[template(path = "index.html")]
pub struct IndexTemplate<'a> {
pub sessions: &'a HashMap<String, Session>
+}
+
+#[derive(Template)]
+#[template(path = "session.html")]
+pub struct SessionTemplate<'a> {
+ pub session: &'a Session
} \ No newline at end of file
diff --git a/templates/index.html b/templates/index.html
index 8b531d3..78d60e9 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -5,11 +5,11 @@
<title>Rusted Ambulator</title>
</head>
<body>
-<h2>Sessions</h2>
+<h1>Sessions</h1>
<ul>
{% for (id, session) in sessions %}
<li>
- <span>{{session.steam_name}}'s game</span>
+ <span>{{session.steam_name}}'s game ({{id}})</span>
<form action="/session/{{id}}">
<input name="passcode" placeholder="Passcode">
<input type="submit">
diff --git a/templates/session.html b/templates/session.html
new file mode 100644
index 0000000..6319d05
--- /dev/null
+++ b/templates/session.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title>{{session.steam_name}}'s game</title>
+ <script src="/dist/session.js" defer></script>
+</head>
+<body>
+<h1>{{session.steam_name}}'s game</h1>
+<form id="player-selection">
+ <h2>Player Selection</h2>
+ <label for="color-select">Your color:</label>
+ <select id="color-select">
+ <option value="">None selected</option>
+ <hr>
+ {% for (color, _) in session.hands %}
+ <option value="{{color}}">{{color}}</option>
+ {% endfor %}
+ </select>
+</form>
+</body>
+</html> \ No newline at end of file