From 2b5969ff649886a4626ba1389e1d13e8b7b0209b Mon Sep 17 00:00:00 2001 From: pengjuyan <42963214+codding-y@users.noreply.github.com> Date: Thu, 16 Feb 2023 15:37:32 +0800 Subject: [PATCH] feat: masa stack config support dcc (#456) * feat: add dcc storage * feat: update masa config * feat: masa stack config support dcc * feat: code review * feat: update appsettings * feat: test * feat: update InitializeMasaStackConfiguration * feat: update test * feat: update test * feat: update test * feat: update test * feat: code review * feat: code review --------- Co-authored-by: yanpengju --- .../IMasaStackConfig.cs | 4 +- .../Constants.cs | 11 +++ .../Masa.Contrib.StackSdks.Config.csproj | 1 + .../MasaStackConfig.cs | 20 +++-- .../MasaStackConfigExtensions.cs | 34 ++++---- .../MasaStackConfigOptions.cs | 18 ++-- .../ServiceCollectionExtensions.cs | 86 +++++++++++++------ .../Masa.Contrib.StackSdks.Config/_Imports.cs | 4 +- .../MasaStackConfigTest.cs | 41 +++++++-- .../_Imports.cs | 4 +- .../appsettings.json | 2 +- .../EventMiddlewareTest.cs | 17 +++- ....Contrib.StackSdks.Middleware.Tests.csproj | 1 + .../RequestMiddlewareTest.cs | 21 ++++- .../_Imports.cs | 5 ++ 15 files changed, 204 insertions(+), 65 deletions(-) create mode 100644 src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Constants.cs diff --git a/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs b/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs index 08e9bebe9..7968e9208 100644 --- a/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs +++ b/src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Config/IMasaStackConfig.cs @@ -35,5 +35,7 @@ public interface IMasaStackConfig void SetValue(string key, string value); - public List ProjectList(); + void SetValues(Dictionary configMap); + + List GetProjectList(); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Constants.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Constants.cs new file mode 100644 index 000000000..78ba35156 --- /dev/null +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Constants.cs @@ -0,0 +1,11 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.StackSdks.Config; + +internal static class Constants +{ + internal const string DEFAULT_PUBLIC_ID = "public-$Config"; + + internal const string DEFAULT_CONFIG_NAME = "$public.DefaultConfig"; +} diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Masa.Contrib.StackSdks.Config.csproj b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Masa.Contrib.StackSdks.Config.csproj index 16303e8e0..1f13bba84 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Masa.Contrib.StackSdks.Config.csproj +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/Masa.Contrib.StackSdks.Config.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs index 2a650e90c..606c0ffde 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfig.cs @@ -5,11 +5,15 @@ namespace Masa.Contrib.StackSdks.Config; public class MasaStackConfig : IMasaStackConfig { - private readonly IOptions _options; - - public MasaStackConfig(IOptions options) + public MasaStackConfig(IConfigurationApiClient client) { - _options = options; + var configs = client.GetAsync>( + Environment, + Cluster, + DEFAULT_PUBLIC_ID, + DEFAULT_CONFIG_NAME).ConfigureAwait(false).GetAwaiter().GetResult(); + + MasaStackConfigOptions.SetValues(configs); } public RedisModel RedisModel @@ -52,9 +56,11 @@ public ElasticModel ElasticModel public bool SingleSsoClient { get; } - public List ProjectList() => this.GetAllServer().Keys.ToList(); + public List GetProjectList() => this.GetAllServer().Keys.ToList(); + + public string GetValue(string key) => MasaStackConfigOptions.GetValue(key); - public string GetValue(string key) => _options.Value.GetValue(key); + public void SetValue(string key, string value) => MasaStackConfigOptions.SetValue(key, value); - public void SetValue(string key, string value) => _options.Value.SetValue(key, value); + public void SetValues(Dictionary configMap) => MasaStackConfigOptions.SetValues(configMap); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs index 693f456ee..4efac59f1 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigExtensions.cs @@ -151,26 +151,28 @@ public static string GetWebId(this IMasaStackConfig masaStackConfig, string proj return obj?[service]?.ToString() ?? ""; } - public static T GetDccMiniOptions(this IMasaStackConfig masaStackConfig) + public static DccOptions GetDefaultDccOptions(this IMasaStackConfig masaStackConfig) { var dccServerAddress = GetDccServiceDomain(masaStackConfig); var redis = masaStackConfig.RedisModel ?? throw new Exception("redis options can not null"); - var stringBuilder = new System.Text.StringBuilder(@"{""ManageServiceAddress"":"); - stringBuilder.Append($"\"{dccServerAddress}\","); - stringBuilder.Append(@"""RedisOptions"": {""Servers"": [{""Host"": "); - stringBuilder.Append($"\"{redis.RedisHost}\","); - stringBuilder.Append(@$"""Port"":{redis.RedisPort}"); - stringBuilder.Append("}],"); - stringBuilder.Append(@$"""DefaultDatabase"":{redis.RedisDb},"); - stringBuilder.Append(@$"""Password"": ""{redis.RedisPassword}"""); - stringBuilder.Append(@"},"); - stringBuilder.Append(@"""ConfigObjectSecret"":"); - stringBuilder.Append($"\"{masaStackConfig.DccSecret}\","); - stringBuilder.Append(@"""PublicSecret"":"); - stringBuilder.Append($"\"{masaStackConfig.DccSecret}\""); - stringBuilder.Append(@"}"); - return JsonSerializer.Deserialize(stringBuilder.ToString()) ?? throw new JsonException(); + 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 + }, + Secret = masaStackConfig.DccSecret, + PublicSecret = masaStackConfig.DccSecret + }; + + return options; } public static Guid GetDefaultUserId(this IMasaStackConfig masaStackConfig) diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs index d796a3ae7..2da6c460f 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/MasaStackConfigOptions.cs @@ -3,17 +3,25 @@ namespace Masa.Contrib.StackSdks.Config; -public class MasaStackConfigOptions +public static class MasaStackConfigOptions { - private ConcurrentDictionary ConfigMap { get; set; } = new(StringComparer.OrdinalIgnoreCase); + private static ConcurrentDictionary ConfigMap { get; set; } = new(StringComparer.OrdinalIgnoreCase); - public string GetValue(string key) => GetValue(key, () => string.Empty); + public static string GetValue(string key) => GetValue(key, () => string.Empty); - public string GetValue(string key, Func defaultFunc) + public static string GetValue(string key, Func defaultFunc) { if (ConfigMap.ContainsKey(key)) return ConfigMap[key]; return defaultFunc.Invoke(); } - public void SetValue(string key, string value) => ConfigMap[key] = value; + public static void SetValue(string key, string value) => ConfigMap[key] = value; + + public static void SetValues(Dictionary configMap) + { + foreach (var config in configMap) + { + SetValue(config.Key, config.Value); + } + } } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs index 97f253e31..efe8cb67e 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/ServiceCollectionExtensions.cs @@ -5,38 +5,76 @@ namespace Microsoft.Extensions.DependencyInjection; public static class ServiceCollectionExtensions { - private static void InitializeMasaStackConfiguration(this IServiceCollection services) + private static async Task InitializeMasaStackConfiguration(this IServiceCollection services) { - services.Configure(masaStackConfig => + var serviceProvider = services.BuildServiceProvider(); + var configuration = serviceProvider.GetRequiredService(); + var configurationApiManage = serviceProvider.GetRequiredService(); + var configurationApiClient = serviceProvider.GetRequiredService(); + + var configs = new Dictionary() { - var serviceProvider = services.BuildServiceProvider(); - var configuration = serviceProvider.GetRequiredService(); - - masaStackConfig.SetValue(MasaStackConfigConstant.VERSION, configuration.GetValue(MasaStackConfigConstant.VERSION)); - masaStackConfig.SetValue(MasaStackConfigConstant.IS_DEMO, configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString()); - masaStackConfig.SetValue(MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue(MasaStackConfigConstant.DOMAIN_NAME)); - masaStackConfig.SetValue(MasaStackConfigConstant.NAMESPACE, configuration.GetValue(MasaStackConfigConstant.NAMESPACE)); - masaStackConfig.SetValue(MasaStackConfigConstant.TLS_NAME, configuration.GetValue(MasaStackConfigConstant.TLS_NAME)); - masaStackConfig.SetValue(MasaStackConfigConstant.CLUSTER, configuration.GetValue(MasaStackConfigConstant.CLUSTER)); - masaStackConfig.SetValue(MasaStackConfigConstant.OTLP_URL, configuration.GetValue(MasaStackConfigConstant.OTLP_URL)); - masaStackConfig.SetValue(MasaStackConfigConstant.REDIS, configuration.GetValue(MasaStackConfigConstant.REDIS)); - masaStackConfig.SetValue(MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue(MasaStackConfigConstant.CONNECTIONSTRING)); - masaStackConfig.SetValue(MasaStackConfigConstant.MASA_SERVER, configuration.GetValue(MasaStackConfigConstant.MASA_SERVER)); - masaStackConfig.SetValue(MasaStackConfigConstant.MASA_UI, configuration.GetValue(MasaStackConfigConstant.MASA_UI)); - masaStackConfig.SetValue(MasaStackConfigConstant.ELASTIC, configuration.GetValue(MasaStackConfigConstant.ELASTIC)); - masaStackConfig.SetValue(MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue(MasaStackConfigConstant.ENVIRONMENT)); - masaStackConfig.SetValue(MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue(MasaStackConfigConstant.ADMIN_PWD)); - masaStackConfig.SetValue(MasaStackConfigConstant.DCC_SECRET, configuration.GetValue(MasaStackConfigConstant.DCC_SECRET)); - }); + { 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) } + }; + + try + { + var remoteConfigs = await configurationApiClient.GetAsync>( + configs[MasaStackConfigConstant.ENVIRONMENT], + configs[MasaStackConfigConstant.CLUSTER], + DEFAULT_PUBLIC_ID, + DEFAULT_CONFIG_NAME); + + if (remoteConfigs != null) + { + await configurationApiManage.UpdateAsync( + configs[MasaStackConfigConstant.ENVIRONMENT], + configs[MasaStackConfigConstant.CLUSTER], + DEFAULT_PUBLIC_ID, + DEFAULT_CONFIG_NAME, + configs); + } + } + catch (ArgumentException) + { + await configurationApiManage.AddAsync( + configs[MasaStackConfigConstant.ENVIRONMENT], + configs[MasaStackConfigConstant.CLUSTER], + DEFAULT_PUBLIC_ID, + new Dictionary + { + { DEFAULT_CONFIG_NAME, JsonSerializer.Serialize(configs) } + }); + } } - public static IServiceCollection AddMasaStackConfig(this IServiceCollection services, bool init = true) + public static IServiceCollection AddMasaStackConfig(this IServiceCollection services, bool init = false) { - services.TryAddSingleton(); if (init) { - InitializeMasaStackConfiguration(services); + InitializeMasaStackConfiguration(services).ConfigureAwait(false); } + + services.TryAddScoped(serviceProvider => + { + var client = serviceProvider.GetRequiredService(); + return new MasaStackConfig(client); + }); + return services; } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/_Imports.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/_Imports.cs index c2d898934..106652327 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/_Imports.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Config/_Imports.cs @@ -1,15 +1,17 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +global using Masa.BuildingBlocks.Configuration; global using Masa.BuildingBlocks.StackSdks.Config; global using Masa.BuildingBlocks.StackSdks.Config.Models; +global using Masa.Contrib.Configuration.ConfigurationApi.Dcc.Options; global using Masa.Contrib.StackSdks.Config; global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection.Extensions; -global using Microsoft.Extensions.Options; global using System.Collections.Concurrent; global using System.Globalization; global using System.Security.Cryptography; global using System.Text; global using System.Text.Json; global using System.Text.Json.Nodes; +global using static Masa.Contrib.StackSdks.Config.Constants; 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 9bdfe3ad8..01aae2327 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 @@ -1,20 +1,51 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +using Masa.BuildingBlocks.Configuration; +using Moq; + namespace Masa.Contrib.StackSdks.Config.Tests; [TestClass] public class MasaStackConfigTest { - private IMasaStackConfig _stackConfig; + private MasaStackConfig _stackConfig; [TestInitialize] public void Initialize() { var builder = WebApplication.CreateBuilder(); + var configuration = builder.Configuration.AddJsonFile("appsettings.json", true, true).Build(); + var configs = new Dictionary() + { + { 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) } + }; + + Mock dccClient = new(); + + dccClient.Setup(aa => aa.GetAsync( + It.IsAny(), + It.IsAny(), + It.IsAny(), It.IsAny(), + It.IsAny>>()!)) + .ReturnsAsync(configs); + + _stackConfig = new MasaStackConfig(dccClient.Object); - builder.Services.AddMasaStackConfig(); - _stackConfig = builder.Services.BuildServiceProvider().GetRequiredService(); } [TestMethod] @@ -26,9 +57,9 @@ public void TestGetAllServers() } [TestMethod] - public void TestGetMiniDccOptions() + public void TestGetDefaultDccOptions() { - var dccOptions = _stackConfig.GetDccMiniOptions(); + var dccOptions = _stackConfig.GetDefaultDccOptions(); Assert.IsNotNull(dccOptions?.RedisOptions); } diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/_Imports.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/_Imports.cs index 0318d5525..a615fe77e 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/_Imports.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/_Imports.cs @@ -2,7 +2,7 @@ // Licensed under the MIT License. See LICENSE.txt in the project root for license information. global using Masa.BuildingBlocks.StackSdks.Config; -global using Masa.Contrib.Configuration.ConfigurationApi.Dcc.Options; global using Microsoft.AspNetCore.Builder; -global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Configuration; global using Microsoft.VisualStudio.TestTools.UnitTesting; + diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/appsettings.json b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/appsettings.json index 73afc1d25..6aac03654 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/appsettings.json +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Config.Tests/appsettings.json @@ -7,7 +7,7 @@ "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\": \"masastack-redis.masastack\", \"RedisPort\": 6379, \"RedisDb\": 0,\"RedisPassword\": \"p@ssw0rd\"}", + "REDIS": "{\"RedisHost\": \"localhost\", \"RedisPort\": 6379, \"RedisDb\": 0,\"RedisPassword\": \"\"}", "TLS_NAME": "masastack", "VERSION": "1.0-Preview1", "ENVIRONMENT": "Development", 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 7e2a6bfb9..2c206d0e0 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 @@ -18,7 +18,22 @@ public EventMiddlewareTest() options.Mapping(nameof(MasaUser.Account), "ACCOUNT"); }); - builder.Services.AddMasaStackConfig(); + Mock dccClient = new(); + var configs = new Dictionary() + { + { MasaStackConfigConstant.IS_DEMO, builder.Configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString() } + }; + dccClient.Setup(aa => aa.GetAsync( + It.IsAny(), + It.IsAny(), + It.IsAny(), It.IsAny(), + It.IsAny>>()!)) + .ReturnsAsync(configs); + + builder.Services.AddSingleton(serviceProvider => + { + return new MasaStackConfig(dccClient.Object); + }); 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/Masa.Contrib.StackSdks.Middleware.Tests.csproj b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/Masa.Contrib.StackSdks.Middleware.Tests.csproj index 64c0b6118..0b59b248d 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/Masa.Contrib.StackSdks.Middleware.Tests.csproj +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/Masa.Contrib.StackSdks.Middleware.Tests.csproj @@ -14,6 +14,7 @@ + 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 03ea9c6df..ea2a6caf8 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 @@ -11,8 +11,25 @@ public class RequestMiddlewareTest public RequestMiddlewareTest() { var builder = WebApplication.CreateBuilder(); - builder.Services.AddMasaStackConfig() - .AddStackMiddleware(); + + Mock dccClient = new(); + var configs = new Dictionary() + { + { MasaStackConfigConstant.IS_DEMO, builder.Configuration.GetValue(MasaStackConfigConstant.IS_DEMO).ToString() } + }; + dccClient.Setup(aa => aa.GetAsync( + It.IsAny(), + It.IsAny(), + It.IsAny(), It.IsAny(), + It.IsAny>>()!)) + .ReturnsAsync(configs); + + builder.Services.AddSingleton(serviceProvider => + { + return new MasaStackConfig(dccClient.Object); + }); + + builder.Services.AddStackMiddleware(); _serviceProvider = builder.Services.BuildServiceProvider(); } diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/_Imports.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/_Imports.cs index 50d0bea65..fb4fd023a 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/_Imports.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Middleware.Tests/_Imports.cs @@ -1,17 +1,22 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +global using Masa.BuildingBlocks.Configuration; global using Masa.BuildingBlocks.Dispatcher.Events; global using Masa.BuildingBlocks.ReadWriteSplitting.Cqrs.Commands; global using Masa.BuildingBlocks.ReadWriteSplitting.Cqrs.Queries; global using Masa.BuildingBlocks.StackSdks.Auth.Contracts; +global using Masa.BuildingBlocks.StackSdks.Config; global using Masa.BuildingBlocks.StackSdks.Middleware; global using Masa.Contrib.Authentication.Identity; global using Masa.Contrib.Dispatcher.Events; +global using Masa.Contrib.StackSdks.Config; global using Masa.Contrib.StackSdks.Middleware.Tests.Application; global using Microsoft.AspNetCore.Builder; global using Microsoft.AspNetCore.Http; +global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using Moq; global using System.Reflection; global using System.Security.Claims;