From 697e9dce23670fd1b580bb9d2b1504817425b5df Mon Sep 17 00:00:00 2001 From: local Date: Sat, 28 Mar 2026 16:00:43 +0000 Subject: [PATCH] fix(01-02): auto-match API URL protocol to client load protocol Client now detects whether it was loaded over HTTP or HTTPS and selects the corresponding API base URL, so both protocols work without manual config changes. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/ChatAgent.Client/Program.cs | 11 ++++++++--- src/ChatAgent.Client/wwwroot/appsettings.json | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ChatAgent.Client/Program.cs b/src/ChatAgent.Client/Program.cs index 2d60c4e..83ce5ba 100644 --- a/src/ChatAgent.Client/Program.cs +++ b/src/ChatAgent.Client/Program.cs @@ -26,12 +26,17 @@ builder.RootComponents.Add("#app"); // content is appended after existing head elements. builder.RootComponents.Add("head::after"); -// Read the API base URL from configuration. -// In Blazor WASM, configuration comes from wwwroot/appsettings.json. +// Determine the API base URL by matching the protocol the app was loaded with. +// In Blazor WASM, builder.HostEnvironment.BaseAddress is the URL the browser +// used to load the app (e.g., "http://localhost:5100/" or "https://localhost:5200/"). +// We check if the app was loaded over HTTPS and pick the corresponding API URL. // IMPORTANT: wwwroot/ files are PUBLIC -- they are downloaded to the browser. // Never put secrets (API keys, passwords) in appsettings.json for a WASM app. // The API key lives server-side in the ChatAgent.Api project. -var apiBaseUrl = builder.Configuration["ApiBaseUrl"] ?? "https://localhost:7100"; +var isHttps = builder.HostEnvironment.BaseAddress.StartsWith("https://", StringComparison.OrdinalIgnoreCase); +var apiBaseUrl = isHttps + ? builder.Configuration["ApiBaseUrl_Https"] ?? "https://localhost:7100" + : builder.Configuration["ApiBaseUrl_Http"] ?? "http://localhost:7000"; // AddHttpClient registers a typed HttpClient using IHttpClientFactory. // IHttpClientFactory manages the underlying HttpMessageHandler lifetime to prevent diff --git a/src/ChatAgent.Client/wwwroot/appsettings.json b/src/ChatAgent.Client/wwwroot/appsettings.json index c542028..64ceea0 100644 --- a/src/ChatAgent.Client/wwwroot/appsettings.json +++ b/src/ChatAgent.Client/wwwroot/appsettings.json @@ -1,3 +1,4 @@ { - "ApiBaseUrl": "https://localhost:7100" + "ApiBaseUrl_Http": "http://localhost:7000", + "ApiBaseUrl_Https": "https://localhost:7100" }