Skip to content

Commit c2b3128

Browse files
committed
feat(Configuration): Add Global Configuration
1 parent 95148ee commit c2b3128

11 files changed

+86
-35
lines changed

Masa.Framework.sln

+14-14
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution
2525
EndProject
2626
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{E747043D-81E2-4A89-8B5B-1258ED45F941}"
2727
EndProject
28-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.EventBus.IntegrationTests", "test\Masa.EventBus.IntegrationTests\Masa.EventBus.IntegrationTests.csproj", "{BDA1DB4F-C408-445D-9391-EC9765CF7349}"
29-
EndProject
3028
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Authentication", "Authentication", "{585B9627-45AA-42C3-965D-03359F25786C}"
3129
EndProject
3230
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OpenIdConnect", "OpenIdConnect", "{CFC8DE12-A06F-46E2-890A-6EC9400F17DA}"
@@ -529,9 +527,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.UoW.EFCor
529527
EndProject
530528
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.UoW.EFCore.Tests", "src\Contrib\Data\UoW\Tests\Masa.Contrib.Data.UoW.EFCore.Tests\Masa.Contrib.Data.UoW.EFCore.Tests.csproj", "{8B19C9E6-002B-4DD4-8D80-9D092C3EA6E9}"
531529
EndProject
532-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.IdGenerator.Snowflake.Tests.Perfs", "src\Contrib\Data\IdGenerator\Snowflake\Tests\Perfs\Tests.Perfs\Masa.Contrib.Data.IdGenerator.Snowflake.Tests.Perfs.csproj", "{B37640C5-3F96-4363-915C-0C2848004A05}"
530+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.IdGenerator.Snowflake.Tests.Perfs", "src\Contrib\Data\IdGenerator\Snowflake\Tests\Perfs\Tests.Perfs\Masa.Contrib.Data.IdGenerator.Snowflake.Tests.Perfs.csproj", "{B37640C5-3F96-4363-915C-0C2848004A05}"
531+
EndProject
532+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis.Tests.Perfs", "src\Contrib\Data\IdGenerator\Snowflake\Tests\Perfs\Distributed.Redis.Tests.Perfs\Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis.Tests.Perfs.csproj", "{9EA05A47-B28A-4CD1-8058-00A2098ED0C2}"
533533
EndProject
534-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis.Tests.Perfs", "src\Contrib\Data\IdGenerator\Snowflake\Tests\Perfs\Distributed.Redis.Tests.Perfs\Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis.Tests.Perfs.csproj", "{9EA05A47-B28A-4CD1-8058-00A2098ED0C2}"
534+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.EventBus.IntegrationTests", "test\Masa.EventBus.IntegrationTests\Masa.EventBus.IntegrationTests.csproj", "{A9EED08B-6C1E-4409-9874-AA7D34102519}"
535535
EndProject
536536
Global
537537
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -541,14 +541,6 @@ Global
541541
Release|x64 = Release|x64
542542
EndGlobalSection
543543
GlobalSection(ProjectConfigurationPlatforms) = postSolution
544-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
545-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Debug|Any CPU.Build.0 = Debug|Any CPU
546-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Debug|x64.ActiveCfg = Debug|Any CPU
547-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Debug|x64.Build.0 = Debug|Any CPU
548-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Release|Any CPU.ActiveCfg = Release|Any CPU
549-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Release|Any CPU.Build.0 = Release|Any CPU
550-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Release|x64.ActiveCfg = Release|Any CPU
551-
{BDA1DB4F-C408-445D-9391-EC9765CF7349}.Release|x64.Build.0 = Release|Any CPU
552544
{523EB315-1A01-4F13-9EDE-266FE8111AB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
553545
{523EB315-1A01-4F13-9EDE-266FE8111AB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
554546
{523EB315-1A01-4F13-9EDE-266FE8111AB6}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -1885,6 +1877,14 @@ Global
18851877
{9EA05A47-B28A-4CD1-8058-00A2098ED0C2}.Release|Any CPU.Build.0 = Release|Any CPU
18861878
{9EA05A47-B28A-4CD1-8058-00A2098ED0C2}.Release|x64.ActiveCfg = Release|Any CPU
18871879
{9EA05A47-B28A-4CD1-8058-00A2098ED0C2}.Release|x64.Build.0 = Release|Any CPU
1880+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1881+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Debug|Any CPU.Build.0 = Debug|Any CPU
1882+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Debug|x64.ActiveCfg = Debug|Any CPU
1883+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Debug|x64.Build.0 = Debug|Any CPU
1884+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Release|Any CPU.ActiveCfg = Release|Any CPU
1885+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Release|Any CPU.Build.0 = Release|Any CPU
1886+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Release|x64.ActiveCfg = Release|Any CPU
1887+
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Release|x64.Build.0 = Release|Any CPU
18881888
EndGlobalSection
18891889
GlobalSection(SolutionProperties) = preSolution
18901890
HideSolutionNode = FALSE
@@ -1893,7 +1893,6 @@ Global
18931893
{DC578D74-98F0-4F19-A230-CFA8DAEE0AF1} = {B9256C4D-86F4-4E67-8774-C3EF971EC811}
18941894
{5944A182-13B8-4DA6-AEE2-0A01E64A9648} = {B9256C4D-86F4-4E67-8774-C3EF971EC811}
18951895
{950DA7D0-48C1-42BA-8E8F-F72C0DCE41C4} = {B9256C4D-86F4-4E67-8774-C3EF971EC811}
1896-
{BDA1DB4F-C408-445D-9391-EC9765CF7349} = {E747043D-81E2-4A89-8B5B-1258ED45F941}
18971896
{585B9627-45AA-42C3-965D-03359F25786C} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
18981897
{CFC8DE12-A06F-46E2-890A-6EC9400F17DA} = {585B9627-45AA-42C3-965D-03359F25786C}
18991898
{87BBAEC2-1A18-4E5D-BDB3-91BF1CF3231B} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
@@ -2048,6 +2047,7 @@ Global
20482047
{11B1DF36-97DC-4928-BED2-A51BDDFE0AEF} = {E676BB35-7888-4FCD-9AFA-E46848639074}
20492048
{D77D8E17-6D1C-4E44-AF18-D572F12745BA} = {E676BB35-7888-4FCD-9AFA-E46848639074}
20502049
{3167F949-F6F4-4440-97CD-2EC7B785D9E8} = {FFEF958B-DA21-4552-86B6-87D9592A3668}
2050+
{A00D752B-65C8-404A-8397-82DB5613884E} = {FFEF958B-DA21-4552-86B6-87D9592A3668}
20512051
{17B751BF-DB79-46B0-A61E-BB8A52919AC9} = {FFEF958B-DA21-4552-86B6-87D9592A3668}
20522052
{0457B5A1-02D7-44FF-8FE6-468A456E2909} = {17B751BF-DB79-46B0-A61E-BB8A52919AC9}
20532053
{55DCA34D-48CE-4D32-8515-B6583FA548AE} = {17B751BF-DB79-46B0-A61E-BB8A52919AC9}
@@ -2144,9 +2144,9 @@ Global
21442144
{979FBAA7-EC54-4183-B2A7-3C5BE5510F16} = {83E5A183-56D6-4CAC-95A6-FB280AA7AE3D}
21452145
{44DB6ED1-71DC-487B-8DD7-3F872CB23254} = {E8B3FB42-C450-4B12-9C2C-7B9A02B616BB}
21462146
{8B19C9E6-002B-4DD4-8D80-9D092C3EA6E9} = {627DAA58-5882-4C65-B7A1-AE16AA91E1DE}
2147-
{A00D752B-65C8-404A-8397-82DB5613884E} = {FFEF958B-DA21-4552-86B6-87D9592A3668}
21482147
{B37640C5-3F96-4363-915C-0C2848004A05} = {55DCA34D-48CE-4D32-8515-B6583FA548AE}
21492148
{9EA05A47-B28A-4CD1-8058-00A2098ED0C2} = {55DCA34D-48CE-4D32-8515-B6583FA548AE}
2149+
{A9EED08B-6C1E-4409-9874-AA7D34102519} = {E747043D-81E2-4A89-8B5B-1258ED45F941}
21502150
EndGlobalSection
21512151
GlobalSection(ExtensibilityGlobals) = postSolution
21522152
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}

src/Contrib/Configuration/Masa.Contrib.Configuration/MasaConfigurationExtensions.cs src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/MasaConfigurationBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Masa.Contrib.Configuration;
55

6-
public static class MasaConfigurationExtensions
6+
public static class MasaConfigurationBuilderExtensions
77
{
88
public static void UseMasaOptions(this IMasaConfigurationBuilder builder, Action<MasaRelationOptions> options)
99
{

src/Contrib/Configuration/Masa.Contrib.Configuration/WebApplicationBuilderExtensions.cs src/Contrib/Configuration/Masa.Contrib.Configuration/Extensions/WebApplicationBuilderExtensions.cs

+34-7
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,41 @@ namespace Microsoft.AspNetCore.Builder;
66
public static class WebApplicationBuilderExtensions
77
{
88
public static WebApplicationBuilder InitializeAppConfiguration(this WebApplicationBuilder builder)
9+
=> builder.InitializeAppConfiguration(null);
10+
11+
public static WebApplicationBuilder InitializeAppConfiguration(
12+
this WebApplicationBuilder builder,
13+
Action<MasaAppConfigureOptionsRelation>? action)
914
{
10-
var configuration = builder.Configuration;
15+
if (builder.Services.Any(service => service.ImplementationType == typeof(InitializeAppConfigurationProvider)))
16+
return builder;
17+
18+
builder.Services.AddSingleton<InitializeAppConfigurationProvider>();
19+
20+
MasaAppConfigureOptionsRelation optionsRelation = new();
21+
action?.Invoke(optionsRelation);
22+
IConfiguration configuration = builder.Configuration;
23+
bool isInitialize = false;
1124
builder.Services.Configure<MasaAppConfigureOptions>(options =>
1225
{
26+
if (!isInitialize)
27+
{
28+
var masaConfiguration = builder.Services.BuildServiceProvider().GetService<IMasaConfiguration>();
29+
if (masaConfiguration != null) configuration = masaConfiguration.Local;
30+
isInitialize = true;
31+
}
32+
1333
if (string.IsNullOrWhiteSpace(options.AppId))
14-
options.AppId = configuration.GetConfigurationValue(nameof(MasaAppConfigureOptions.AppId),
15-
() => (Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName().Name!.Replace(".", "-"));
34+
options.AppId = configuration.GetConfigurationValue(optionsRelation.DataVariables[nameof(options.AppId)],
35+
() => optionsRelation.DataDefaultValue[nameof(options.AppId)]);
1636

1737
if (string.IsNullOrWhiteSpace(options.Environment))
18-
options.Environment = configuration.GetConfigurationValue("ASPNETCORE_ENVIRONMENT",
19-
() => "Production");
38+
options.Environment = configuration.GetConfigurationValue(optionsRelation.DataVariables[nameof(options.Environment)],
39+
() => optionsRelation.DataDefaultValue[nameof(options.Environment)]);
2040

2141
if (string.IsNullOrWhiteSpace(options.Cluster))
22-
options.Environment = configuration.GetConfigurationValue(nameof(MasaAppConfigureOptions.Cluster),
23-
() => "Default");
42+
options.Cluster = configuration.GetConfigurationValue(optionsRelation.DataVariables[nameof(options.Cluster)],
43+
() => optionsRelation.DataDefaultValue[nameof(options.Cluster)]);
2444
});
2545
return builder;
2646
}
@@ -58,6 +78,8 @@ public static WebApplicationBuilder AddMasaConfiguration(
5878
Action<IMasaConfigurationBuilder>? configureDelegate,
5979
Action<ConfigurationOptions>? action)
6080
{
81+
builder.InitializeAppConfiguration();
82+
6183
IConfigurationRoot masaConfiguration =
6284
builder.Services.CreateMasaConfiguration(
6385
configureDelegate,
@@ -77,4 +99,9 @@ public static WebApplicationBuilder AddMasaConfiguration(
7799

78100
public static IMasaConfiguration GetMasaConfiguration(this WebApplicationBuilder builder)
79101
=> builder.Services.BuildServiceProvider().GetRequiredService<IMasaConfiguration>();
102+
103+
private class InitializeAppConfigurationProvider
104+
{
105+
106+
}
80107
}

src/Contrib/Configuration/Masa.Contrib.Configuration/Internal/ConfigurationExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public static IConfigurationBuilder AddRange(this IConfigurationBuilder configur
2828
configurationBuilder.Add(configurationSource);
2929
return configurationBuilder;
3030
}
31+
32+
public static string GetConfigurationValue(this IConfiguration configuration, string key, Func<string> func)
33+
{
34+
var configurationValue = configuration[key];
35+
return string.IsNullOrWhiteSpace(configurationValue) ? func() : configurationValue;
36+
}
3137
}

src/Contrib/Configuration/Masa.Contrib.Configuration/Internal/ConfigurationManagerExtensions.cs

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Configuration.Options;
5+
6+
public class MasaAppConfigureOptionsRelation
7+
{
8+
public Dictionary<string, string> DataVariables { get; }
9+
10+
public Dictionary<string, string> DataDefaultValue { get; }
11+
12+
public MasaAppConfigureOptionsRelation()
13+
{
14+
DataVariables = new()
15+
{
16+
{ nameof(MasaAppConfigureOptions.AppId), nameof(MasaAppConfigureOptions.AppId) },
17+
{ nameof(MasaAppConfigureOptions.Environment), "ASPNETCORE_ENVIRONMENT" },
18+
{ nameof(MasaAppConfigureOptions.Cluster), nameof(MasaAppConfigureOptions.Cluster) },
19+
};
20+
DataDefaultValue = new Dictionary<string, string>()
21+
{
22+
{
23+
nameof(MasaAppConfigureOptions.AppId),
24+
(Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName().Name!.Replace(".", "-")
25+
},
26+
{ nameof(MasaAppConfigureOptions.Environment), "Production" },
27+
{ nameof(MasaAppConfigureOptions.Cluster), "Default" },
28+
};
29+
}
30+
}

test/Masa.EventBus.IntegrationTests/Masa.EventBus.IntegrationTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23+
<ProjectReference Include="..\..\src\Contrib\Configuration\Masa.Contrib.Configuration\Masa.Contrib.Configuration.csproj" />
2324
<ProjectReference Include="..\..\src\Contrib\Data\Contracts\Masa.Contrib.Data.Contracts.EFCore\Masa.Contrib.Data.Contracts.EFCore.csproj" />
2425
<ProjectReference Include="..\..\src\Contrib\Data\Orm\EFCore\Masa.Contrib.Data.EFCore.Sqlite\Masa.Contrib.Data.EFCore.Sqlite.csproj" />
2526
<ProjectReference Include="..\..\src\Contrib\Data\UoW\Masa.Contrib.Data.UoW.EFCore\Masa.Contrib.Data.UoW.EFCore.csproj" />

0 commit comments

Comments
 (0)