Skip to content

Commit 9e920e5

Browse files
committed
Use separate clients in proxy. Add tg-proxy scripts.
1 parent ed528ef commit 9e920e5

File tree

6 files changed

+77
-22
lines changed

6 files changed

+77
-22
lines changed

build.cmd

+5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
@setlocal
33
set ERROR_CODE=0
44

5+
echo Building CLI...
56
cd src\TigerGraph.CLI\
67
dotnet build /p:Configuration=Debug %*
78

9+
echo Building proxy...
10+
cd ..\TigerGraph.CLI\
11+
dotnet build /p:Configuration=Debug %*
12+
813
:end
914
cd ..\..
1015
exit /B %ERROR_CODE%

src/TigerGraph.NET.sln

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{9A0B
1515
..\scripts\endpoints.cmd = ..\scripts\endpoints.cmd
1616
..\scripts\schema.cmd = ..\scripts\schema.cmd
1717
..\tg = ..\tg
18+
..\tg-proxy = ..\tg-proxy
19+
..\tg-proxy.cmd = ..\tg-proxy.cmd
1820
..\tg.cmd = ..\tg.cmd
1921
..\scripts\vertices.cmd = ..\scripts\vertices.cmd
2022
EndProjectSection

src/TigerGraph.Proxy/Program.cs

+41-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using System.Text;
56
using System.Net;
67

78
using Microsoft.AspNetCore.Hosting;
@@ -17,44 +18,66 @@ public class Program
1718
#region Entry-point
1819
public static void Main(string[] args)
1920
{
21+
/* Configure the Serilog Logger */
2022
var config = new LoggerConfiguration();
2123
Log.Logger = config.MinimumLevel.Debug().Enrich.FromLogContext().WriteTo.Console().CreateLogger();
2224

23-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TG_TOKEN")))
25+
/* Check for required environment vars */
26+
if (string.IsNullOrEmpty(TG_TOKEN))
2427
{
2528
Log.Error("The environment variable {0} could not be read.", "TG_TOKEN");
2629
return;
2730
}
28-
else if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TG_SERVER_URL")))
31+
else if (string.IsNullOrEmpty(TG_REST_SERVER_URL))
2932
{
30-
Log.Error("The environment variable {0} could not be read.", "TG_SERVER_URL");
33+
Log.Error("The environment variable {0} could not be read.", "TG_REST_SERVER_URL");
3134
return;
3235
}
33-
else if (!Uri.TryCreate(Environment.GetEnvironmentVariable("TG_SERVER_URL"), UriKind.Absolute, out Uri u))
36+
else if (string.IsNullOrEmpty(TG_GSQL_SERVER_URL))
3437
{
35-
Log.Error("The environment variable {0}:{1} is not a valid URI.", "TG_SERVER_URL", Environment.GetEnvironmentVariable("TG_SERVER_URL"));
38+
Log.Error("The environment variable {0} could not be read.", "TG_GSQL_SERVER_URL");
39+
return;
40+
}
41+
else if (!Uri.TryCreate(TG_REST_SERVER_URL, UriKind.Absolute, out Uri rsu))
42+
{
43+
Log.Error("The environment variable {0}:{1} is not a valid URI.", "TG_REST_SERVER_URL", TG_REST_SERVER_URL);
44+
return;
45+
}
46+
else if (!Uri.TryCreate(TG_GSQL_SERVER_URL, UriKind.Absolute, out Uri gu))
47+
{
48+
Log.Error("The environment variable {0}:{1} is not a valid URI.", "TG_GSQL_SERVER_URL", TG_GSQL_SERVER_URL);
3649
return;
3750
}
3851
else
3952
{
40-
WebClient wc = new WebClient();
41-
wc.Headers.Add("Authorization", "Bearer " + Environment.GetEnvironmentVariable("TG_TOKEN"));
53+
/* Setup authentication to the TG servers */
54+
RestServerWebClient = new WebClient();
55+
RestServerWebClient.Headers.Add("Authorization", "Bearer " + TG_TOKEN);
56+
GsqlServerWebClient = new WebClient();
57+
string credentials = Convert.ToBase64String(
58+
Encoding.ASCII.GetBytes(TG_USER + ":" + TG_PASS));
59+
GsqlServerWebClient.Headers[HttpRequestHeader.Authorization] = string.Format(
60+
"Basic {0}", credentials);
61+
Log.Information("Authentication to REST++ server at {0} and GSQL server at {1} initialized.", TG_REST_SERVER_URL, TG_GSQL_SERVER_URL);
62+
/* Setup the timer for pinging the server.*/
4263
var pingTimer = new System.Timers.Timer(1000 * 60 * 15);
4364
pingTimer.Elapsed += (sender, e) => {
4465
var now = DateTime.Now;
4566
try
4667
{
47-
wc.DownloadString(u.ToString() + "echo");
48-
Log.Information("Pinged {0} at {1}.", u, now);
68+
RestServerWebClient.DownloadString(rsu.ToString() + "echo");
69+
Log.Information("Pinged {0} at {1}.", rsu, now);
4970
}
5071
catch (Exception ex)
5172
{
52-
Log.Error(ex, "Error attempting to ping {0} at {1}.", u, now);
73+
Log.Error(ex, "Error attempting to ping {0} at {1}.", rsu, now);
5374
}
5475
};
5576
pingTimer.Enabled = true;
5677
CreateHostBuilder(args).Build().Run();
57-
wc.Dispose();
78+
/* After the webhost shuts down dispose of any unmanaged resources. */
79+
RestServerWebClient.Dispose();
80+
GsqlServerWebClient.Dispose();
5881
pingTimer.Dispose();
5982
}
6083
}
@@ -70,13 +93,20 @@ public static void Main(string[] args)
7093
public static string TG_REST_SERVER_URL = Environment.GetEnvironmentVariable("TG_REST_SERVER_URL");
7194

7295
public static string TG_GSQL_SERVER_URL = Environment.GetEnvironmentVariable("TG_GSQL_SERVER_URL");
96+
97+
public static WebClient RestServerWebClient = new WebClient();
98+
99+
public static WebClient GsqlServerWebClient = new WebClient();
73100
#endregion
101+
102+
#region Methods
74103
public static IHostBuilder CreateHostBuilder(string[] args) =>
75104
Host.CreateDefaultBuilder(args)
76105
.ConfigureWebHostDefaults(webBuilder =>
77106
{
78107
webBuilder.UseSerilog();
79108
webBuilder.UseStartup<Startup>();
80109
});
110+
#endregion
81111
}
82112
}

src/TigerGraph.Proxy/ProxyController.cs

+19-11
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@ namespace TigerGraph.Proxy
1616
{
1717
public class ProxyController : ControllerBase
1818
{
19-
private static ILogger<ProxyController> log;
19+
#region Constructors
2020
public ProxyController(ILogger<ProxyController> logger)
2121
{
2222
log = logger;
2323
}
2424

25-
private HttpProxyOptions _httpOptions = HttpProxyOptionsBuilder.Instance
25+
#endregion
26+
27+
#region Actions
28+
[Route("p/{**rest}")]
29+
public Task RestServerProxy(string rest)
30+
{
31+
log.LogInformation("Proxying request {0} to {1}...", rest, $"{Program.TG_REST_SERVER_URL}{rest}");
32+
return this.HttpProxyAsync($"{Program.TG_REST_SERVER_URL}/{rest}", _restServerHttpOptions);
33+
}
34+
#endregion
35+
36+
#region Fields
37+
private static ILogger<ProxyController> log;
38+
39+
private HttpProxyOptions _restServerHttpOptions = HttpProxyOptionsBuilder.Instance
2640
.WithShouldAddForwardedHeaders(false)
2741
.WithHttpClientName("TigerGraphClient")
2842
.WithIntercept(context =>
@@ -31,8 +45,7 @@ public ProxyController(ILogger<ProxyController> logger)
3145
})
3246
.WithBeforeSend((c, hrm) =>
3347
{
34-
hrm.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("TG_TOKEN"));
35-
48+
hrm.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Program.TG_TOKEN);
3649
return Task.CompletedTask;
3750
})
3851
.WithAfterReceive((c, hrm) =>
@@ -48,12 +61,7 @@ public ProxyController(ILogger<ProxyController> logger)
4861
log.LogError(e, "Proxy to {0} error.", c.Request.Query);
4962
return Task.CompletedTask;
5063
}).Build();
51-
52-
[Route("p/{**rest}")]
53-
public Task Proxy(string rest)
54-
{
55-
log.LogInformation("Proxying request {0} to {1}...", rest, $"{Environment.GetEnvironmentVariable("TG_SERVER_URL")}{rest}");
56-
return this.HttpProxyAsync($"{Environment.GetEnvironmentVariable("TG_SERVER_URL")}{rest}", _httpOptions);
57-
}
64+
#endregion
5865
}
66+
5967
}

tg-proxy

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
cd src/TigerGraph.Proxy
3+
dotnet run bin/Debug/netcoreapp3.1/TigerGraph.Proxy.exe --urls=$1
4+
cd ../..

tg-proxy.cmd

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@echo off
2+
@setlocal
3+
4+
cd src\TigerGraph.Proxy
5+
dotnet run bin\Debug\netcoreapp3.1\TigerGraph.Proxy.exe --urls=%1
6+
cd ..\..

0 commit comments

Comments
 (0)