-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
138 lines (115 loc) · 4.14 KB
/
Program.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ElencyConfig;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Client
{
class Program
{
const string AppId = "centre";
const string Environment = "Prod";
const string HMACAuthorizationKey = "YWJlZjYwNzQwYzk4NDY4Zjg3ZTg5MWU0";
const string ConfigEncryptionKey = "M2Y0M2E0NTMyZDBjNDNjNDk5YWJjOGEy";
static ElencyConfigClient<TypedConfiguration> Client;
static void Main(string[] args)
{
try
{
Task.Run(async () =>
{
await GetConfiguration();
})
.GetAwaiter()
.GetResult();
while (true)
{
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void WaitForServerToBecomeAvailable()
{
while (!File.Exists("status/server-up.txt"))
Thread.Sleep(200);
}
private static async Task GetConfiguration()
{
WaitForServerToBecomeAvailable();
var configuration = new ElencyConfiguration
{
Uri = "http://app:3000",
AppId = AppId,
AppVersion = "2.0.0",
Environment = Environment,
RefreshInterval = 1000,
HMACAuthorizationKey = HMACAuthorizationKey,
ConfigEncryptionKey = ConfigEncryptionKey,
Retrieved = Retrieved,
RefreshFailure = RefreshFailure,
};
Client = new ElencyConfigClient<TypedConfiguration>();
await Client.Init(configuration);
}
private static void Retrieved()
{
Console.WriteLine();
Console.WriteLine("__________________________________________");
Console.WriteLine($"Configuration retrieved: {DateTime.Now.ToString()}");
Console.WriteLine($"Version: {Client.AppVersion}");
Console.WriteLine($"Environment: {Client.Environment}");
Console.WriteLine($"Configuration Id: {Client.ConfigurationId}");
Console.WriteLine("Keys and values:");
Console.WriteLine();
Client.GetAllKeys().ForEach(key =>
{
Console.WriteLine("{0}: {1}", key, Client.Get(key));
});
Console.WriteLine();
Console.WriteLine("Typed configuration:");
Console.WriteLine();
JsonSerializerSettings settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new LongNameContractResolver()
};
Console.WriteLine(JsonConvert.SerializeObject(Client.Configuration, settings));
Console.WriteLine("__________________________________________");
}
private static void RefreshFailure(Exception exception)
{
Console.WriteLine(exception.Message);
Console.WriteLine("** REFRESH FAILURE **");
}
}
public class TypedConfiguration
{
[JsonProperty("REST_API_URL")]
public string RestApiUrl { get; set; }
[JsonProperty("ENVIRONMENT")]
public string Environment { get; set; }
[JsonProperty("LOG_LEVEL")]
public string LogLevel { get; set; }
[JsonProperty("SECURE_DATA")]
public string SecureData { get; set; }
}
class LongNameContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
foreach (JsonProperty prop in list)
{
prop.PropertyName = prop.UnderlyingName;
}
return list;
}
}
}