-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (96 loc) · 4.37 KB
/
Copy pathProgram.cs
File metadata and controls
108 lines (96 loc) · 4.37 KB
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
using System;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace TigerGraph.Proxy
{
public class Program
{
#region Entry-point
public static void Main(string[] args)
{
/* Configure the Serilog Logger */
var config = new LoggerConfiguration();
Log.Logger = config.MinimumLevel.Information().Enrich.FromLogContext().WriteTo.Console().CreateLogger();
/* Check for required environment vars */
if (string.IsNullOrEmpty(TG_TOKEN))
{
Log.Error("The environment variable {0} could not be read.", "TG_TOKEN");
return;
}
else if (string.IsNullOrEmpty(TG_REST_SERVER_URL))
{
Log.Error("The environment variable {0} could not be read.", "TG_REST_SERVER_URL");
return;
}
else if (string.IsNullOrEmpty(TG_GSQL_SERVER_URL))
{
Log.Error("The environment variable {0} could not be read.", "TG_GSQL_SERVER_URL");
return;
}
else if (!Uri.TryCreate(TG_REST_SERVER_URL, UriKind.Absolute, out Uri rsu))
{
Log.Error("The environment variable {0}:{1} is not a valid URI.", "TG_REST_SERVER_URL", TG_REST_SERVER_URL);
return;
}
else if (!Uri.TryCreate(TG_GSQL_SERVER_URL, UriKind.Absolute, out Uri gu))
{
Log.Error("The environment variable {0}:{1} is not a valid URI.", "TG_GSQL_SERVER_URL", TG_GSQL_SERVER_URL);
return;
}
else
{
/* Setup authentication to the TG servers */
RestServerWebClient = new WebClient();
RestServerWebClient.Headers.Add("Authorization", "Bearer " + TG_TOKEN);
GsqlServerWebClient = new WebClient();
string credentials = Convert.ToBase64String(
Encoding.ASCII.GetBytes(TG_USER + ":" + TG_PASS));
GsqlServerWebClient.Headers[HttpRequestHeader.Authorization] = string.Format(
"Basic {0}", credentials);
Log.Information("Authentication to REST++ server at {0} and GSQL server at {1} initialized.", TG_REST_SERVER_URL, TG_GSQL_SERVER_URL);
/* Setup the timer for pinging the server.*/
var pingTimer = new System.Timers.Timer(1000 * 60 * 15);
pingTimer.Elapsed += (sender, e) => {
var now = DateTime.Now;
try
{
RestServerWebClient.DownloadString(rsu.ToString() + "echo");
Log.Information("Pinged {0} at {1}.", rsu, now);
}
catch (Exception ex)
{
Log.Error(ex, "Error attempting to ping {0} at {1}.", rsu, now);
}
};
pingTimer.Enabled = true;
CreateHostBuilder(args).Build().Run();
/* After the webhost shuts down dispose of any unmanaged resources. */
RestServerWebClient.Dispose();
GsqlServerWebClient.Dispose();
pingTimer.Dispose();
}
}
#endregion
#region Properties
public static string TG_USER = Environment.GetEnvironmentVariable("TG_USER");
public static string TG_PASS = Environment.GetEnvironmentVariable("TG_PASS");
public static string TG_TOKEN = Environment.GetEnvironmentVariable("TG_TOKEN");
public static string TG_REST_SERVER_URL = Environment.GetEnvironmentVariable("TG_REST_SERVER_URL");
public static string TG_GSQL_SERVER_URL = Environment.GetEnvironmentVariable("TG_GSQL_SERVER_URL");
public static WebClient RestServerWebClient = new WebClient();
public static WebClient GsqlServerWebClient = new WebClient();
#endregion
#region Methods
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseSerilog();
webBuilder.UseStartup<Startup>();
});
#endregion
}
}