diff --git a/src/CodeBreaker.Blazor.Client/Configuration/RemoteServiceDiscovery.cs b/src/CodeBreaker.Blazor.Client/Configuration/RemoteServiceDiscovery.cs deleted file mode 100644 index 6d40468..0000000 --- a/src/CodeBreaker.Blazor.Client/Configuration/RemoteServiceDiscovery.cs +++ /dev/null @@ -1,106 +0,0 @@ -using Microsoft.AspNetCore.Components.WebAssembly.Hosting; -using System.Net.Http.Json; - -namespace CodeBreaker.Blazor.Client.Configuration; - -/// -/// A configuration provider that retrieves the URL of the gateway service from the remote Blazor application. -/// -internal class RemoteServiceDiscovery(Uri baseAddress) : ConfigurationProvider -{ - /// - /// Loads the configuration by retrieving the URLs from the remote service discovery endpoint. - /// - public override async void Load() - { - using var httpClient = new HttpClient() - { - BaseAddress = baseAddress - }; - var urls = await httpClient.GetFromJsonAsync>("service-discovery"); - - if (urls is null) - return; - - foreach (var (key, value) in urls) - Set($"services:{key}", value); - } -} - -internal class RemoteServiceDiscoveryConfigurationSource(Uri baseAddress) : IConfigurationSource -{ - /// - /// Builds the configuration provider using the specified configuration builder. - /// - public IConfigurationProvider Build(IConfigurationBuilder builder) => - new RemoteServiceDiscovery(baseAddress); -} - -internal static class RemoteServiceDiscoveryConfigurationExtensions -{ - /// - /// Adds the remote service discovery configuration provider to the configuration builder with the specified base address. - /// - public static IConfigurationBuilder AddRemoteServiceDiscovery(this IConfigurationBuilder builder, Uri baseAddress) - { - builder.Add(new RemoteServiceDiscoveryConfigurationSource(baseAddress)); - return builder; - } - - /// - /// Adds the remote service discovery configuration provider to the configuration builder with the base address from the specified host environment. - /// - public static IConfigurationBuilder AddRemoteServiceDiscovery(this IConfigurationBuilder builder, IWebAssemblyHostEnvironment hostEnvironment) - { - builder.Add(new RemoteServiceDiscoveryConfigurationSource(new(hostEnvironment.BaseAddress))); - return builder; - } - - /// - /// Adds the remote service discovery configuration provider to the configuration builder with the base address from the specified host builder. - /// - public static IConfigurationBuilder AddRemoteServiceDiscovery(this IConfigurationBuilder builder, WebAssemblyHostBuilder hostBuilder) - { - builder.Add(new RemoteServiceDiscoveryConfigurationSource(new(hostBuilder.HostEnvironment.BaseAddress))); - return builder; - } -} - -internal static class ServiceDiscoveryHttpClientBuilderExtensions -{ - /// - /// Configures the HttpClient to use the service discovery endpoint to replace the base address with the service endpoint based on the configuration. - /// - public static IHttpClientBuilder ConfigureRemoteServiceDiscovery(this IHttpClientBuilder httpClientBuilder) - { - httpClientBuilder.ConfigureHttpClient((services, HttpClient) => - { - var configuredBaseAddress = HttpClient.BaseAddress; - - // If the base address is not configured or is not a DNS host name, do nothing - if (configuredBaseAddress is null || configuredBaseAddress.HostNameType != UriHostNameType.Dns) - return; - - var oldHostName = configuredBaseAddress.Host; // The old host name represents the service name (e.g. "backend" or "gateway") - var configuration = services.GetRequiredService(); - - var serviceEndpoint = configuration[$"services:{oldHostName}"]; - - // If no service endpoint is found, do nothing and use the configured base address - if (serviceEndpoint is null) - return; - - var serviceEndpointUri = new Uri(serviceEndpoint); - - // Replace the old host name with the new host name - HttpClient.BaseAddress = new UriBuilder(configuredBaseAddress) - { - Scheme = serviceEndpointUri.Scheme, - Host = serviceEndpointUri.Host, - Port = serviceEndpointUri.Port - }.Uri; - }); - - return httpClientBuilder; - } -} \ No newline at end of file diff --git a/src/CodeBreaker.Blazor.Client/Program.cs b/src/CodeBreaker.Blazor.Client/Program.cs index 0882732..f83e4a7 100644 --- a/src/CodeBreaker.Blazor.Client/Program.cs +++ b/src/CodeBreaker.Blazor.Client/Program.cs @@ -1,6 +1,5 @@ using BlazorApplicationInsights; using Codebreaker.GameAPIs.Client; -using CodeBreaker.Blazor.Client.Configuration; using CodeBreaker.Blazor.Client.Contracts.Services; using CodeBreaker.Blazor.Client.Extensions; using CodeBreaker.Blazor.Client.Services; @@ -10,8 +9,6 @@ var builder = WebAssemblyHostBuilder.CreateDefault(args); -builder.Configuration.AddRemoteServiceDiscovery(new Uri (builder.HostEnvironment.BaseAddress)); - builder.Services.AddFluentUIComponents(); builder.Services.AddLocalization(); @@ -33,14 +30,12 @@ builder.Services.AddTransient(); builder.Services.AddHttpClient(configure => - configure.BaseAddress = new Uri("https://gateway/users/public") -) -.ConfigureRemoteServiceDiscovery(); + configure.BaseAddress = new Uri(builder.Configuration.GetRequired("UserServicePublicBaseAddress")) +); builder.Services.AddHttpClient(configure => - configure.BaseAddress = new Uri("https://gateway/games/") + configure.BaseAddress = new Uri(builder.Configuration.GetRequired("GameServiceBaseAddress")) ) -.ConfigureRemoteServiceDiscovery() .AddHttpMessageHandler(); builder.Services.AddScoped(); diff --git a/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.Development.json b/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.Development.json index b909150..4dd2f1e 100644 --- a/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.Development.json +++ b/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.Development.json @@ -1,3 +1,6 @@ { - "DetailedErrors": true + "DetailedErrors": true, + "AzureAdB2C:RedirectUri": "http://localhost:5208/authentication/login-callback", + "GameServiceBaseAddress": "http://localhost:5011/games", + "UserServicePublicBaseAddress": "http://localhost:5011/users/public" } diff --git a/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.json b/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.json index b860218..085d8cb 100644 --- a/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.json +++ b/src/CodeBreaker.Blazor.Client/wwwroot/appsettings.json @@ -2,6 +2,9 @@ "AzureAdB2C": { "Authority": "https://codebreaker3000.b2clogin.com/codebreaker3000.onmicrosoft.com/B2C_1_SUSI", "ClientId": "558b37b7-0a71-4463-9d3c-d74220b7e5ee", - "ValidateAuthority": false - } + "ValidateAuthority": false, + "RedirectUri": "https://blazor.codebreaker.app/authentication/login-callback" + }, + "GameServiceBaseAddress": "https://codebreaker.cninnovation.com/games", + "UserServicePublicBaseAddress": "https://codebreaker.cninnovation.com/users/public" } diff --git a/src/CodeBreaker.Blazor/Program.cs b/src/CodeBreaker.Blazor/Program.cs index fbadbe2..3c23ace 100644 --- a/src/CodeBreaker.Blazor/Program.cs +++ b/src/CodeBreaker.Blazor/Program.cs @@ -1,6 +1,7 @@ using CodeBreaker.Blazor.Client.Pages; using Codebreaker.GameAPIs.Client; using CodeBreaker.Blazor.Components; +using CodeBreaker.Blazor.Client.Extensions; using CodeBreaker.Blazor.Client.Services; using CodeBreaker.Blazor.Client.Contracts.Services; using Microsoft.FluentUI.AspNetCore.Components; @@ -19,11 +20,11 @@ builder.Services.AddHttpClient(configure => configure.BaseAddress = - new Uri("https://userapis") // Utilize Aspire service discovery + new Uri(builder.Configuration.GetRequired("UserServicePublicBaseAddress")) ); builder.Services.AddHttpClient(configure => - configure.BaseAddress = new Uri("https://gameapis") // Utilize Aspire service discovery + configure.BaseAddress = new Uri(builder.Configuration.GetRequired("GameServiceBaseAddress")) ); builder.Services.AddScoped(); @@ -57,31 +58,4 @@ .AddInteractiveWebAssemblyRenderMode() .AddAdditionalAssemblies(typeof(GamePage).Assembly); -/// -/// Reads the environment variables for services and returns a mapping of service names to URLs. -/// -app.MapGet("/service-discovery", () => -{ - var serviceMapping = new Dictionary(); - var environmentVariables = Environment.GetEnvironmentVariables(); - - foreach (var key in environmentVariables.Keys) - { - var keyText = key.ToString()!; - - // Check if the key is a service URL (e.g. services__gameapis__http) - if (keyText.StartsWith("services__")) - { - // Extract the service name from the key (e.g. gameapis - var serviceName = keyText.Split("__")[1]; - - // Add the service name and URL to the mapping if it doesn't already exist - if (!serviceMapping.ContainsKey(serviceName)) - serviceMapping.Add(serviceName, environmentVariables[key]!.ToString()!); - } - } - - return Results.Json(serviceMapping); -}); - app.Run(); \ No newline at end of file diff --git a/src/CodeBreaker.Blazor/appsettings.Development.json b/src/CodeBreaker.Blazor/appsettings.Development.json index b909150..b135478 100644 --- a/src/CodeBreaker.Blazor/appsettings.Development.json +++ b/src/CodeBreaker.Blazor/appsettings.Development.json @@ -1,3 +1,5 @@ { - "DetailedErrors": true + "DetailedErrors": true, + "GameServiceBaseAddress": "http://localhost:5011/games", + "UserServicePublicBaseAddress": "http://localhost:5011/users/public" } diff --git a/src/CodeBreaker.Blazor/appsettings.json b/src/CodeBreaker.Blazor/appsettings.json index 2c63c08..148ac35 100644 --- a/src/CodeBreaker.Blazor/appsettings.json +++ b/src/CodeBreaker.Blazor/appsettings.json @@ -1,2 +1,4 @@ { + "GameServiceBaseAddress": "https://codebreaker.cninnovation.com/games", + "UserServicePublicBaseAddress": "https://codebreaker.cninnovation.com/users/public" }