4 Commits

Author SHA1 Message Date
Jonas Platte
c2eb6137ae Fix nginx sample config 2022-11-25 14:38:36 +01:00
Jonas Platte
a0876f81c1 Reduce feature set enabled for tokio 2022-11-25 14:36:59 +01:00
Jonas Platte
3bec177cd9 Upgrade axum 2022-11-25 14:36:00 +01:00
Jonas Platte
f54bf11d0b Embed index.html and dedup people 2022-11-25 14:34:02 +01:00
5 changed files with 46 additions and 79 deletions

91
Cargo.lock generated
View File

@@ -21,9 +21,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "axum"
version = "0.5.17"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acee9fd5073ab6b045a275b3e709c163dd36c90685219cb21804a147b58dba43"
checksum = "744864363a200a5e724a7e61bc8c11b6628cf2e3ec519c8a1a48e609a8156b40"
dependencies = [
"async-trait",
"axum-core",
@@ -39,8 +39,10 @@ dependencies = [
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper",
"tokio",
@@ -52,9 +54,9 @@ dependencies = [
[[package]]
name = "axum-core"
version = "0.2.9"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e5939e02c56fecd5c017c37df4238c0a839fa76b7f97acdd7efb804fd181cc"
checksum = "79b8558f5a0581152dc94dcd289132a1d377494bdeafcd41869b3258e3e2ad92"
dependencies = [
"async-trait",
"bytes",
@@ -62,6 +64,7 @@ dependencies = [
"http",
"http-body",
"mime",
"rustversion",
"tower-layer",
"tower-service",
]
@@ -233,16 +236,6 @@ version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "lock_api"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.17"
@@ -263,9 +256,9 @@ dependencies = [
[[package]]
name = "matchit"
version = "0.5.0"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73cbba799671b762df5a175adf59ce145165747bb891505c43d09aefbbf38beb"
checksum = "3dfc802da7b1cf80aefffa0c7b2f77247c8b32206cc83c270b61264f5b360a80"
[[package]]
name = "memchr"
@@ -323,29 +316,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
]
[[package]]
name = "percent-encoding"
version = "2.2.0"
@@ -438,15 +408,6 @@ dependencies = [
"getrandom",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.7.0"
@@ -471,18 +432,18 @@ version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "rustversion"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.147"
@@ -514,6 +475,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_path_to_error"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "184c643044780f7ceb59104cef98a5a6f12cb2288a7bc701ab93a362b49fd47d"
dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@@ -547,15 +517,6 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [
"libc",
]
[[package]]
name = "smallvec"
version = "1.10.0"
@@ -605,14 +566,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d76ce4a75fb488c605c54bf610f221cea8b0dafb53333c1a67e8ee199dcd2ae3"
dependencies = [
"autocfg",
"bytes",
"libc",
"memchr",
"mio",
"num_cpus",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"winapi",

View File

@@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
axum = "0.5.17"
tokio = { version = "1.0", features = ["full"] }
axum = { version = "0.6.0", features = ["http1", "json", "tokio"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
serde = { version = "1.0", features = ["derive"] }
once_cell = "1.8.0"

View File

@@ -4,10 +4,8 @@ server {
#
# server_name whatever.tld;
# index.html stored in /srv/http/whatever.tld/secret-santa/index.html
location /secret-santa { root /srv/http/whatever.tld; }
# app running on localhost with the port hardcoded in main.rs
location /secret-santa-api/ { proxy_pass http://localhost:3067/secret-santa-api; }
location /secret-santa/ { proxy_pass http://localhost:3067/; }
# SSL config...
}

View File

@@ -10,10 +10,7 @@
<select id="person">
<option selected disabled>Who is drawing?</option>
<option value="Alice">Alice</option>
<option value="Bob">Bob</option>
<option value="Carol">Carol</option>
<option value="Dave">Dave</option>
__OPTIONS__
</select>
<div id="result"></div>
@@ -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}`;

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 {