From 7dd25c1580e08bd48533d876b5ed96a0f8a99d5c Mon Sep 17 00:00:00 2001 From: Victor Vobis Date: Wed, 10 Sep 2025 17:45:09 +0200 Subject: [PATCH] bless --- node/src/api/server.rs | 75 ++++++++++++++++++++++++++++++------------ node/src/node/node.rs | 5 +-- node/static/app.js | 4 ++- node/static/index.html | 47 +------------------------- node/static/style.css | 32 ++++++++++++++++++ 5 files changed, 93 insertions(+), 70 deletions(-) diff --git a/node/src/api/server.rs b/node/src/api/server.rs index 81bd494..b997505 100644 --- a/node/src/api/server.rs +++ b/node/src/api/server.rs @@ -3,52 +3,85 @@ use thiserror::Error; use crate::{ log, PROJECT_PATH }; use vlogger::*; +use std::fs; + #[derive(Error, Debug)] pub enum HttpServerError { #[error("Socket Error: {0}")] SockerError(#[from] std::io::Error) } -const ENDPOINTS: [&str; 3] = ["/index.html", "/app.js", "/404.html"]; +const INTERNAL_SERVER_ERROR: &'static str = r#" + + + + + + Node + + + +

500 - Internal Server Error

+

{}

+ + +"#; -fn derive_content_type(endpoint: &String) -> &str { - match endpoint.rsplit(".").last() { +fn derive_content_type(endpoint: &str) -> &'static str { + match endpoint.rsplit(".").next() { Some(ext) => { + log(msg!(DEBUG, "{:?}", ext)); match ext { "html" => "text/html", "js" => "text/javascript", + "css" => "text/css", _ => "text/plain" } } None => { - "text/plain" + "text/json" } } } -fn file_response(path: String, endpoint: String) -> Result, std::io::Error> { - let file = std::fs::OpenOptions::new().read(true).open(path)?; - let content_type = derive_content_type(&endpoint); - Ok( - Response::from_file(file) - .with_status_code(200) - .with_header(Header::from_bytes(&b"Content-Type"[..], content_type.as_bytes()).unwrap()) - ) +fn serve_static(status: u16, file_path: &'static str) -> Result, HttpServerError> { + let path: std::path::PathBuf = {PROJECT_PATH.to_string() + "/static" + file_path}.into(); + let file = std::fs::File::open(path)?; + let content_type = derive_content_type(file_path); + let response = Response::from_file(file).with_header(Header::from_bytes(b"Content-Type", content_type.as_bytes()).unwrap()).with_status_code(status); + Ok(response) } -pub fn start_server() -> Result<(), HttpServerError>{ +fn internal_server_error(e: String) -> Response>> { + Response::from_string(INTERNAL_SERVER_ERROR.replace("{}", &e)) + .with_header(Header::from_bytes(b"Content-Type", b"text/html").unwrap()) +} + +pub async fn start_server() -> Result<(), HttpServerError>{ let address = "127.0.0.1:6080"; let server = tiny_http::Server::http(address).unwrap(); - let root = format!("{}/{}", PROJECT_PATH, "static"); - loop { - let req = server.recv()?; - log(msg!(DEBUG, "{:?}", req)); - match req.method() { - Method::Get => { - } - _ => {} + let req = server.try_recv()?; + match req { + Some(req) => { + let url = req.url().split('?').next().unwrap(); + log(msg!(DEBUG, "{:?}", req.url())); + + let response = match (req.method(), url) { + (&Method::Get, "/") => serve_static(200, "/index.html"), + (&Method::Get, "/app.js") => serve_static(200, "/app.js"), + (&Method::Get, "/style.css") => serve_static(200, "/style.css"), + (_, _) => serve_static(404, "/404.html"), + }; + + match response { + Ok(r) => { let _ = req.respond(r); }, + Err(e) => { internal_server_error(e.to_string()); }, + }; + }, + None => {} } + tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; } } diff --git a/node/src/node/node.rs b/node/src/node/node.rs index 445712f..929e752 100644 --- a/node/src/node/node.rs +++ b/node/src/node/node.rs @@ -473,8 +473,8 @@ impl Node { .await; }; - tokio::spawn(async move { - let _ = crate::api::server::start_server(); + let handle = tokio::spawn(async move { + let _ = crate::api::server::start_server().await; }); let mut system_rx = subscribe_system_event(); @@ -500,6 +500,7 @@ impl Node { } } } + handle.abort_handle().abort(); self.shutdown().await; } } diff --git a/node/static/app.js b/node/static/app.js index 18323bc..60ef851 100644 --- a/node/static/app.js +++ b/node/static/app.js @@ -1,5 +1,7 @@ const API_URL='http://localhost:6080/api/'; +console.log("Hello World"); + const connection = { ws: [], node_list: [], @@ -14,7 +16,7 @@ function updateTableStats() { } async function getTableContent() { - let response = await fetch(API_URL + "get_table"); + let response = await fetch(API_URL + "get_table_content"); let content = await response.json() console.log(content); diff --git a/node/static/index.html b/node/static/index.html index 2cc1e2d..a7bacad 100644 --- a/node/static/index.html +++ b/node/static/index.html @@ -4,7 +4,7 @@ - +
@@ -21,52 +21,7 @@
- diff --git a/node/static/style.css b/node/static/style.css index e69de29..7381023 100644 --- a/node/static/style.css +++ b/node/static/style.css @@ -0,0 +1,32 @@ +body { + margin: 0; + padding: 0; +} + +.main-container { + padding: 6px; + display: flex; + align-content: center; + flex-direction: column; + height: 100%; + width: 100%; + box-sizing: border-box; +} + +.table-container { + padding: 6px; + place-self: center; + border: 2px solid #000000; + width: 50%; + height: 50%; + box-sizing: border-box; +} + +.background { + background-color: #808080; + width: 100vw; + height: 100vh; + max-height: 100vh; + max-width: 100vw; + box-sizing: border-box; +}