-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathDaprEnvironmentProvider.cs
67 lines (50 loc) · 1.85 KB
/
DaprEnvironmentProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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.Development.DaprStarter;
public class DaprEnvironmentProvider : IDaprEnvironmentProvider
{
private const string GRPC_PORT = "DAPR_GRPC_PORT";
private const string HTTP_PORT = "DAPR_HTTP_PORT";
private const string METRICS_PORT = "DAPR_METRICS_PORT";
public string? GetDaprAppId() => Environment.GetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID);
public ushort? GetHttpPort() => GetEnvironmentVariable(HTTP_PORT);
public ushort? GetGrpcPort() => GetEnvironmentVariable(GRPC_PORT);
public ushort? GetMetricsPort() => GetEnvironmentVariable(METRICS_PORT);
private static ushort? GetEnvironmentVariable(string environment)
{
if (ushort.TryParse(Environment.GetEnvironmentVariable(environment), out ushort port))
return port;
return null;
}
public bool TrySetHttpPort(ushort? httpPort)
{
if (httpPort is > 0)
{
Environment.SetEnvironmentVariable(HTTP_PORT, httpPort.Value.ToString());
return true;
}
return false;
}
public bool TrySetGrpcPort(ushort? grpcPort)
{
if (grpcPort is > 0)
{
Environment.SetEnvironmentVariable(GRPC_PORT, grpcPort.Value.ToString());
return true;
}
return false;
}
public bool TrySetMetricsPort(ushort? metricsPort)
{
if (metricsPort is > 0)
{
Environment.SetEnvironmentVariable(METRICS_PORT, metricsPort.Value.ToString());
return true;
}
return false;
}
public void SetDaprAppId(string appId)
{
Environment.SetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID, appId);
}
}