Skip to content

Commit fe16c86

Browse files
committed
Merge branch 'main' of https://github.com/masastack/MASA.Framework into fix/issues-550
� Conflicts: � src/Contrib/Development/Masa.Contrib.Development.DaprStarter/DaprProcessBase.cs
2 parents 063e536 + b4c6560 commit fe16c86

File tree

8 files changed

+198
-7
lines changed

8 files changed

+198
-7
lines changed

src/BuildingBlocks/Development/Masa.BuildingBlocks.Development.DaprStarter/DaprStarterConstant.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ public static class DaprStarterConstant
2828
/// <summary>
2929
/// When sidecar is started, the appid of the current dapr will be set to this environment variable
3030
/// </summary>
31-
public const string DEFAULT_DAPR_APPID = "dapr-appid";
31+
public const string DEFAULT_DAPR_APPID = "DAPR_APPID";
3232
}

src/BuildingBlocks/Development/Masa.BuildingBlocks.Development.DaprStarter/Options/DaprOptions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ public override int HeartBeatInterval
235235
/// </summary>
236236
public string ExtendedParameter { get; set; }
237237

238+
/// <summary>
239+
/// Whether to use the default placement host address when no PlacementHostAddress is specified
240+
/// default: true
241+
/// </summary>
242+
public bool EnableDefaultPlacementHostAddress { get; set; } = true;
243+
238244
public bool IsIncompleteAppId()
239245
{
240246
return !DisableAppIdSuffix && (AppIdSuffix == null || AppIdSuffix.Trim() != string.Empty);

src/BuildingBlocks/Development/Masa.BuildingBlocks.Development.DaprStarter/Options/DaprOptionsBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ushort? AppPort
3030
/// The protocol (gRPC or HTTP) Dapr uses to talk to the application. Valid values are: http or grpc
3131
/// default: HTTP
3232
/// </summary>
33-
public Protocol? AppProtocol { get; protected set; } = Protocol.Http;
33+
public Protocol? AppProtocol { get; set; } = Protocol.Http;
3434

3535
/// <summary>
3636
/// Enable https when Dapr invokes the application

src/Contrib/Development/Masa.Contrib.Development.DaprStarter/DaprProcessBase.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ public abstract class DaprProcessBase
1010

1111
private readonly IDaprProvider _daprProvider;
1212

13-
private static readonly string[] HttpPortPatterns = { "http server is running on port ([0-9]+)" };
14-
private static readonly string[] GrpcPortPatterns = { "API gRPC server is running on port ([0-9]+)" };
13+
private static readonly string[] HttpPortPatterns =
14+
{
15+
"http server is running on port ([0-9]+)"
16+
};
17+
18+
private static readonly string[] GrpcPortPatterns =
19+
{
20+
"API gRPC server is running on port ([0-9]+)"
21+
};
22+
1523
private static readonly string UserFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
1624

1725
internal SidecarOptions? SuccessDaprOptions;
@@ -69,15 +77,16 @@ internal SidecarOptions ConvertToSidecarOptions(DaprOptions options)
6977
Mode = options.Mode,
7078
RootPath = options.RootPath,
7179
DaprRootPath = options.DaprRootPath,
72-
ExtendedParameter = options.ExtendedParameter
80+
ExtendedParameter = options.ExtendedParameter,
81+
EnableDefaultPlacementHostAddress = options.EnableDefaultPlacementHostAddress
7382
};
7483
sidecarOptions.TrySetHttpPort(options.DaprHttpPort ?? DaprEnvironmentProvider.GetHttpPort());
7584
sidecarOptions.TrySetGrpcPort(options.DaprGrpcPort ?? DaprEnvironmentProvider.GetGrpcPort());
7685
sidecarOptions.TrySetMetricsPort(options.MetricsPort ?? DaprEnvironmentProvider.GetMetricsPort());
7786

7887
if (sidecarOptions.EnableDefaultPlacementHostAddress && sidecarOptions.PlacementHostAddress.IsNullOrWhiteSpace())
7988
{
80-
var port = Environment.OSVersion.Platform == PlatformID.Win32NT ? 6050 : 50005;
89+
var port = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 6050 : 50005;
8190
sidecarOptions.PlacementHostAddress = $"127.0.0.1:{port}";
8291
}
8392

src/Contrib/Development/Masa.Contrib.Development.DaprStarter/_Imports.cs

-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@
1919
global using System.Runtime.InteropServices;
2020
global using System.Text.Json.Serialization;
2121
global using System.Text.RegularExpressions;
22-
global using LogLevel = Masa.BuildingBlocks.Development.DaprStarter.LogLevel;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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.Contrib.Development.DaprStarter.Tests;
5+
6+
[TestClass]
7+
public class DaprProcessTest
8+
{
9+
[DataRow(true, "", "AppId", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
10+
[DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
11+
[DataRow(true, "", "AppId", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
12+
[DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
13+
[DataRow(true, "", "AppId", true, "", "", "", "", "")]
14+
[DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "", "", "", "", "")]
15+
[DataRow(true, "", "AppId", false, "", "", "", "", "")]
16+
[DataRow(true, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "", "", "", "", "")]
17+
[DataRow(false, "", "AppId-AppIdSuffix", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
18+
[DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
19+
[DataRow(false, "", "AppId-AppIdSuffix", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
20+
[DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "inputPlacementHostAddress", "inputRootPath", "inputDaprRootPath", "inputComponentPath", "inputConfig")]
21+
[DataRow(false, "", "AppId-AppIdSuffix", true, "", "", "", "", "")]
22+
[DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", true, "", "", "", "", "")]
23+
[DataRow(false, "", "AppId-AppIdSuffix", false, "", "", "", "", "")]
24+
[DataRow(false, "AppId-ByEnvironment", "AppId-ByEnvironment", false, "", "", "", "", "")]
25+
[DataTestMethod]
26+
public void TestConvertToSidecarOptions(
27+
bool disableAppIdSuffix,
28+
string inputDaprAppIdByEnvironment,
29+
string expectedDaprAppId,
30+
bool inputEnableDefaultPlacementHostAddress,
31+
string inputPlacementHostAddress,
32+
string inputRootPath,
33+
string inputDaprRootPath,
34+
string inputComponentPath,
35+
string inputConfig)
36+
{
37+
Environment.SetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID, inputDaprAppIdByEnvironment);
38+
var daprOptions = new DaprOptions()
39+
{
40+
AppPort = 5000,
41+
AppIdSuffix = "AppIdSuffix",
42+
AppProtocol = Protocol.GRpc,
43+
EnableSsl = true,
44+
DaprGrpcPort = 5001,
45+
DaprHttpPort = 3501,
46+
EnableHeartBeat = false,
47+
HeartBeatInterval = 30,
48+
CreateNoWindow = true,
49+
MaxConcurrency = 1,
50+
Config = inputConfig,
51+
ComponentPath = inputComponentPath,
52+
RootPath = inputRootPath,
53+
DaprRootPath = inputDaprRootPath,
54+
EnableProfiling = true,
55+
LogLevel = LogLevel.Error,
56+
SentryAddress = "SentryAddress",
57+
MetricsPort = 9000,
58+
ProfilePort = 1,
59+
UnixDomainSocket = "UnixDomainSocket",
60+
AppId = "AppId",
61+
AppIdDelimiter = "-",
62+
DisableAppIdSuffix = disableAppIdSuffix,
63+
DaprHttpMaxRequestSize = 1,
64+
EnableDefaultPlacementHostAddress = inputEnableDefaultPlacementHostAddress,
65+
PlacementHostAddress = inputPlacementHostAddress,
66+
AllowedOrigins = "AllowedOrigins",
67+
ControlPlaneAddress = "ControlPlaneAddress",
68+
DaprHttpReadBufferSize = 1,
69+
DaprInternalGrpcPort = 1,
70+
EnableApiLogging = true,
71+
EnableMetrics = true,
72+
Mode = "Mode",
73+
ExtendedParameter = "ExtendedParameter"
74+
};
75+
Mock<IOptionsMonitor<DaprOptions>> optionsMonitor = new();
76+
77+
var customDaprProcess = new CustomDaprProcess(new DaprEnvironmentProvider(), new DefaultDaprProvider(), optionsMonitor.Object);
78+
var sidecarOptions = customDaprProcess.ConvertToSidecarOptions(daprOptions);
79+
Assert.AreEqual(expectedDaprAppId, sidecarOptions.AppId);
80+
Assert.AreEqual(daprOptions.AppPort, sidecarOptions.AppPort);
81+
Assert.AreEqual(daprOptions.AppProtocol, sidecarOptions.AppProtocol);
82+
Assert.AreEqual(daprOptions.EnableSsl, sidecarOptions.EnableSsl);
83+
Assert.AreEqual(daprOptions.DaprGrpcPort, sidecarOptions.DaprGrpcPort);
84+
Assert.AreEqual(daprOptions.DaprHttpPort, sidecarOptions.DaprHttpPort);
85+
Assert.AreEqual(daprOptions.EnableHeartBeat, sidecarOptions.EnableHeartBeat);
86+
Assert.AreEqual(daprOptions.HeartBeatInterval, sidecarOptions.HeartBeatInterval);
87+
Assert.AreEqual(daprOptions.CreateNoWindow, sidecarOptions.CreateNoWindow);
88+
Assert.AreEqual(daprOptions.MaxConcurrency, sidecarOptions.MaxConcurrency);
89+
Assert.AreEqual(GetConfig(inputConfig), sidecarOptions.Config);
90+
Assert.AreEqual(GetComponentPath(inputComponentPath), sidecarOptions.ComponentPath);
91+
Assert.AreEqual(GetRootPath(inputRootPath), sidecarOptions.RootPath);
92+
93+
Assert.AreEqual(GetDaprRootPath(inputDaprRootPath), sidecarOptions.DaprRootPath);
94+
Assert.AreEqual(daprOptions.EnableProfiling, sidecarOptions.EnableProfiling);
95+
Assert.AreEqual(daprOptions.LogLevel, sidecarOptions.LogLevel);
96+
Assert.AreEqual(daprOptions.SentryAddress, sidecarOptions.SentryAddress);
97+
Assert.AreEqual(daprOptions.MetricsPort, sidecarOptions.MetricsPort);
98+
Assert.AreEqual(daprOptions.ProfilePort, sidecarOptions.ProfilePort);
99+
Assert.AreEqual(daprOptions.UnixDomainSocket, sidecarOptions.UnixDomainSocket);
100+
Assert.AreEqual(daprOptions.EnableDefaultPlacementHostAddress, sidecarOptions.EnableDefaultPlacementHostAddress);
101+
102+
Assert.AreEqual(GetPlacementHostAddress(inputEnableDefaultPlacementHostAddress, inputPlacementHostAddress),
103+
sidecarOptions.PlacementHostAddress);
104+
Assert.AreEqual(daprOptions.AllowedOrigins, sidecarOptions.AllowedOrigins);
105+
Assert.AreEqual(daprOptions.ControlPlaneAddress, sidecarOptions.ControlPlaneAddress);
106+
Assert.AreEqual(daprOptions.DaprHttpMaxRequestSize, sidecarOptions.DaprHttpMaxRequestSize);
107+
Assert.AreEqual(daprOptions.DaprHttpReadBufferSize, sidecarOptions.DaprHttpReadBufferSize);
108+
Assert.AreEqual(daprOptions.DaprInternalGrpcPort, sidecarOptions.DaprInternalGrpcPort);
109+
Assert.AreEqual(daprOptions.EnableApiLogging, sidecarOptions.EnableApiLogging);
110+
Assert.AreEqual(daprOptions.EnableMetrics, sidecarOptions.EnableMetrics);
111+
Assert.AreEqual(daprOptions.Mode, sidecarOptions.Mode);
112+
Assert.AreEqual(daprOptions.ExtendedParameter, sidecarOptions.ExtendedParameter);
113+
114+
Environment.SetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID, "");
115+
116+
string GetPlacementHostAddress(bool enableDefaultPlacementHostAddress, string placementHostAddress)
117+
{
118+
if (enableDefaultPlacementHostAddress && placementHostAddress.IsNullOrWhiteSpace())
119+
{
120+
var port = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 6050 : 50005;
121+
return $"127.0.0.1:{port}";
122+
}
123+
return placementHostAddress;
124+
}
125+
126+
string GetRootPath(string rootPath)
127+
{
128+
if (rootPath.IsNullOrWhiteSpace())
129+
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".dapr");
130+
131+
return rootPath;
132+
}
133+
134+
string GetDaprRootPath(string daprRootPath)
135+
{
136+
if (daprRootPath.IsNullOrWhiteSpace())
137+
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\dapr" : "/usr/local/bin";
138+
139+
return daprRootPath;
140+
}
141+
142+
string GetComponentPath(string componentPath)
143+
{
144+
if (!componentPath.IsNullOrWhiteSpace())
145+
return componentPath;
146+
147+
return "components";
148+
}
149+
150+
string GetConfig(string config)
151+
{
152+
if (!config.IsNullOrWhiteSpace())
153+
return config;
154+
155+
return "config.yaml";
156+
}
157+
}
158+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
// ReSharper disable once CheckNamespace
5+
6+
namespace Masa.Contrib.Development.DaprStarter.Tests;
7+
8+
public class CustomDaprProcess : DaprProcessBase
9+
{
10+
public CustomDaprProcess(
11+
IDaprEnvironmentProvider daprEnvironmentProvider,
12+
IDaprProvider daprProvider,
13+
IOptionsMonitor<DaprOptions> daprOptions)
14+
: base(daprEnvironmentProvider, daprProvider, daprOptions)
15+
{
16+
}
17+
}

src/Contrib/Development/Tests/Masa.Contrib.Development.DaprStarter.Tests/_Imports.cs

+2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
global using Microsoft.Extensions.DependencyInjection;
1010
global using Microsoft.Extensions.Options;
1111
global using Microsoft.VisualStudio.TestTools.UnitTesting;
12+
global using Moq;
1213
global using System.Net.NetworkInformation;
1314
global using System.Reflection;
15+
global using System.Runtime.InteropServices;

0 commit comments

Comments
 (0)