diff --git a/index.html b/src/index.html similarity index 80% rename from index.html rename to src/index.html index 6b40abc..dc6abc1 100644 --- a/index.html +++ b/src/index.html @@ -10,10 +10,7 @@
@@ -28,7 +25,7 @@ console.log(options); - fetch("/secret-santa-api/", options) + fetch("api", options) .then(response => response.text()) .then(text => { document.getElementById("result").textContent = `Your draw: ${text}`; diff --git a/src/main.rs b/src/main.rs index 37e6fde..0cc62b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ -use axum::{routing::post, Json, Router}; +use axum::{ + response::Html, + routing::{get, post}, + Json, Router, +}; use once_cell::sync::Lazy; use rand::{thread_rng, Rng}; use serde::Deserialize; @@ -10,7 +14,9 @@ async fn main() { tracing_subscriber::fmt::init(); // build our application with some routes - let app = Router::new().route("/secret-santa-api", post(input)); + let app = Router::new() + .route("/", get(|| async { Html(INDEX_HTML.as_str()) })) + .route("/api", post(input)); // run it with hyper let addr = SocketAddr::from(([127, 0, 0, 1], 3067)); @@ -20,6 +26,15 @@ async fn main() { .unwrap(); } +static INDEX_HTML: Lazy = Lazy::new(|| { + let content = include_str!("index.html"); + let options: String = everyone() + .into_iter() + .map(|person| format!(r#""#)) + .collect(); + content.replace("__OPTIONS__", &options) +}); + static STATE: Lazy> = Lazy::new(Mutex::default); struct State {