From 46c9ac3dd76046b9a713f71a221021cb73034ad1 Mon Sep 17 00:00:00 2001 From: Victor Vobis Date: Tue, 9 Sep 2025 21:03:49 +0200 Subject: [PATCH] bless --- node/Cargo.lock | 214 +++--------------------- node/Cargo.toml | 2 +- node/src/api/server.rs | 62 +++++-- node/src/args.rs | 2 +- node/src/constants.rs | 1 + node/src/db/database.rs | 5 +- node/src/lib.rs | 3 + node/src/node/node.rs | 4 + node/static/404.html | 12 ++ node/static/app.js | 21 +++ {node_client => node/static}/index.html | 10 +- node/static/style.css | 0 node_client/app.js | 12 -- 13 files changed, 120 insertions(+), 228 deletions(-) create mode 100644 node/src/constants.rs create mode 100644 node/static/404.html create mode 100644 node/static/app.js rename {node_client => node/static}/index.html (88%) create mode 100644 node/static/style.css delete mode 100644 node_client/app.js diff --git a/node/Cargo.lock b/node/Cargo.lock index 512c491..76d44ad 100644 --- a/node/Cargo.lock +++ b/node/Cargo.lock @@ -163,6 +163,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "async-trait" version = "0.1.89" @@ -331,7 +337,7 @@ dependencies = [ "sled", "textwrap", "thiserror 2.0.16", - "tinyhttp", + "tiny_http", "tokio", "tokio-tungstenite", "uuid", @@ -397,17 +403,6 @@ dependencies = [ "shlex", ] -[[package]] -name = "cfb" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f" -dependencies = [ - "byteorder", - "fnv", - "uuid", -] - [[package]] name = "cfg-if" version = "0.1.10" @@ -458,6 +453,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + [[package]] name = "cipher" version = "0.4.4" @@ -610,15 +611,6 @@ dependencies = [ "cfg-if 1.0.3", ] -[[package]] -name = "crossbeam-channel" -version = "0.5.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-epoch" version = "0.9.18" @@ -839,12 +831,6 @@ dependencies = [ "litrs", ] -[[package]] -name = "dyn-clone" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" - [[package]] name = "ecdsa" version = "0.16.9" @@ -937,16 +923,6 @@ dependencies = [ "toml", ] -[[package]] -name = "flate2" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a3d7db9596fecd151c5f638c0ee5d5bd487b6e0ea232e5dc96d5250f6f94b1d" -dependencies = [ - "crc32fast", - "miniz_oxide", -] - [[package]] name = "fluent" version = "0.16.1" @@ -1003,21 +979,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - [[package]] name = "form_urlencoded" version = "1.2.2" @@ -1306,6 +1267,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "i18n-config" version = "0.4.8" @@ -1515,15 +1482,6 @@ version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd" -[[package]] -name = "infer" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb33622da908807a06f9513c19b3c1ad50fab3e4137d82a78107d502075aa199" -dependencies = [ - "cfb", -] - [[package]] name = "inout" version = "0.1.4" @@ -1758,22 +1716,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "mime" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - -[[package]] -name = "mime_guess" -version = "2.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" -dependencies = [ - "mime", - "unicase", -] - [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1874,16 +1816,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "object" version = "0.36.7" @@ -1917,44 +1849,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" -[[package]] -name = "openssl" -version = "0.10.73" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8505734d46c8ab1e19a1dce3aef597ad87dcb4c37e7188231769bd6bd51cebf8" -dependencies = [ - "bitflags 2.9.3", - "cfg-if 1.0.3", - "foreign-types", - "libc", - "once_cell", - "openssl-macros", - "openssl-sys", -] - -[[package]] -name = "openssl-macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "openssl-sys" -version = "0.9.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90096e2e47630d78b7d1c20952dc621f957103f8bc2c8359ec81290d75238571" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "parking_lot" version = "0.11.2" @@ -2081,12 +1975,6 @@ dependencies = [ "spki", ] -[[package]] -name = "pkg-config" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" - [[package]] name = "poly1305" version = "0.8.0" @@ -2439,19 +2327,6 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" -[[package]] -name = "rusty_pool" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ed36cdb20de66d89a17ea04b8883fc7a386f2cf877aaedca5005583ce4876ff" -dependencies = [ - "crossbeam-channel", - "futures", - "futures-channel", - "futures-executor", - "num_cpus", -] - [[package]] name = "ryu" version = "1.0.20" @@ -2869,42 +2744,15 @@ dependencies = [ ] [[package]] -name = "tinyhttp" -version = "0.5.0" +name = "tiny_http" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40da872cd88188b99336266db850a3a87037dc463b376db7a422236427e3a0b6" +checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82" dependencies = [ - "tinyhttp-codegen", - "tinyhttp-internal", -] - -[[package]] -name = "tinyhttp-codegen" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "020ea8639b093aa2a1f70cdcb07af1935ec1fcdcc36b3f7e1208751dbbf2470d" -dependencies = [ - "quote", - "syn", - "tinyhttp-internal", -] - -[[package]] -name = "tinyhttp-internal" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58a51a6d7166b0083f7f9537a54cb749ce16ef73811bfd77452e4d4eab1c4389" -dependencies = [ - "dyn-clone", - "flate2", - "infer", + "ascii", + "chunked_transfer", + "httpdate", "log", - "mime_guess", - "num_cpus", - "openssl", - "rusty_pool", - "thiserror 1.0.69", - "tokio", ] [[package]] @@ -3018,12 +2866,6 @@ dependencies = [ "tinystr", ] -[[package]] -name = "unicase" -version = "2.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" - [[package]] name = "unicode-ident" version = "1.0.18" @@ -3129,12 +2971,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - [[package]] name = "version_check" version = "0.9.5" diff --git a/node/Cargo.toml b/node/Cargo.toml index 6b62315..f7e7178 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -32,4 +32,4 @@ ring = "0.17.14" shared = { path = "../shared", features = ["node"] } wallet = { path = "../wallet" } cli-renderer = { path = "../cli-renderer" } -tinyhttp = { version = "0.5.0", features = ["async"] } +tiny_http = "0.12.0" diff --git a/node/src/api/server.rs b/node/src/api/server.rs index 50a8391..81bd494 100644 --- a/node/src/api/server.rs +++ b/node/src/api/server.rs @@ -1,22 +1,54 @@ -use std::net::TcpStream; +use tiny_http::{ Method, Response, Header }; +use thiserror::Error; +use crate::{ log, PROJECT_PATH }; +use vlogger::*; -use tinyhttp; -use tokio::net::{TcpListener, TcpSocket}; - -#[derive(Error, Clone)] -enum HttpServerError { +#[derive(Error, Debug)] +pub enum HttpServerError { #[error("Socket Error: {0}")] SockerError(#[from] std::io::Error) } -async fn start_server() { - let tcp_listner = TcpListener::bind("127.0.0.1:6080").await.unwrap(); +const ENDPOINTS: [&str; 3] = ["/index.html", "/app.js", "/404.html"]; - let config = tinyhttp::prelude::Config::new(); - - let home_route = tinyhttp::prelude::Routes::new(); - - let server = tinyhttp::prelude::HttpListener::new(tcp_listner, config); - - server.start(); +fn derive_content_type(endpoint: &String) -> &str { + match endpoint.rsplit(".").last() { + Some(ext) => { + match ext { + "html" => "text/html", + "js" => "text/javascript", + _ => "text/plain" + } + } + None => { + "text/plain" + } + } +} + +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()) + ) +} + +pub 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 => { + } + _ => {} + } + } } diff --git a/node/src/args.rs b/node/src/args.rs index 5714874..6ff9b0a 100644 --- a/node/src/args.rs +++ b/node/src/args.rs @@ -169,7 +169,7 @@ pub struct CliArgs { pub seed: bool, /// Run with temporary db - #[arg(short = 't', long = "seed", action = clap::ArgAction::SetTrue)] + #[arg(short = 't', long = "temporary", action = clap::ArgAction::SetTrue)] pub temporary: bool, diff --git a/node/src/constants.rs b/node/src/constants.rs new file mode 100644 index 0000000..96b9e57 --- /dev/null +++ b/node/src/constants.rs @@ -0,0 +1 @@ +pub const PROJECT_PATH: &str = env!("CARGO_MANIFEST_DIR"); diff --git a/node/src/db/database.rs b/node/src/db/database.rs index 22ca08b..93ada1e 100644 --- a/node/src/db/database.rs +++ b/node/src/db/database.rs @@ -11,10 +11,9 @@ use crate::{ use shared::print_error_chain; +use crate::PROJECT_PATH; static BINCODE_CONFIG: Configuration = bincode::config::standard(); -const DB_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/database"); - const DB_TREE: &str = "blocks"; const BLOCK_PREFIX: [u8; 7] = *b"blocks:"; @@ -47,7 +46,7 @@ impl ChainDb { let path = if path.is_some() { &path.unwrap() } else { - DB_PATH + &format!("{}/{}", PROJECT_PATH, "database") }; let config = sled::Config::new() .cache_capacity(512 * 1024) diff --git a/node/src/lib.rs b/node/src/lib.rs index 1423a1a..cf12fbd 100644 --- a/node/src/lib.rs +++ b/node/src/lib.rs @@ -9,6 +9,9 @@ pub mod node { pub use blockchain::*; } +pub mod constants; +pub use constants::*; + pub mod cli; pub mod api { diff --git a/node/src/node/node.rs b/node/src/node/node.rs index 4d3ce1c..445712f 100644 --- a/node/src/node/node.rs +++ b/node/src/node/node.rs @@ -473,6 +473,10 @@ impl Node { .await; }; + tokio::spawn(async move { + let _ = crate::api::server::start_server(); + }); + let mut system_rx = subscribe_system_event(); publish_system_event(SystemEvent::NodeStarted); diff --git a/node/static/404.html b/node/static/404.html new file mode 100644 index 0000000..265d720 --- /dev/null +++ b/node/static/404.html @@ -0,0 +1,12 @@ + + + + + + + + + +

404 - Not Found

+ + diff --git a/node/static/app.js b/node/static/app.js new file mode 100644 index 0000000..18323bc --- /dev/null +++ b/node/static/app.js @@ -0,0 +1,21 @@ +const API_URL='http://localhost:6080/api/'; + +const connection = { + ws: [], + node_list: [], +} + +function establishConnection() { + const ws = new WebSocket('ws://localhost:6000'); +} + +function updateTableStats() { + +} + +async function getTableContent() { + let response = await fetch(API_URL + "get_table"); + let content = await response.json() + + console.log(content); +} diff --git a/node_client/index.html b/node/static/index.html similarity index 88% rename from node_client/index.html rename to node/static/index.html index c8c0def..2cc1e2d 100644 --- a/node_client/index.html +++ b/node/static/index.html @@ -11,22 +11,18 @@

Node Stats

- + - - - - - - +
stat 1stat 2stat 3stat 4stat 5