Skip to content

Commit

Permalink
feat(stacksdks.config): refactor stack config (#476)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Mayue <[email protected]>
  • Loading branch information
3 people authored Mar 2, 2023
1 parent e902310 commit d05865c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public interface IMasaStackConfig

string GetValue(string key);

void SetValue(string key, string value);

void SetValues(Dictionary<string, string> configMap);
Dictionary<string, string> GetValues();

List<string> GetProjectList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ namespace Masa.Contrib.StackSdks.Config;

public class MasaStackConfig : IMasaStackConfig
{
public MasaStackConfig(IConfigurationApiClient client)
{
var configs = client.GetAsync<Dictionary<string, string>>(
Environment,
Cluster,
DEFAULT_PUBLIC_ID,
DEFAULT_CONFIG_NAME).ConfigureAwait(false).GetAwaiter().GetResult();
private IConfigurationApiClient _configurationApiClient;

private static ConcurrentDictionary<string, string> ConfigMap { get; set; } = new(StringComparer.OrdinalIgnoreCase);

MasaStackConfigOptions.SetValues(configs);
public MasaStackConfig(IConfigurationApiClient client, Dictionary<string, string> configs)
{
_configurationApiClient = client;
ConfigMap = new(configs);
}

public RedisModel RedisModel
Expand Down Expand Up @@ -58,9 +57,27 @@ public ElasticModel ElasticModel

public List<string> 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<string, string> GetValues()
{
try
{
var remoteConfigs = _configurationApiClient.GetAsync<Dictionary<string, string>>(
ConfigMap[MasaStackConfigConstant.ENVIRONMENT],
ConfigMap[MasaStackConfigConstant.CLUSTER],
DEFAULT_PUBLIC_ID,
DEFAULT_CONFIG_NAME).ConfigureAwait(false).GetAwaiter().GetResult();

public void SetValues(Dictionary<string, string> configMap) => MasaStackConfigOptions.SetValues(configMap);
return remoteConfigs;
}
catch (ArgumentException)
{
return new(ConfigMap);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ public static class MasaStackConfigExtensions
{
public static Dictionary<string, JsonObject> GetAllServer(this IMasaStackConfig masaStackConfig)
{
var value = masaStackConfig.GetValue(MasaStackConfigConstant.MASA_SERVER);
if (string.IsNullOrEmpty(value))
{
return new();
}
return JsonSerializer.Deserialize<Dictionary<string, JsonObject>>(value) ?? new();
return MasaStackConfigUtils.GetAllServer(masaStackConfig.GetValues());
}

public static Dictionary<string, JsonObject> GetAllUI(this IMasaStackConfig masaStackConfig)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<Caching.Distributed.StackExchangeRedis.RedisServerOptions>
{
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static string GetValue(string key, Func<string> defaultFunc)
return defaultFunc.Invoke();
}

public static Dictionary<string, string> GetValues() => new(ConfigMap);

public static void SetValue(string key, string value) => ConfigMap[key] = value;

public static void SetValues(Dictionary<string, string> configMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public static class ServiceCollectionExtensions
{
private static async Task InitializeMasaStackConfiguration(
IServiceCollection services,
Dictionary<string, string> configs,
bool init)
Dictionary<string, string> configs)
{
var serviceProvider = services.BuildServiceProvider();
var configurationApiManage = serviceProvider.GetRequiredService<IConfigurationApiManage>();
Expand All @@ -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],
Expand All @@ -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<string, string>
{
{ DEFAULT_CONFIG_NAME, JsonSerializer.Serialize(configs) }
});
}
// remoteConfigs is null
await configurationApiManage.AddAsync(
configs[MasaStackConfigConstant.ENVIRONMENT],
configs[MasaStackConfigConstant.CLUSTER],
DEFAULT_PUBLIC_ID,
new Dictionary<string, string>
{
{ DEFAULT_CONFIG_NAME, JsonSerializer.Serialize(configs) }
});
}
}

Expand All @@ -54,12 +51,15 @@ public static async Task<IServiceCollection> 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<IMasaStackConfig>(serviceProvider =>
{
var client = serviceProvider.GetRequiredService<IConfigurationApiClient>();
return new MasaStackConfig(client);
var configurationApiClient = serviceProvider.GetRequiredService<IConfigurationApiClient>();
return new MasaStackConfig(configurationApiClient, configs);
});

return services;
Expand All @@ -68,14 +68,17 @@ public static async Task<IServiceCollection> AddMasaStackConfigAsync(this IServi
public static async Task<IServiceCollection> 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<IMasaStackConfig>(serviceProvider =>
{
var client = serviceProvider.GetRequiredService<IConfigurationApiClient>();
return new MasaStackConfig(client);
return new MasaStackConfig(client, configs);
});

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,30 @@ public void Initialize()

Mock<IConfigurationApiClient> dccClient = new();

dccClient.Setup(aa => aa.GetAsync(
dccClient.Setup(client => client.GetAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<Action<Dictionary<string, string>>>()!))
.ReturnsAsync(_config);

_stackConfig = new MasaStackConfig(dccClient.Object);
_stackConfig = new MasaStackConfig(dccClient.Object, _config);

}

[TestMethod]
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,24 @@ public EventMiddlewareTest()
});

Mock<IConfigurationApiClient> dccClient = new();
var configuration = builder.Configuration;
var configs = new Dictionary<string, string>()
{
{ MasaStackConfigConstant.IS_DEMO, builder.Configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() }
{ MasaStackConfigConstant.VERSION, configuration.GetValue<string>(MasaStackConfigConstant.VERSION) },
{ MasaStackConfigConstant.IS_DEMO, configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() },
{ MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue<string>(MasaStackConfigConstant.DOMAIN_NAME) },
{ MasaStackConfigConstant.NAMESPACE, configuration.GetValue<string>(MasaStackConfigConstant.NAMESPACE) },
{ MasaStackConfigConstant.TLS_NAME, configuration.GetValue<string>(MasaStackConfigConstant.TLS_NAME) },
{ MasaStackConfigConstant.CLUSTER, configuration.GetValue<string>(MasaStackConfigConstant.CLUSTER) },
{ MasaStackConfigConstant.OTLP_URL, configuration.GetValue<string>(MasaStackConfigConstant.OTLP_URL) },
{ MasaStackConfigConstant.REDIS, configuration.GetValue<string>(MasaStackConfigConstant.REDIS) },
{ MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue<string>(MasaStackConfigConstant.CONNECTIONSTRING) },
{ MasaStackConfigConstant.MASA_SERVER, configuration.GetValue<string>(MasaStackConfigConstant.MASA_SERVER) },
{ MasaStackConfigConstant.MASA_UI, configuration.GetValue<string>(MasaStackConfigConstant.MASA_UI) },
{ MasaStackConfigConstant.ELASTIC, configuration.GetValue<string>(MasaStackConfigConstant.ELASTIC) },
{ MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue<string>(MasaStackConfigConstant.ENVIRONMENT) },
{ MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue<string>(MasaStackConfigConstant.ADMIN_PWD) },
{ MasaStackConfigConstant.DCC_SECRET, configuration.GetValue<string>(MasaStackConfigConstant.DCC_SECRET) }
};
dccClient.Setup(aa => aa.GetAsync(
It.IsAny<string>(),
Expand All @@ -32,7 +47,7 @@ public EventMiddlewareTest()

builder.Services.AddSingleton<IMasaStackConfig>(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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@ public RequestMiddlewareTest()
var builder = WebApplication.CreateBuilder();

Mock<IConfigurationApiClient> dccClient = new();
var configuration = builder.Configuration;
var configs = new Dictionary<string, string>()
{
{ MasaStackConfigConstant.IS_DEMO, builder.Configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() }
{ MasaStackConfigConstant.VERSION, configuration.GetValue<string>(MasaStackConfigConstant.VERSION) },
{ MasaStackConfigConstant.IS_DEMO, configuration.GetValue<bool>(MasaStackConfigConstant.IS_DEMO).ToString() },
{ MasaStackConfigConstant.DOMAIN_NAME, configuration.GetValue<string>(MasaStackConfigConstant.DOMAIN_NAME) },
{ MasaStackConfigConstant.NAMESPACE, configuration.GetValue<string>(MasaStackConfigConstant.NAMESPACE) },
{ MasaStackConfigConstant.TLS_NAME, configuration.GetValue<string>(MasaStackConfigConstant.TLS_NAME) },
{ MasaStackConfigConstant.CLUSTER, configuration.GetValue<string>(MasaStackConfigConstant.CLUSTER) },
{ MasaStackConfigConstant.OTLP_URL, configuration.GetValue<string>(MasaStackConfigConstant.OTLP_URL) },
{ MasaStackConfigConstant.REDIS, configuration.GetValue<string>(MasaStackConfigConstant.REDIS) },
{ MasaStackConfigConstant.CONNECTIONSTRING, configuration.GetValue<string>(MasaStackConfigConstant.CONNECTIONSTRING) },
{ MasaStackConfigConstant.MASA_SERVER, configuration.GetValue<string>(MasaStackConfigConstant.MASA_SERVER) },
{ MasaStackConfigConstant.MASA_UI, configuration.GetValue<string>(MasaStackConfigConstant.MASA_UI) },
{ MasaStackConfigConstant.ELASTIC, configuration.GetValue<string>(MasaStackConfigConstant.ELASTIC) },
{ MasaStackConfigConstant.ENVIRONMENT, configuration.GetValue<string>(MasaStackConfigConstant.ENVIRONMENT) },
{ MasaStackConfigConstant.ADMIN_PWD, configuration.GetValue<string>(MasaStackConfigConstant.ADMIN_PWD) },
{ MasaStackConfigConstant.DCC_SECRET, configuration.GetValue<string>(MasaStackConfigConstant.DCC_SECRET) }
};
dccClient.Setup(aa => aa.GetAsync(
It.IsAny<string>(),
Expand All @@ -26,7 +41,7 @@ public RequestMiddlewareTest()

builder.Services.AddSingleton<IMasaStackConfig>(serviceProvider =>
{
return new MasaStackConfig(dccClient.Object);
return new MasaStackConfig(dccClient.Object, configs);
});

builder.Services.AddStackMiddleware();
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit d05865c

Please sign in to comment.