summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJomar Milan <jomarm@jomarm.com>2026-05-29 23:35:12 -0700
committerJomar Milan <jomarm@jomarm.com>2026-05-29 23:35:12 -0700
commit144e183280dae877d331acac819c46ef698fe800 (patch)
tree8572b4e1b77673bd2ff4f55a13fbb15fe9b10943 /src
Start with basic session creation
Diffstat (limited to 'src')
-rw-r--r--src/main.rs106
-rw-r--r--src/session.rs5
-rw-r--r--src/template.rs8
3 files changed, 119 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..422e784
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,106 @@
+mod session;
+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;
+use axum::extract::{Path, Query, State};
+use axum::http::{header, StatusCode};
+use axum::response::{Html, IntoResponse, Response};
+use axum::Router;
+use axum::routing::{get};
+use rust_embed::Embed;
+use crate::session::{Session};
+use crate::template::IndexTemplate;
+
+#[derive(Embed)]
+#[folder = "assets/"]
+struct Asset;
+
+struct AppState {
+ sessions: Mutex<Vec<Session>>
+}
+
+impl AppState {
+ fn new() -> Self {
+ AppState {
+ sessions: Mutex::new(Vec::new())
+ }
+ }
+}
+
+#[tokio::main]
+async fn main() {
+ let app = Router::new()
+ .route("/", get(serve_index))
+ .route("/dist/{*path}", get(serve_static))
+ .route("/session/{id}", get(visit_session).put(create_session))
+ .with_state(Arc::new(AppState::new()));
+
+ let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
+ let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
+ 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
+ };
+
+ match template.render() {
+ Ok(html) => Html(html).into_response(),
+ Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Render error").into_response()
+ }
+}
+
+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",
+ _ => "application/octet-stream"
+ };
+ ([(header::CONTENT_TYPE, mime)], content.data).into_response()
+ }
+ None => (StatusCode::NOT_FOUND, "404 Not Found").into_response()
+ }
+}
+
+async fn visit_session(Path(id): Path<String>, Query(query): Query<HashMap<String, String>>, State(state): State<Arc<AppState>>) -> Response {
+ let passcode = query.get("passcode");
+
+ let sessions = state.sessions.lock().unwrap();
+
+ match sessions.iter().find(|session| session.steam_id == 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()
+ }
+ },
+ None => (StatusCode::NOT_FOUND, "Session does not exist").into_response()
+ }
+}
+
+async fn create_session(Path(id): Path<String>, Query(query): Query<HashMap<String, String>>, State(state): State<Arc<AppState>>) -> Response {
+ let name = query.get("name").map(|name| name.clone()).unwrap_or("Unknown".to_string());
+ let passcode = SystemTime::now().duration_since(UNIX_EPOCH).map(|duration| duration.subsec_nanos()).unwrap_or(675603000).to_string();
+
+ 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);
+
+ (StatusCode::CREATED, passcode).into_response()
+} \ No newline at end of file
diff --git a/src/session.rs b/src/session.rs
new file mode 100644
index 0000000..7b0d1d1
--- /dev/null
+++ b/src/session.rs
@@ -0,0 +1,5 @@
+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
new file mode 100644
index 0000000..aa13215
--- /dev/null
+++ b/src/template.rs
@@ -0,0 +1,8 @@
+use askama::Template;
+use crate::session::Session;
+
+#[derive(Template)]
+#[template(path = "index.html")]
+pub struct IndexTemplate<'a> {
+ pub sessions: &'a Vec<Session>
+} \ No newline at end of file