From f0bd6020a29b7891eaec3527a961fa86bd2bdb2b Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Wed, 11 Dec 2024 20:47:15 +0100 Subject: [PATCH] add basic website --- Cargo.lock | 61 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ assets/index.html | 15 ++++++++++++ src/webapi.rs | 11 +++++++++ test.html | 12 ---------- test.js | 13 ---------- 6 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 assets/index.html delete mode 100644 test.html delete mode 100644 test.js diff --git a/Cargo.lock b/Cargo.lock index 60b4563..693efc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,6 +214,20 @@ dependencies = [ "tracing", ] +[[package]] +name = "axum-embed" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "077959a7f8cf438676af90b483304528eb7e16eadadb7f44e9ada4f9dceb9e62" +dependencies = [ + "axum-core", + "chrono", + "http", + "mime_guess", + "rust-embed", + "tower-service", +] + [[package]] name = "axum-extra" version = "0.9.6" @@ -651,6 +665,7 @@ dependencies = [ "async-trait", "atoi_radix10", "axum", + "axum-embed", "axum-extra", "axum-streams", "bytes", @@ -660,6 +675,7 @@ dependencies = [ "headers", "image", "rand", + "rust-embed", "serde", "tempfile", "test-case", @@ -1667,6 +1683,40 @@ version = "0.8.50" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a" +[[package]] +name = "rust-embed" +version = "8.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa66af4a4fdd5e7ebc276f115e895611a34739a9c1c01028383d612d550953c0" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "8.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6125dbc8867951125eec87294137f4e9c2c96566e61bf72c45095a7c77761478" +dependencies = [ + "proc-macro2", + "quote", + "rust-embed-utils", + "syn", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "8.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e5347777e9aacb56039b0e1f28785929a8a3b709e87482e7442c72e7c12529d" +dependencies = [ + "sha2", + "walkdir", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1787,6 +1837,17 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" diff --git a/Cargo.toml b/Cargo.toml index 860ed2c..c761c8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" async-trait = "0.1.83" atoi_radix10 = { version = "0.0.1", optional = true } axum = { version = "0.7.7", features = ["ws"] } +axum-embed = "0.1.0" axum-extra = { version = "0.9.4", features = ["typed-header"] } axum-streams = "0.19.0" bytes = "1.6.0" @@ -15,6 +16,7 @@ futures = "0.3.31" headers = "0.4.0" image = "0.25.2" rand = "*" +rust-embed = "8.5.0" serde = { version = "1.0.210", features = ["derive"] } tokio = { version = "1.38", features = ["full"] } tokio-stream = "0.1.16" diff --git a/assets/index.html b/assets/index.html new file mode 100644 index 0000000..02ebee2 --- /dev/null +++ b/assets/index.html @@ -0,0 +1,15 @@ + + + + + + + Flurry + + + + + The main pixelflut canvas + + + diff --git a/src/webapi.rs b/src/webapi.rs index 5328cc6..6aef64c 100644 --- a/src/webapi.rs +++ b/src/webapi.rs @@ -10,6 +10,7 @@ use axum::{ use axum_extra::TypedHeader; use axum_streams::StreamBodyAs; use futures::{never::Never, stream::repeat_with, Stream}; +use rust_embed::RustEmbed; use serde::Deserialize; use tokio::net::TcpListener; use tower_http::trace::{DefaultMakeSpan, TraceLayer}; @@ -21,14 +22,24 @@ use crate::{ AsyncResult, }; +#[derive(RustEmbed, Clone)] +#[folder = "assets/"] +struct Assets; + #[derive(Clone)] pub struct WebApiContext { pub grids: Arc<[grid::Flut]>, } pub async fn serve(ctx: WebApiContext) -> AsyncResult { + let assets = axum_embed::ServeEmbed::::with_parameters( + Some("404.html".to_string()), + axum_embed::FallbackBehavior::NotFound, + Some("index.html".to_string()), + ); let app = Router::new() .route("/imgstream", any(image_stream)) + .nest_service("/", assets) .with_state(ctx) // logging middleware .layer( diff --git a/test.html b/test.html deleted file mode 100644 index 3ba10a2..0000000 --- a/test.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - Test - - - -

Test

-

Test

- - - \ No newline at end of file diff --git a/test.js b/test.js deleted file mode 100644 index f82602e..0000000 --- a/test.js +++ /dev/null @@ -1,13 +0,0 @@ -const ws = new WebSocket("ws://127.0.0.1:3000/imgstream?canvas=0"); -console.log("Connecting"); -ws.onopen = () => { - console.log("Connected"); -} -ws.onmessage = (msg) => { - var reader = new FileReader(); - reader.readAsDataURL(msg.data); - reader.onloadend = function() { - var base64data = reader.result; - document.getElementById("image").src = base64data; - } -} \ No newline at end of file