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) <noreply@anthropic.com>
This commit is contained in:
local
2026-03-28 16:00:43 +00:00
parent 1fde98ca79
commit 697e9dce23
2 changed files with 10 additions and 4 deletions

View File

@@ -26,12 +26,17 @@ builder.RootComponents.Add<App>("#app");
// content is appended after existing head elements. // content is appended after existing head elements.
builder.RootComponents.Add<HeadOutlet>("head::after"); builder.RootComponents.Add<HeadOutlet>("head::after");
// Read the API base URL from configuration. // Determine the API base URL by matching the protocol the app was loaded with.
// In Blazor WASM, configuration comes from wwwroot/appsettings.json. // 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. // IMPORTANT: wwwroot/ files are PUBLIC -- they are downloaded to the browser.
// Never put secrets (API keys, passwords) in appsettings.json for a WASM app. // Never put secrets (API keys, passwords) in appsettings.json for a WASM app.
// The API key lives server-side in the ChatAgent.Api project. // 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<ChatApiClient> registers a typed HttpClient using IHttpClientFactory. // AddHttpClient<ChatApiClient> registers a typed HttpClient using IHttpClientFactory.
// IHttpClientFactory manages the underlying HttpMessageHandler lifetime to prevent // IHttpClientFactory manages the underlying HttpMessageHandler lifetime to prevent

View File

@@ -1,3 +1,4 @@
{ {
"ApiBaseUrl": "https://localhost:7100" "ApiBaseUrl_Http": "http://localhost:7000",
"ApiBaseUrl_Https": "https://localhost:7100"
} }