added form

This commit is contained in:
WanderingPenwing 2024-09-02 17:50:25 +02:00
parent ba25617391
commit 49b32d8d53
8 changed files with 352 additions and 22 deletions

275
Cargo.lock generated
View file

@ -127,8 +127,8 @@ dependencies = [
"actix-utils",
"futures-core",
"futures-util",
"mio",
"socket2",
"mio 1.0.2",
"socket2 0.5.7",
"tokio",
"tracing",
]
@ -191,7 +191,7 @@ dependencies = [
"serde_json",
"serde_urlencoded",
"smallvec",
"socket2",
"socket2 0.5.7",
"time",
"url",
]
@ -484,12 +484,65 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "futures"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-executor"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
[[package]]
name = "futures-macro"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.30"
@ -508,10 +561,16 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
@ -736,6 +795,18 @@ dependencies = [
"adler2",
]
[[package]]
name = "mio"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys 0.48.0",
]
[[package]]
name = "mio"
version = "1.0.2"
@ -746,7 +817,7 @@ dependencies = [
"libc",
"log",
"wasi",
"windows-sys",
"windows-sys 0.52.0",
]
[[package]]
@ -790,7 +861,7 @@ dependencies = [
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
@ -817,6 +888,19 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "ping-rs"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d873f038f84371f9c7fa13f6afea4d5f1fbcd5070ba8eb7af2a6d41c768eff8b"
dependencies = [
"futures",
"mio 0.8.11",
"paste",
"socket2 0.4.10",
"windows",
]
[[package]]
name = "pkg-config"
version = "0.3.30"
@ -1060,6 +1144,16 @@ version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "socket2"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "socket2"
version = "0.5.7"
@ -1067,7 +1161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
dependencies = [
"libc",
"windows-sys",
"windows-sys 0.52.0",
]
[[package]]
@ -1136,12 +1230,12 @@ dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"mio 1.0.2",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"windows-sys",
"socket2 0.5.7",
"windows-sys 0.52.0",
]
[[package]]
@ -1248,13 +1342,74 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04662ed0e3e5630dfa9b26e4cb823b817f1a9addda855d973a9458c236556244"
dependencies = [
"windows_aarch64_gnullvm 0.42.2",
"windows_aarch64_msvc 0.42.2",
"windows_i686_gnu 0.42.2",
"windows_i686_msvc 0.42.2",
"windows_x86_64_gnu 0.42.2",
"windows_x86_64_gnullvm 0.42.2",
"windows_x86_64_msvc 0.42.2",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
@ -1263,28 +1418,64 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc 0.52.6",
"windows_i686_gnu 0.52.6",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
"windows_i686_msvc 0.52.6",
"windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc 0.52.6",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
@ -1297,24 +1488,72 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
@ -1327,7 +1566,9 @@ version = "0.1.0"
dependencies = [
"actix-files",
"actix-web",
"ping-rs",
"pulldown-cmark",
"serde",
]
[[package]]

View file

@ -9,3 +9,5 @@ edition = "2021"
actix-web = "4"
actix-files = "0.6"
pulldown-cmark = "0.9" # Markdown parser
ping-rs = "0.1.2"
serde = { version = "1.0", features = ["derive"] }

View file

@ -6,7 +6,9 @@ It loads so fast ! *yes, I know x)* (go check the epilogue)
###### spacer
*summary*
~SUMMARY~
~SUCCESS~
###### spacer

18
pages/summary_off.html Normal file
View file

@ -0,0 +1,18 @@
<table class="summary">
<tr><td></td><td><a href="https://www.penwing.org">www.penwing.org</a></td><td>this website</td></tr>
<tr><td>🔍</td><td><a href="https://search.penwing.org">search.penwing.org</a></td><td>search engine</td></tr>
<tr><td></td><td><a href="https://git.penwing.org">git.penwing.org</a></td><td>git server</td></tr>
<tr></tr>
<tr></tr>
<tr><td>*</td><td><em>movie.penwing.org</em></td><td>movies/series collection</td></tr>
<tr><td>*</td><td><em>pdf.penwing.org</em></td><td>pdf edition</td></tr>
<tr><td>*</td><td><em>files.penwing.org</em></td><td>remote storage</td></tr>
</table>
<p>The boiler is currently off, some websites won't work.
If you want to start the boiler, enter the magic word : </p>
<form action="/wake">
<input type="text" id="password" name="password">
<input type="submit" value="Submit">
</form>

10
pages/summary_on.html Normal file
View file

@ -0,0 +1,10 @@
<table class="summary">
<tr><td></td><td><a href="https://www.penwing.org">www.penwing.org</a></td><td>this website</td></tr>
<tr><td>🔍</td><td><a href="https://search.penwing.org">search.penwing.org</a></td><td>search engine</td></tr>
<tr><td></td><td><a href="https://git.penwing.org">git.penwing.org</a></td><td>git server</td></tr>
<tr></tr>
<tr></tr>
<tr><td>🎬</td><td><a href="https://movie.penwing.org">movie.penwing.org</a></td><td>movies/series collection</td></tr>
<tr><td>📄</td><td><a href="https://pdf.penwing.org">pdf.penwing.org</a></td><td>pdf edition</td></tr>
<tr><td>📁</td><td><a href="https://file.penwing.org">files.penwing.org</a></td><td>remote storage</td></tr>
</table>

1
pages/wake_failure.html Normal file
View file

@ -0,0 +1 @@
<p>Wrong password, try again.</p>

1
pages/wake_success.html Normal file
View file

@ -0,0 +1 @@
<p>Successfuly started boiler, please wait a few minutes for the startup process to end.</p>

View file

@ -1,6 +1,13 @@
use pulldown_cmark::{Parser, html, Event, Tag, HeadingLevel};
use actix_web::{web, App, HttpServer, HttpResponse, Responder};
use std::fs;
use std::time::Duration;
use serde::Deserialize;
#[derive(Deserialize)]
struct WakeQuery {
password: String,
}
fn slugify(text: &str) -> String {
text.to_lowercase()
@ -8,6 +15,17 @@ fn slugify(text: &str) -> String {
.replace(|c: char| !c.is_alphanumeric() && c != '-', "")
}
fn boiler_on() -> bool {
let addr = "192.168.1.42".parse().unwrap();
let data = []; // ping data
let timeout = Duration::from_millis(20);
let result = ping_rs::send_ping(&addr, timeout, &data, None);
match result {
Ok(_reply) => return true,
Err(_e) => return false
}
}
fn markdown_to_html(markdown_content: &str) -> String {
let parser = Parser::new(markdown_content);
@ -44,16 +62,41 @@ fn markdown_to_html(markdown_content: &str) -> String {
html_renderer
}
async fn serve_markdown(file_path: &str) -> impl Responder {
async fn serve_markdown(file_path: &str, query_success: Option<bool>) -> impl Responder {
// Read the Markdown file
let markdown_content = fs::read_to_string(file_path).unwrap_or_else(|_| String::from("Error reading markdown file"));
// Convert Markdown to HTML
let html_content = markdown_to_html(&markdown_content);
let mut html_content = markdown_to_html(&markdown_content);
// Create the common HTML wrapper
let common_html = fs::read_to_string("pages/common.html").unwrap_or_else(|_| String::from("Error reading html file"));
if file_path == "pages/summary.md" {
if let Some(success) = query_success {
let summary_html : String;
let success_html : String;
if success {
summary_html = fs::read_to_string("pages/summary_on.html").unwrap_or_else(|_| String::from("Error reading summary on html file"));
success_html = fs::read_to_string("pages/wake_success.html").unwrap_or_else(|_| String::from("Error reading success html file"));
} else {
summary_html = fs::read_to_string("pages/summary_off.html").unwrap_or_else(|_| String::from("Error reading summary off html file"));
success_html = fs::read_to_string("pages/wake_failure.html").unwrap_or_else(|_| String::from("Error reading failure html file"));
}
html_content = html_content.replace("~SUMMARY~", &summary_html).replace("~SUCCESS~", &success_html);
} else {
let summary_html = if boiler_on() {
fs::read_to_string("pages/summary_on.html").unwrap_or_else(|_| String::from("Error reading summary on html file"))
} else {
fs::read_to_string("pages/summary_off.html").unwrap_or_else(|_| String::from("Error reading summary off html file"))
};
html_content = html_content.replace("~SUMMARY~", &summary_html).replace("~SUCCESS~", "");
}
}
// Replace #CONTENT# placeholder with the actual Markdown HTML content
let page_html = common_html.replace("#CONTENT#", &html_content);
@ -62,11 +105,21 @@ async fn serve_markdown(file_path: &str) -> impl Responder {
async fn summary() -> impl Responder {
serve_markdown("pages/summary.md").await
serve_markdown("pages/summary.md", None).await
}
async fn tools() -> impl Responder {
serve_markdown("pages/tools.md").await
serve_markdown("pages/tools.md", None).await
}
async fn wake(query: web::Query<WakeQuery>) -> impl Responder {
let password = &query.password;
if password == "magic" {
serve_markdown("pages/summary.md", Some(true)).await
} else {
serve_markdown("pages/summary.md", Some(false)).await
}
}
#[actix_web::main]
@ -75,7 +128,9 @@ async fn main() -> std::io::Result<()> {
App::new()
.route("/", web::get().to(summary)) // Route for root
.route("/tools", web::get().to(tools)) // Route for tools
.route("/wake", web::get().to(wake))
.service(actix_files::Files::new("/assets", "./assets").show_files_listing())
.default_service(web::route().to(summary))
})
.bind("127.0.0.1:8080")?
.run()