From d05865c86addf0727ac335b5ae3a37ecc40ffcdf Mon Sep 17 00:00:00 2001 From: pengjuyan <42963214+codding-y@users.noreply.github.com> Date: Thu, 2 Mar 2023 11:12:07 +0800 Subject: [PATCH] feat(stacksdks.config): refactor stack config (#476) * fix: change AddMasaStackConfig to AddMasaStackConfigAsync * feat(masa-stack-config): refactor masa stack config * fix: fix Test error * fix: fix TEST error * feat:modify getvalue method --------- Co-authored-by: yanpengju Co-authored-by: Mayue --- .../IMasaStackConfig.cs | 4 +- .../MasaStackConfig.cs | 39 +++++++++++----- .../MasaStackConfigExtensions.cs | 45 ++----------------- .../MasaStackConfigOptions.cs | 2 + .../ServiceCollectionExtensions.cs | 43 +++++++++--------- .../MasaStackConfigTest.cs | 8 +--- .../EventMiddlewareTest.cs | 19 +++++++- .../RequestMiddlewareTest.cs | 19 +++++++- .../appsettings.json | 16 ++++++- 9 files changed, 108 insertions(+), 87 deletions(-) diff --git a/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs b/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs index 7968e9208..9c1206819 100644 --- a/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs +++ b/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs @@ -33,9 +33,7 @@ public interface IMasaStackConfig string GetValue(string key); - void SetValue(string key, string value); - - void SetValues(Dictionary configMap); + Dictionary GetValues(); List GetProjectList(); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs index 606c0ffde..b49156ac0 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs @@ -5,15 +5,14 @@ namespace Masa.Contrib.StackSdks.Config; public class MasaStackConfig : IMasaStackConfig { - public MasaStackConfig(IConfigurationApiClient client) - { - var configs = client.GetAsync>( - Environment, - Cluster, - DEFAULT_PUBLIC_ID, - DEFAULT_CONFIG_NAME).ConfigureAwait(false).GetAwaiter().GetResult(); + private IConfigurationApiClient _configurationApiClient; + + private static ConcurrentDictionary ConfigMap { get; set; } = new(StringComparer.OrdinalIgnoreCase); - MasaStackConfigOptions.SetValues(configs); + public MasaStackConfig(IConfigurationApiClient client, Dictionary configs) + { + _configurationApiClient = client; + ConfigMap = new(configs); } public RedisModel RedisModel @@ -58,9 +57,27 @@ public ElasticModel ElasticModel public List GetProjectList() => this.GetAllServer().Keys.ToList(); - public string GetValue(string key) => MasaStackConfigOptions.GetValue(key); + public string GetValue(string key) + { + GetValues().TryGetValue(key, out var value); + return value ?? ConfigMap[key]; + } - public void SetValue(string key, string value) => MasaStackConfigOptions.SetValue(key, value); + public Dictionary GetValues() + { + try + { + var remoteConfigs = _configurationApiClient.GetAsync>( + ConfigMap[MasaStackConfigConstant.ENVIRONMENT], + ConfigMap[MasaStackConfigConstant.CLUSTER], + DEFAULT_PUBLIC_ID, + DEFAULT_CONFIG_NAME).ConfigureAwait(false).GetAwaiter().GetResult(); - public void SetValues(Dictionary configMap) => MasaStackConfigOptions.SetValues(configMap); + return remoteConfigs; + } + catch (ArgumentException) + { + return new(ConfigMap); + } + } } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs index e434d897c..403e31631 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs @@ -7,12 +7,7 @@ public static class MasaStackConfigExtensions { public static Dictionary GetAllServer(this IMasaStackConfig masaStackConfig) { - var value = masaStackConfig.GetValue(MasaStackConfigConstant.MASA_SERVER); - if (string.IsNullOrEmpty(value)) - { - return new(); - } - return JsonSerializer.Deserialize>(value) ?? new(); + return MasaStackConfigUtils.GetAllServer(masaStackConfig.GetValues()); } public static Dictionary GetAllUI(this IMasaStackConfig masaStackConfig) @@ -50,17 +45,7 @@ public static string GetConnectionString(this IMasaStackConfig masaStackConfig, public static string GetServerDomain(this IMasaStackConfig masaStackConfig, string protocol, string project, string service) { - var domain = ""; - GetAllServer(masaStackConfig).TryGetValue(project, out JsonObject? jsonObject); - if (jsonObject != null) - { - var secondaryDomain = jsonObject[service]?.ToString(); - if (secondaryDomain != null) - { - domain = $"{protocol}://{secondaryDomain}.{masaStackConfig.Namespace}"; - } - } - return domain.TrimEnd('.'); + return MasaStackConfigUtils.GetServerDomain(masaStackConfig.GetValues(), protocol, project, service); } public static string GetUIDomain(this IMasaStackConfig masaStackConfig, string protocol, string project, string service) @@ -90,7 +75,7 @@ public static string GetPmServiceDomain(this IMasaStackConfig masaStackConfig) public static string GetDccServiceDomain(this IMasaStackConfig masaStackConfig) { - return GetServerDomain(masaStackConfig, HttpProtocol.HTTP, MasaStackConstant.DCC, MasaStackConstant.SERVER); + return MasaStackConfigUtils.GetDccServiceDomain(masaStackConfig.GetValues()); } public static string GetTscServiceDomain(this IMasaStackConfig masaStackConfig) @@ -151,30 +136,6 @@ public static string GetWebId(this IMasaStackConfig masaStackConfig, string proj return obj?[service]?.ToString() ?? ""; } - public static DccOptions GetDefaultDccOptions(this IMasaStackConfig masaStackConfig) - { - var dccServerAddress = GetDccServiceDomain(masaStackConfig); - var redis = masaStackConfig.RedisModel ?? throw new Exception("redis options can not null"); - - var options = new DccOptions - { - ManageServiceAddress = dccServerAddress, - RedisOptions = new Caching.Distributed.StackExchangeRedis.RedisConfigurationOptions - { - Servers = new List - { - new Caching.Distributed.StackExchangeRedis.RedisServerOptions(redis.RedisHost,redis.RedisPort) - }, - DefaultDatabase = redis.RedisDb, - Password = redis.RedisPassword - }, - PublicSecret = masaStackConfig.DccSecret, - ConfigObjectSecret = masaStackConfig.DccSecret - }; - - return options; - } - public static Guid GetDefaultUserId(this IMasaStackConfig masaStackConfig) { return CreateGuid(masaStackConfig.Namespace); diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs index 2da6c460f..847159ad1 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs @@ -15,6 +15,8 @@ public static string GetValue(string key, Func defaultFunc) return defaultFunc.Invoke(); } + public static Dictionary GetValues() => new(ConfigMap); + public static void SetValue(string key, string value) => ConfigMap[key] = value; public static void SetValues(Dictionary configMap) diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs index 5aaa18a11..2340de9b3 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs @@ -7,8 +7,7 @@ public static class ServiceCollectionExtensions { private static async Task InitializeMasaStackConfiguration( IServiceCollection services, - Dictionary configs, - bool init) + Dictionary configs) { var serviceProvider = services.BuildServiceProvider(); var configurationApiManage = serviceProvider.GetRequiredService(); @@ -22,7 +21,7 @@ private static async Task InitializeMasaStackConfiguration( DEFAULT_PUBLIC_ID, DEFAULT_CONFIG_NAME); - if (remoteConfigs != null && init) + if (remoteConfigs != null) { await configurationApiManage.UpdateAsync( configs[MasaStackConfigConstant.ENVIRONMENT], @@ -34,17 +33,15 @@ await configurationApiManage.UpdateAsync( } catch (ArgumentException) { - if (init) - { - await configurationApiManage.AddAsync( - configs[MasaStackConfigConstant.ENVIRONMENT], - configs[MasaStackConfigConstant.CLUSTER], - DEFAULT_PUBLIC_ID, - new Dictionary - { - { DEFAULT_CONFIG_NAME, JsonSerializer.Serialize(configs) } - }); - } + // remoteConfigs is null + await configurationApiManage.AddAsync( + configs[MasaStackConfigConstant.ENVIRONMENT], + configs[MasaStackConfigConstant.CLUSTER], + DEFAULT_PUBLIC_ID, + new Dictionary + { + { DEFAULT_CONFIG_NAME, JsonSerializer.Serialize(configs) } + }); } } @@ -54,12 +51,15 @@ public static async Task AddMasaStackConfigAsync(this IServi var dccOptions = MasaStackConfigUtils.GetDefaultDccOptions(configs); services.AddMasaConfiguration(builder => builder.UseDcc(dccOptions)); - await InitializeMasaStackConfiguration(services, configs, init).ConfigureAwait(false); + if (init) + { + await InitializeMasaStackConfiguration(services, configs).ConfigureAwait(false); + } services.TryAddScoped(serviceProvider => { - var client = serviceProvider.GetRequiredService(); - return new MasaStackConfig(client); + var configurationApiClient = serviceProvider.GetRequiredService(); + return new MasaStackConfig(configurationApiClient, configs); }); return services; @@ -68,14 +68,17 @@ public static async Task AddMasaStackConfigAsync(this IServi public static async Task AddMasaStackConfigAsync(this IServiceCollection services, DccOptions dccOptions, bool init = false) { services.AddMasaConfiguration(builder => builder.UseDcc(dccOptions)); - var configs = GetConfigMap(services); - await InitializeMasaStackConfiguration(services, configs, init).ConfigureAwait(false); + + if (init) + { + await InitializeMasaStackConfiguration(services, configs).ConfigureAwait(false); + } services.TryAddScoped(serviceProvider => { var client = serviceProvider.GetRequiredService(); - return new MasaStackConfig(client); + return new MasaStackConfig(client, configs); }); return services; diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/MasaStackConfigTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/MasaStackConfigTest.cs index fdb063436..05e3c8820 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/MasaStackConfigTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/MasaStackConfigTest.cs @@ -38,14 +38,14 @@ public void Initialize() Mock dccClient = new(); - dccClient.Setup(aa => aa.GetAsync( + dccClient.Setup(client => client.GetAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny>>()!)) .ReturnsAsync(_config); - _stackConfig = new MasaStackConfig(dccClient.Object); + _stackConfig = new MasaStackConfig(dccClient.Object, _config); } @@ -53,19 +53,15 @@ public void Initialize() public void TestGetAllServers() { var allServer = _stackConfig.GetAllServer(); - var allServer1 = MasaStackConfigUtils.GetAllServer(_config); Assert.IsNotNull(allServer); - Assert.IsNotNull(allServer1); } [TestMethod] public void TestGetDefaultDccOptions() { - var dccOptions = _stackConfig.GetDefaultDccOptions(); var dccOptions1 = MasaStackConfigUtils.GetDefaultDccOptions(_config); - Assert.IsNotNull(dccOptions?.RedisOptions); Assert.IsNotNull(dccOptions1?.RedisOptions); } diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/EventMiddlewareTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/EventMiddlewareTest.cs index 2c206d0e0..5a6d4959e 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/EventMiddlewareTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/EventMiddlewareTest.cs @@ -19,9 +19,24 @@ public EventMiddlewareTest() }); Mock dccClient = new(); + var configuration = builder.Configuration; var configs = new Dictionary() { - { MasaStackConfigConstant.IS_DEMO, builder.Configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString() } + { MasaStackConfigConstant.VERSION, configuration.GetValue(MasaStackConfigConstant.VERSION) }, + { MasaStackConfigConstant.IS_DEMO, configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString() }, + { MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue(MasaStackConfigConstant.DOMAIN_NAME) }, + { MasaStackConfigConstant.NAMESPACE, configuration.GetValue(MasaStackConfigConstant.NAMESPACE) }, + { MasaStackConfigConstant.TLS_NAME, configuration.GetValue(MasaStackConfigConstant.TLS_NAME) }, + { MasaStackConfigConstant.CLUSTER, configuration.GetValue(MasaStackConfigConstant.CLUSTER) }, + { MasaStackConfigConstant.OTLP_URL, configuration.GetValue(MasaStackConfigConstant.OTLP_URL) }, + { MasaStackConfigConstant.REDIS, configuration.GetValue(MasaStackConfigConstant.REDIS) }, + { MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue(MasaStackConfigConstant.CONNECTIONSTRING) }, + { MasaStackConfigConstant.MASA_SERVER, configuration.GetValue(MasaStackConfigConstant.MASA_SERVER) }, + { MasaStackConfigConstant.MASA_UI, configuration.GetValue(MasaStackConfigConstant.MASA_UI) }, + { MasaStackConfigConstant.ELASTIC, configuration.GetValue(MasaStackConfigConstant.ELASTIC) }, + { MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue(MasaStackConfigConstant.ENVIRONMENT) }, + { MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue(MasaStackConfigConstant.ADMIN_PWD) }, + { MasaStackConfigConstant.DCC_SECRET, configuration.GetValue(MasaStackConfigConstant.DCC_SECRET) } }; dccClient.Setup(aa => aa.GetAsync( It.IsAny(), @@ -32,7 +47,7 @@ public EventMiddlewareTest() builder.Services.AddSingleton(serviceProvider => { - return new MasaStackConfig(dccClient.Object); + return new MasaStackConfig(dccClient.Object, configs); }); builder.Services.AddTestEventBus(new Assembly[1] { Assembly.GetExecutingAssembly() }, ServiceLifetime.Scoped); builder.Services.AddStackMiddleware(); diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/RequestMiddlewareTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/RequestMiddlewareTest.cs index ea2a6caf8..b433b6239 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/RequestMiddlewareTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/RequestMiddlewareTest.cs @@ -13,9 +13,24 @@ public RequestMiddlewareTest() var builder = WebApplication.CreateBuilder(); Mock dccClient = new(); + var configuration = builder.Configuration; var configs = new Dictionary() { - { MasaStackConfigConstant.IS_DEMO, builder.Configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString() } + { MasaStackConfigConstant.VERSION, configuration.GetValue(MasaStackConfigConstant.VERSION) }, + { MasaStackConfigConstant.IS_DEMO, configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString() }, + { MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue(MasaStackConfigConstant.DOMAIN_NAME) }, + { MasaStackConfigConstant.NAMESPACE, configuration.GetValue(MasaStackConfigConstant.NAMESPACE) }, + { MasaStackConfigConstant.TLS_NAME, configuration.GetValue(MasaStackConfigConstant.TLS_NAME) }, + { MasaStackConfigConstant.CLUSTER, configuration.GetValue(MasaStackConfigConstant.CLUSTER) }, + { MasaStackConfigConstant.OTLP_URL, configuration.GetValue(MasaStackConfigConstant.OTLP_URL) }, + { MasaStackConfigConstant.REDIS, configuration.GetValue(MasaStackConfigConstant.REDIS) }, + { MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue(MasaStackConfigConstant.CONNECTIONSTRING) }, + { MasaStackConfigConstant.MASA_SERVER, configuration.GetValue(MasaStackConfigConstant.MASA_SERVER) }, + { MasaStackConfigConstant.MASA_UI, configuration.GetValue(MasaStackConfigConstant.MASA_UI) }, + { MasaStackConfigConstant.ELASTIC, configuration.GetValue(MasaStackConfigConstant.ELASTIC) }, + { MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue(MasaStackConfigConstant.ENVIRONMENT) }, + { MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue(MasaStackConfigConstant.ADMIN_PWD) }, + { MasaStackConfigConstant.DCC_SECRET, configuration.GetValue(MasaStackConfigConstant.DCC_SECRET) } }; dccClient.Setup(aa => aa.GetAsync( It.IsAny(), @@ -26,7 +41,7 @@ public RequestMiddlewareTest() builder.Services.AddSingleton(serviceProvider => { - return new MasaStackConfig(dccClient.Object); + return new MasaStackConfig(dccClient.Object, configs); }); builder.Services.AddStackMiddleware(); diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/appsettings.json b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/appsettings.json index 6a5f769c0..6aac03654 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/appsettings.json +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/appsettings.json @@ -1,3 +1,17 @@ { - "IS_DEMO": "true" + "CLUSTER": "Default", + "CONNECTIONSTRING": "{\"Server\": \"masastack-sqlserver.masastack\", \"port\": 1433,\"Database\":\"\",\"UserId\": \"sa\",\"Password\":\"p@ssw0rd\"}", + "DOMAIN_NAME": "masastack.com", + "ELASTIC": "{\"ESNode\": \"masastack-es.masastack\",\"Index\": \"user_dev\",\"ESPort\":9200}", + "IS_DEMO": "true", + "MASA_SERVER": "{\"pm\":{\"server\":\"masa-pm-server\",\"ui\":\"masa-pm-ui\"},\"dcc\":{\"server\":\"masa-dcc-server\",\"ui\":\"masa-dcc-ui\"},\"auth\":{\"server\":\"masa-auth-server\",\"ui\":\"masa-auth-ui\",\"sso\":\"masa-auth-sso\"},\"mc\":{\"server\":\"masa-mc-server\",\"ui\":\"masa-mc-web\"},\"scheduler\":{\"server\":\"masa-scheduler-server\",\"ui\":\"masa-scheduler-ui\",\"worker\":\"masa-scheduler-worker\"},\"tsc\":{\"server\":\"masa-tsc-server\",\"ui\":\"masa-tsc-web\"},\"alert\":{\"server\":\"masa-alert-server\",\"ui\":\"masa-alert-web\"}}", + "MASA_UI": "{\"pm\":{\"server\":\"masa-pm-server\",\"ui\":\"masa-pm-ui-demo\"},\"dcc\":{\"server\":\"masa-dcc-server\",\"ui\":\"masa-dcc-ui-demo\"},\"auth\":{\"server\":\"masa-auth-server\",\"ui\":\"masa-auth-ui-demo\",\"sso\":\"masa-auth-sso-demo\"},\"mc\":{\"server\":\"masa-mc-server\",\"ui\":\"masa-mc-web-demo\"},\"scheduler\":{\"server\":\"masa-scheduler-server\",\"ui\":\"masa-scheduler-ui-demo\",\"worker\":\"masa-scheduler-worker\"},\"tsc\":{\"server\":\"masa-tsc-server\",\"ui\":\"masa-tsc-web-demo\"},\"alert\":{\"server\":\"masa-alert-server\",\"ui\":\"masa-alert-web-demo\"}}", + "OTLP_URL": "otel-collector.masastack:9013", + "REDIS": "{\"RedisHost\": \"localhost\", \"RedisPort\": 6379, \"RedisDb\": 0,\"RedisPassword\": \"\"}", + "TLS_NAME": "masastack", + "VERSION": "1.0-Preview1", + "ENVIRONMENT": "Development", + "NAMESPACE": "masastack", + "ADMIN_PWD": "admin_pwd", + "DCC_SECRET": "dcc_secret" }