-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
197 lines (167 loc) · 6.14 KB
/
Helpers.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using DnsClient;
using DnsClient.Protocol;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Net.Http;
namespace cs_ijson_microservice
{
public delegate JProperty Callback(string action, JObject param);
public class Options
{
public Options() { }
public Options(string version, string env, string ijson, int requestTimeout)
{
this.version = version;
this.env = env;
this.ijson = ijson.EndsWith("/") ? ijson : (ijson + "/");
this.requestTimeout = TimeSpan.FromMilliseconds(requestTimeout);
}
public string version { get; set; } = "1.0.0";
public string env { get; set; } = "development";
public string ijson { get; set; } = "http://localhost:8001";
public TimeSpan requestTimeout { get; set; } = TimeSpan.FromMilliseconds(1000 * 15);
}
public class helpersExeption : Exception
{
public helpersExeption(string message) : base(message) { }
}
public class Helpers
{
public class MjRequest
{
public bool isError { get; set; } = false;
public JObject request { get; set; }
public string invalidJson { get; set; }
public string errorMessages { get; set; }
public MjRequest()
{
}
}
public static string ExpandSrv(string host)
{
if (!host.EndsWith(".srv"))
{
return host;
}
string[] hostSplits = host.Split("://");
string protocol = hostSplits.First();
string domain = hostSplits.Last();
IDnsQueryResponse query = new LookupClient().Query(domain.Replace(".srv", ""), QueryType.SRV);
if (!query.HasError)
{
SrvRecord record = query.Answers.SrvRecords()
.OrderBy(record => record.Priority)
.FirstOrDefault();
string target = record.Target.Original.EndsWith(".") ?
record.Target.Original[0..^1] :
record.Target.Original;
return string.Format("{0}://{1}:{2}/", protocol, target, record.Port);
}
throw new helpersExeption(query.ErrorMessage);
}
public static MjRequest HttpRequest(HttpResponseMessage httpResponseMessage)
{
MjRequest result = new MjRequest();
string content = httpResponseMessage.Content.ReadAsStringAsync().Result;
try
{
result.request = JObject.Parse(content);
}
catch (Exception e)
{
result.isError = true;
result.errorMessages = e.Message;
result.invalidJson = content.Replace("\n", "").Replace("\r", "");
}
return result;
}
public class MySqlConfig
{
public MySqlConfig(string host, string port, string database, string user, string password)
{
Host = host;
Port = port;
Database = database;
User = user;
Password = password;
}
public MySqlConfig(JObject jObject)
{
if (jObject.ContainsKey("MysqlCredentials"))
{
JObject mysqlCredentials = (JObject)jObject.SelectToken("MysqlCredentials[0]");
Host = (string)mysqlCredentials["host"];
Port = (string)mysqlCredentials["port"];
Database = (string)mysqlCredentials["database"];
User = (string)mysqlCredentials["user"];
Password = (string)mysqlCredentials["password"];
}
}
public string Host;
public string Port;
public string Database;
public string User;
public string Password;
public string getStringConnection => string.Format("Server={0};Port={1};UserId={2};Password={3};Database={4};", Host, Port, User, Password, Database);
}
public class MicroserviceConfig
{
public MicroserviceConfig() { }
public MicroserviceConfig(MySqlConfig defaultMySql, string authAlias)
{
mysql = defaultMySql;
this.authAlias = authAlias;
}
public void addObject(JObject jObject)
{
if (jObject.ContainsKey("result"))
{
JObject result = (JObject)jObject.SelectToken("result.model");
mysql = new MySqlConfig(result);
JObject services = (JObject)result["Services"][0];
if (services.ContainsKey("alias"))
{
hasAuthorization = ((string)services["alias"] == authAlias) || hasAuthorization;
}
}
}
public MySqlConfig mysql;
public bool hasAuthorization;
private readonly string authAlias;
}
}
public class LogsDriver
{
public class TYPE
{
public const string Response = " <-- Response";
public const string Request = " --> Request";
public const string Error = " ERROR";
}
private readonly string serviceName;
public LogsDriver(string serviceName)
{
this.serviceName = serviceName;
}
public void Write(string type, JObject jObject)
{
string id = "0";
if (jObject.ContainsKey("id"))
{
id = (string)jObject["id"];
}
Console.WriteLine("{0} ({1}): {2}", type, id, JsonConvert.SerializeObject(jObject));
}
public void Write(string type, string errorMessages)
{
if (type == TYPE.Error)
{
Console.ForegroundColor = ConsoleColor.Red;
}
Console.WriteLine("{0}: {1}", type, errorMessages);
Console.ResetColor();
}
}
}