From 8db94239a745ce6cc2376bd48c8d1544fe471bb0 Mon Sep 17 00:00:00 2001 From: Petr Vecera Date: Mon, 19 Feb 2024 20:46:11 +0100 Subject: [PATCH] Add simple http --- src-tauri/Cargo.lock | 26 ++++++++++++++++++++ src-tauri/Cargo.toml | 2 ++ src-tauri/src/dp_utils.rs | 50 +++++++++++++++++++++++++++++++++++++++ src-tauri/src/lib.rs | 1 + src-tauri/src/main.rs | 7 +++++- 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src-tauri/src/dp_utils.rs diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 72a73ea..aff3941 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -99,6 +99,12 @@ dependencies = [ "x11rb", ] +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "async-broadcast" version = "0.5.1" @@ -592,6 +598,12 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + [[package]] name = "ciborium" version = "0.2.1" @@ -730,6 +742,7 @@ dependencies = [ "tauri-plugin-store", "tauri-plugin-window-state", "thiserror", + "tiny_http", "tokio", "vault", "window-shadows", @@ -4847,6 +4860,19 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny_http" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0d6ef4e10d23c1efb862eecad25c5054429a71958b4eeef85eb5e7170b477ca" +dependencies = [ + "ascii", + "chunked_transfer", + "log", + "time", + "url", +] + [[package]] name = "tinytemplate" version = "1.2.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a284a0b..e39567a 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -38,6 +38,8 @@ vault = "8" window-shadows = "0.2" # monitoring by sentry sentry = "0.32.2" +# For the OBS interface we need to expose webserver +tiny_http = "0.11" [dev-dependencies] criterion = { version = "0.4", features = ["html_reports"] } diff --git a/src-tauri/src/dp_utils.rs b/src-tauri/src/dp_utils.rs new file mode 100644 index 0000000..51acac8 --- /dev/null +++ b/src-tauri/src/dp_utils.rs @@ -0,0 +1,50 @@ +use std::fs::File; +use std::path::Path; +use std::panic; +use log::{error, info, warn}; + +use tiny_http::{Server, Response, StatusCode}; + +// const server: tiny_http::Server = Server::http("127.0.0.1:8000").unwrap(); +// +// const request: tiny_http::Request = match server.recv() { +// Ok(rq) => rq, +// Err(e) => { println!("error: {}", e); break } +// }; +// +// const response: tiny_http::Response = tiny_http::Response::from_file(File::open(&Path::new("image.png")).unwrap()); +// const responded = request.respond(response); +// +// + + +pub fn test_fn() { + + let result = panic::catch_unwind(|| { + + println!("Starting server..."); + let server = Server::http("127.0.0.1:8000").unwrap(); + + for request in server.incoming_requests() { + let file_path = Path::new(r"C:\Users\pagep\AppData\Roaming\com.coh3stats.desktop\streamerOverlay.html"); + let file = match File::open(&file_path) { + Ok(file) => file, + Err(_) => { + let response = Response::new_empty(StatusCode(404)); + let _ = request.respond(response); + continue; + } + }; + + let response = Response::from_file(file); + let _ = request.respond(response); + } + + }); + + if let Err(err) = result { + println!("Error: {:?}", err); + error!("Couldn't start the : {:?}", err); + } + +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 5cbdbc4..a27aefa 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,2 +1,3 @@ pub mod parse_log_file; pub mod plugins; +pub mod dp_utils; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 951d9aa..faf4fc0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,9 +5,10 @@ extern crate machine_uid; -use coh3_stats_desktop_app::{parse_log_file, plugins::cohdb}; +use coh3_stats_desktop_app::{parse_log_file, plugins::cohdb, dp_utils::test_fn}; use log::error; use std::path::Path; +use std::thread; use tauri::Manager; use tauri_plugin_log::LogTarget; use window_shadows::set_shadow; @@ -27,6 +28,10 @@ fn main() { ..Default::default() })); + let _handle = thread::spawn(|| { + test_fn(); + }); + tauri::Builder::default() .invoke_handler(tauri::generate_handler![ default_log_file_path,