Embed index.html and dedup people

This commit is contained in:
Jonas Platte
2022-11-25 14:34:02 +01:00
parent f92b5c56ff
commit f54bf11d0b
2 changed files with 19 additions and 7 deletions

34
src/index.html Normal file
View File

@@ -0,0 +1,34 @@
<!doctype html>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Secret Santa</title>
<h1>Secret Santa</h1>
<select id="person">
<option selected disabled>Who is drawing?</option>
__OPTIONS__
</select>
<div id="result"></div>
<script>
document.getElementById("person").addEventListener('change', event => {
const options = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ "person": event.target.value })
};
console.log(options);
fetch("api", options)
.then(response => response.text())
.then(text => {
document.getElementById("result").textContent = `Your draw: ${text}`;
});
});
</script>

View File

@@ -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<String> = Lazy::new(|| {
let content = include_str!("index.html");
let options: String = everyone()
.into_iter()
.map(|person| format!(r#"<option value="{person}">{person}</option>"#))
.collect();
content.replace("__OPTIONS__", &options)
});
static STATE: Lazy<Mutex<State>> = Lazy::new(Mutex::default);
struct State {