From 7ff3f43c93ed90a277e87ceb0a95e2f6383661ba Mon Sep 17 00:00:00 2001 From: Dani Date: Thu, 21 Aug 2025 22:57:48 -0400 Subject: [PATCH] Troubleshooting the cloudflare and the api not communicating --- client/app.js | 86 ++++++++++++++++++++++------------------- docker-compose.test.yml | 3 +- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/client/app.js b/client/app.js index f2436a9..1d60f68 100644 --- a/client/app.js +++ b/client/app.js @@ -1,5 +1,39 @@ import { encryptString, decryptToString, toBlob } from "./crypto.js"; +// ---- Helpers ---- +function defaultApiBase() { + // 1) URL query override: …/index.html?api=https://api.domain + try { + const qs = new URLSearchParams(window.location.search); + const qApi = qs.get("api"); + if (qApi) return qApi.replace(/\/+$/, ""); + } catch {} + + // 2) Meta override in index.html: + const m = document.querySelector('meta[name="gc-api-base"]'); + if (m && m.content) return m.content.replace(/\/+$/, ""); + + // 3) Heuristic from frontend origin + try { + const u = new URL(window.location.href); + const proto = u.protocol; + const host = u.hostname; + const portStr = u.port; + const bracketHost = host.includes(":") ? `[${host}]` : host; + + const port = portStr ? parseInt(portStr, 10) : null; + let apiPort = port; + if (port === 8082) apiPort = 8080; + else if (port === 9082) apiPort = 9080; + else if (port) apiPort = Math.max(1, port - 2); + + return apiPort ? `${proto}//${bracketHost}:${apiPort}` : `${proto}//${bracketHost}`; + } catch { + return window.location.origin.replace(/\/+$/, ""); + } +} + +// ---- DOM refs ---- const els = { shardUrl: document.getElementById("shardUrl"), bearer: document.getElementById("bearer"), @@ -15,45 +49,20 @@ const els = { discordStart: document.getElementById("discordStart"), }; -function defaultApiBase() { - // 1) URL query override: …/index.html?api=http://host:9080 - try { - const qs = new URLSearchParams(window.location.search); - const qApi = qs.get("api"); - if (qApi) return qApi.replace(/\/+$/, ""); - } catch {} - - // 2) Meta override in index.html: - const m = document.querySelector('meta[name="gc-api-base"]'); - if (m && m.content) return m.content.replace(/\/+$/, ""); - - // 3) Heuristic from frontend origin - try { - const u = new URL(window.location.href); - const proto = u.protocol; - const host = u.hostname; // no port - const portStr = u.port; // "" if default (80/443) - const bracketHost = host.includes(":") ? `[${host}]` : host; // IPv6-safe - - const port = portStr ? parseInt(portStr, 10) : null; - let apiPort = port; - - // Known frontend→API mappings - if (port === 8082) apiPort = 8080; - else if (port === 9082) apiPort = 9080; - else if (port) apiPort = Math.max(1, port - 2); // generic “minus two” fallback - - return apiPort ? `${proto}//${bracketHost}:${apiPort}` : `${proto}//${bracketHost}`; - } catch { - return window.location.origin.replace(/\/+$/, ""); - } -} - - +// ---- Config + state ---- const LS_KEY = "gc_client_config_v1"; const POSTS_KEY = "gc_posts_index_v1"; -const cfg = loadConfig(); applyConfig(); checkHealth(); syncIndex(); sse(); +// IMPORTANT: define before sse() is ever called +let sseCtrl = null; + +// ---- Boot ---- +const cfg = loadConfig(); +applyConfig(); +checkHealth(); +syncIndex(); +sse(); + els.saveConn.onclick = async () => { const c = { url: norm(els.shardUrl.value), bearer: els.bearer.value.trim(), passphrase: els.passphrase.value }; @@ -69,9 +78,8 @@ function getPosts(){ try { return JSON.parse(localStorage.getItem(POSTS_KEY)) ?? function setPosts(v){ localStorage.setItem(POSTS_KEY, JSON.stringify(v)); renderPosts(); } function norm(u){ return (u||"").replace(/\/+$/,""); } function applyConfig() { - // If no URL saved yet, detect a sensible default and persist it if (!cfg.url) { - const detected = defaultApiBase(); // uses ?api=…, , or port heuristic + const detected = defaultApiBase(); cfg.url = detected; try { localStorage.setItem(LS_KEY, JSON.stringify(cfg)); } catch {} } @@ -80,6 +88,7 @@ function applyConfig() { els.passphrase.value = cfg.passphrase ?? ""; } + async function checkHealth() { if (!cfg.url) return; els.health.textContent = "Checking…"; try { const r = await fetch(cfg.url + "/healthz"); els.health.textContent = r.ok ? "Connected ✔" : `Error: ${r.status}`; } @@ -122,7 +131,6 @@ async function syncIndex() { } catch(e){ console.warn("index sync failed", e); } } -let sseCtrl; function sse(){ if (!cfg.url) return; if (sseCtrl) { sseCtrl.abort(); sseCtrl = undefined; } diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 0c32e20..b9086e0 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -6,7 +6,7 @@ services: container_name: greencoast-shard-test restart: unless-stopped user: "0:0" - # You can keep these published for local debugging; Tunnel doesn't require them. + # These ports are optional (useful for local debug). Tunnel doesn't need them. ports: - "9080:9080" # API - "9082:9082" # Frontend @@ -19,7 +19,6 @@ services: cloudflared: image: cloudflare/cloudflared:latest - # Use the token you copy from Cloudflare Zero Trust → Tunnels command: tunnel --no-autoupdate run --token ${CF_TUNNEL_TOKEN} restart: unless-stopped depends_on: