diff --git a/.gitignore b/.gitignore index 181db24..3b90369 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ !.gitignore +!*.js !*.md !*.py !*.rs diff --git a/server/Cargo.lock b/server/Cargo.lock index 62aee44..81ef216 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -574,6 +574,24 @@ dependencies = [ "serde", ] +[[package]] +name = "embed" +version = "0.1.0" +source = "git+https://github.com/RGBCube/embed-rs#7c41253f4f6006fcc2aad189c19132d2f0cccec2" +dependencies = [ + "embed-macros", +] + +[[package]] +name = "embed-macros" +version = "0.1.0" +source = "git+https://github.com/RGBCube/embed-rs#7c41253f4f6006fcc2aad189c19132d2f0cccec2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.43", +] + [[package]] name = "encoding_rs" version = "0.8.33" @@ -1327,6 +1345,7 @@ dependencies = [ "actix-cors", "actix-web", "chrono", + "embed", "maud", "serde", "sqlx", @@ -2353,3 +2372,8 @@ dependencies = [ "cc", "pkg-config", ] + +[[patch.unused]] +name = "proc-macro2" +version = "1.0.75" +source = "git+https://github.com/RGBCube/proc-macro2#e1713f29a5488360d96178081ac0fca3d4abdb8a" diff --git a/server/Cargo.toml b/server/Cargo.toml index cf7c1f8..f992a5c 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -9,12 +9,16 @@ edition = "2021" [dependencies] actix-cors = "0.6.5" -actix-web = "4.4.0" -chrono = "0.4.31" -maud = { version = "0.25.0", features = [ "actix-web" ] } -serde = { version = "1.0.192", features = [ "derive" ] } -sqlx = { version = "0.7.3", features = [ "chrono", "sqlite", "runtime-tokio" ] } -tokio = { version = "1.34.0", features = [ "full" ] } +actix-web = "4.4.0" +chrono = "0.4.31" +embed = { git = "https://github.com/RGBCube/embed-rs" } +maud = { version = "0.25.0", features = [ "actix-web" ] } +serde = { version = "1.0.192", features = [ "derive" ] } +sqlx = { version = "0.7.3", features = [ "chrono", "sqlite", "runtime-tokio" ] } +tokio = { version = "1.34.0", features = [ "full" ] } + +[patch.crates-io] +proc-macro2 = { git = "https://github.com/RGBCube/proc-macro2" } [profile.dev] incremental = true diff --git a/server/src/alert.js b/server/src/alert.js new file mode 100644 index 0000000..a3bc601 --- /dev/null +++ b/server/src/alert.js @@ -0,0 +1,30 @@ +const alertIfTime = () => { + const reminders = Array.from( + document + .querySelectorAll("li") + ) + .map(item => { + content: item.querySelector("p").textContent, + timestamp: +item.getAttribute("data-timestamp"), + led: +item.getAttribute("data-led"), + }); + + const currentTime = Math.floor(Date.now() / 1000); + + reminders.forEach(reminder => { + const differenceSeconds = currentTime - reminder.timestamp; + + if (differenceSeconds < 1 * 60) { + if (reminder.led != 0) fetch("http://localhost:3000/led/toggle?" + new URLSearchParams({ number: reminder.led }).then(console.log); + fetch("http://localhost:3000/speak?" + new URLSearchParams({ text: reminder.content })).then(console.log); + } + }); +}; + +alertIfTime(); + +setInterval(() => { + location.reload(); + alertIfTime(); +}, 1 * 60 * 1000); + diff --git a/server/src/view.rs b/server/src/view.rs index 500422f..f28dfca 100644 --- a/server/src/view.rs +++ b/server/src/view.rs @@ -89,36 +89,8 @@ async fn view(data: Data) -> web::Result { } } - script defer {(PreEscaped(r##" - const alertIfTime = () => { - const reminders = Array.from( - document - .querySelectorAll("li") - ) - .map(item => ({ - content: item.querySelector("p").textContent, - timestamp: +item.getAttribute("data-timestamp"), - led: +item.getAttribute("data-led"), - })); - - const currentTime = Math.floor(Date.now() / 1000); - - reminders.forEach(reminder => { - const differenceSeconds = currentTime - reminder.timestamp; - - if (differenceSeconds < 1 * 60) { - if (reminder.led != 0) fetch("http://localhost:3000/led/toggle?" + new URLSearchParams({ number: reminder.led }).then(console.log); - fetch("http://localhost:3000/speak?" + new URLSearchParams({ text: reminder.content })).then(console.log); - } - }); - }; - - alertIfTime(); - - setInterval(() => { - location.reload(); - alertIfTime(); - }, 1 * 60 * 1000); - "##))} + script { + (PreEscaped(embed::string!("alert.js"))) + } }) }