From aef1ffbd9050f5e501ed6322b156ee084d2dfeb3 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 20 Nov 2023 22:10:01 +0000 Subject: [PATCH] More chatgpt modifications --- Makefile | 5 +++++ crates/librqbit/src/http_api.rs | 20 ++++++++++---------- crates/librqbit/webui/app.js | 9 +++------ crates/librqbit/webui/app.ts | 8 +++----- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 04072b9..0a794a7 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,11 @@ OPENSSL_VERSION=3.1.1 all: sign-release sign-debug +@PHONY: webui-dev +webui-dev: + cd crates/librqbit/webui && \ + tsc --watch + @PHONY: clean clean: rm -rf target diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index ffe2596..ce101f0 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -13,7 +13,6 @@ use serde::{Deserialize, Serialize}; use std::net::SocketAddr; use std::sync::Arc; use std::time::{Duration, Instant}; -use tower_http::cors::{AllowHeaders, AllowOrigin}; use tracing::{info, warn}; use axum::Router; @@ -118,6 +117,7 @@ impl HttpApi { state.api_peer_stats(idx, filter).map(axum::Json) } + #[allow(unused_mut)] let mut app = Router::new() .route("/", get(api_root)) .route("/dht/stats", get(dht_stats)) @@ -150,18 +150,18 @@ impl HttpApi { }), ); - let cors_layer = { - #[cfg(debug_assertions)] - { + // This is to develop webui by just doing "open index.html && tsc --watch" + let cors_layer = std::env::var("CORS_DEBUG") + .ok() + .map(|_| { + use tower_http::cors::{AllowHeaders, AllowOrigin}; + + warn!("CorsLayer: allowing everything because CORS_DEBUG is set"); tower_http::cors::CorsLayer::default() .allow_origin(AllowOrigin::predicate(|_, _| true)) .allow_headers(AllowHeaders::any()) - } - #[cfg(not(debug_assertions))] - { - tower_http::cors::CorsLayer::default() - } - }; + }) + .unwrap_or_default(); app = app.nest("/web/", webui_router).layer(cors_layer); } diff --git a/crates/librqbit/webui/app.js b/crates/librqbit/webui/app.js index c6464e5..73f141e 100644 --- a/crates/librqbit/webui/app.js +++ b/crates/librqbit/webui/app.js @@ -1,5 +1,5 @@ // Define API URL and base path -const apiUrl = window.origin == null ? 'http://localhost:3030' : ''; +const apiUrl = window.origin == 'null' ? 'http://localhost:3030' : ''; // Helper function for making API requests (async/await) async function makeRequest(method, path, data) { @@ -99,10 +99,7 @@ async function displayTorrents() { } // Replace the old content with the new one const outputDiv = document.getElementById('output'); - if (outputDiv) { - outputDiv.innerHTML = ''; - outputDiv.appendChild(torrentsContainer); - } + outputDiv.replaceChildren(torrentsContainer); } catch (error) { console.error(error); @@ -170,7 +167,7 @@ function clearErrorAlert() { async function init() { try { await displayTorrents(); - autoRefreshTorrents(5000); // Set the interval (in milliseconds), e.g., 5000 for every 5 seconds + autoRefreshTorrents(500); // Set the interval (in milliseconds), e.g., 5000 for every 5 seconds } catch (error) { console.error(error); diff --git a/crates/librqbit/webui/app.ts b/crates/librqbit/webui/app.ts index b81a2d2..db38354 100644 --- a/crates/librqbit/webui/app.ts +++ b/crates/librqbit/webui/app.ts @@ -1,5 +1,5 @@ // Define API URL and base path -const apiUrl = 'http://localhost:3030'; +const apiUrl = window.origin == 'null' ? 'http://localhost:3030' : ''; // Interface for the Torrent API response interface Torrent { @@ -149,10 +149,8 @@ async function displayTorrents(): Promise { // Replace the old content with the new one const outputDiv = document.getElementById('output'); - if (outputDiv) { - outputDiv.innerHTML = ''; - outputDiv.appendChild(torrentsContainer); - } + outputDiv.innerHTML = ''; + outputDiv.appendChild(torrentsContainer); } catch (error) { console.error(error); }