Skip to content

Commit

Permalink
Merge pull request #62 from rbonestell/yarp-dynamic-port
Browse files Browse the repository at this point in the history
WIP: Implementation outline for dynamic destination ports with YARP
  • Loading branch information
acfunk authored Feb 8, 2025
2 parents d77692b + 3ed1c59 commit 699f277
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
4 changes: 4 additions & 0 deletions FUNC/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public static async Task<NodeStatus> Get(string name)
var endpointAddressToken = config.GetValue("EndpointAddress");
string endpointAddress = endpointAddressToken?.Value<string>() ?? ":0";
port = int.Parse(endpointAddress[(endpointAddress.IndexOf(":") + 1)..]);
if (name == "algorand")
Shared.AlgoPort = port;
else if (name == "voi")
Shared.VoiPort = port;
}

string sc = string.Empty;
Expand Down
42 changes: 33 additions & 9 deletions FUNC/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FUNC;
using Microsoft.Net.Http.Headers;
//using Yarp.ReverseProxy.Transforms;
using Yarp.ReverseProxy.Transforms;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -18,14 +18,38 @@
});
});
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
//.AddTransforms(transformBuilderContext =>
//{
// transformBuilderContext.AddRequestTransform(transformContext =>
// {
// // TODO: change port on destination address based on node configuration value
// });
//});
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
.AddTransforms(transformBuilderContext =>
{
transformBuilderContext.AddRequestTransform(transformContext =>
{
// Get the original host
var ogPort = transformContext.HttpContext.Request.Host.Port;
var destPort = 8080;

// Check if it's a request to the legacy site
if (ogPort == 1234) {
destPort = Shared.VoiPort;

} else if (ogPort == 5678) {
// Algo Port
destPort = Shared.AlgoPort;
}

var destinationPrefix = transformContext.DestinationPrefix;
if (destinationPrefix != null)
{
var originalUri = new Uri(destinationPrefix);
var newUri = new UriBuilder(originalUri)
{
Port = destPort
}.Uri;
transformContext.DestinationPrefix = newUri.ToString();
}

return ValueTask.CompletedTask;
});
});

var app = builder.Build();

Expand Down
8 changes: 8 additions & 0 deletions FUNC/Shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FUNC
{
public static class Shared
{
public static int AlgoPort { get; set; } = 8081;
public static int VoiPort { get; set; } = 8082;
}
}

0 comments on commit 699f277

Please sign in to comment.