From 5ba78f7c326fe5fd5edc2ad6dff7e33659cab562 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 30 Jan 2024 20:22:14 -0500 Subject: [PATCH] Updated and refactored --- .../Localization/CachedLocalizationService.cs | 5 +- .../Aggregations/LocalizationAggregation.cs | 56 +++++++++++-------- .../EShopOnAbpWebGatewayModule.cs | 2 + .../ReverseProxyBuilderExtensions.cs | 7 +-- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/CachedLocalizationService.cs b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/CachedLocalizationService.cs index 33f4a27c..e665f3be 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/CachedLocalizationService.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/Localization/CachedLocalizationService.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Collections.Immutable; using System.Linq; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.DependencyInjection; @@ -10,7 +9,7 @@ namespace EShopOnAbp.WebGateway.Aggregations.Localization; public class CachedLocalizationService : ISingletonDependency { - protected ConcurrentDictionary LocalizationDictionary { get; } + protected static ConcurrentDictionary LocalizationDictionary { get; private set; } public CachedLocalizationService() { @@ -32,6 +31,6 @@ public class CachedLocalizationService : ISingletonDependency { return LocalizationDictionary .Where(kv => serviceNamesWithCulture.Contains(kv.Key)) - .ToImmutableDictionary(); + .ToDictionary(); } } \ No newline at end of file diff --git a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/LocalizationAggregation.cs b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/LocalizationAggregation.cs index f2a004e9..aeb813fa 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/LocalizationAggregation.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/Aggregations/LocalizationAggregation.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Net.Http; -using System.Text.Json; using System.Threading.Tasks; using EShopOnAbp.WebGateway.Aggregations.Localization; using Microsoft.Extensions.Logging; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; -using Volo.Abp.Caching; -using Volo.Abp.DependencyInjection; namespace EShopOnAbp.WebGateway.Aggregations; @@ -29,36 +24,51 @@ public class LocalizationAggregation : ILocalizationAggregation public async Task GetLocalizationAsync(LocalizationRequest input) { - //Temp list - Dictionary services = new Dictionary() - { - { "Administration_tr", "https://localhost:44353/api/abp/application-localization?cultureName=tr&onlyDynamics=false" }, - { "Catalog_tr", "https://localhost:44354/api/abp/application-localization?cultureName=tr&onlyDynamics=false" } - }; - - // Check the cache - var cachedLocalization = _cachedLocalizationService.GetMultipleLocalizationsAsync(services.Keys.ToArray()); + // Check the cache service + var cachedLocalization = _cachedLocalizationService.GetMultipleLocalizationsAsync(input.LocalizationEndpoints.Keys.ToArray()); // Compare cache with input service list + var missingLocalizationKeys = GetMissingLocalizations(cachedLocalization, input.LocalizationEndpoints); - // Make request to remote localization service - var remoteLocalizationResults = await _remoteLocalizationService.GetMultipleLocalizationsAsync(services); + if (missingLocalizationKeys.Count != 0) + { + // Make request to remote localization service + var remoteLocalizationResults = await _remoteLocalizationService + .GetMultipleLocalizationsAsync(input.LocalizationEndpoints + .Where(kv=> missingLocalizationKeys.Contains(kv.Key)) + .ToDictionary(k=>k.Key,v=>v.Value)); + foreach (var result in remoteLocalizationResults) + { + _cachedLocalizationService.AddOrUpdate(result.Key, result.Value); + } + cachedLocalization = _cachedLocalizationService.GetMultipleLocalizationsAsync(input.LocalizationEndpoints.Keys.ToArray()); + } //merge result - ApplicationLocalizationDto mergedResult = MergeLocalizations(remoteLocalizationResults); + ApplicationLocalizationDto mergedResult = MergeLocalizations(cachedLocalization); //return result - return mergedResult; } - private ApplicationLocalizationDto MergeLocalizations(Dictionary localizationResults) + private List GetMissingLocalizations(IDictionary serviceNameWithLocalization, Dictionary serviceNameWithUrls) + { + List missingKeysInCache = serviceNameWithUrls.Keys.Except(serviceNameWithLocalization.Keys).ToList(); + List missingKeysInUrls = serviceNameWithLocalization.Keys.Except(serviceNameWithUrls.Keys).ToList(); + + return missingKeysInCache.Concat(missingKeysInUrls).ToList(); + } + + private ApplicationLocalizationDto MergeLocalizations(IDictionary localizationResults) { var localizationDto = new ApplicationLocalizationDto(); - //TODO: fix - foreach (var localizationResult in localizationResults) + + foreach (var localization in localizationResults) { - localizationDto.Resources.AddIfNotContains(localizationResult.Value.Resources); + foreach (var resource in localization.Value.Resources) + { + localizationDto.Resources.TryAdd(resource.Key, resource.Value); + } } return localizationDto; diff --git a/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs b/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs index 96c9a855..984e1a2a 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Hosting; using System; using System.Linq; using EShopOnAbp.WebGateway.Aggregations; +using EShopOnAbp.WebGateway.Aggregations.Localization; using Microsoft.AspNetCore.Http; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; @@ -69,6 +70,7 @@ public class EShopOnAbpWebGatewayModule : AbpModule } app.UseCorrelationId(); + app.UseCors(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); diff --git a/gateways/web/src/EShopOnAbp.WebGateway/ReverseProxyBuilderExtensions.cs b/gateways/web/src/EShopOnAbp.WebGateway/ReverseProxyBuilderExtensions.cs index 7601ff6a..b025dbe3 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/ReverseProxyBuilderExtensions.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/ReverseProxyBuilderExtensions.cs @@ -1,21 +1,19 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Text.Json; using EShopOnAbp.WebGateway.Aggregations; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Yarp.ReverseProxy.Configuration; namespace EShopOnAbp.WebGateway; public static class ReverseProxyBuilderExtensions { - public static string LocalizationPath = "/api/abp/application-localization?cultureName=tr&onlyDynamics=false"; + public static string LocalizationPath = "api/abp/application-localization"; public static ReverseProxyConventionBuilder MapReverseProxyWithLocalization(this IEndpointRouteBuilder endpoints) { @@ -53,11 +51,12 @@ public static class ReverseProxyBuilderExtensions context.Request.Query.TryGetValue("CultureName", out var cultureName); var input = new LocalizationRequest(cultureName); + string path = $"{LocalizationPath}?cultureName={cultureName}&onlyDynamics=false"; var clusterList = GetClusters(proxyConfig); foreach (var cluster in clusterList) { - var hostUrl = new Uri(cluster.Value.Address) + $"{LocalizationPath}"; + var hostUrl = new Uri(cluster.Value.Address) + $"{path}"; input.LocalizationEndpoints.Add($"{cluster.Key}_{cultureName}", hostUrl); }