Skip to content

Commit

Permalink
Convert to file-scoped namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Aug 10, 2023
1 parent f4d9633 commit 6ac363c
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 309 deletions.
47 changes: 23 additions & 24 deletions src/Ocelot/Configuration/Creator/DownstreamAddressesCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,41 @@

using Ocelot.Configuration.File;

namespace Ocelot.Configuration.Creator
namespace Ocelot.Configuration.Creator;

public class DownstreamAddressesCreator : IDownstreamAddressesCreator
{
public class DownstreamAddressesCreator : IDownstreamAddressesCreator
{
private readonly IOcelotLogger _logger;
private readonly IOcelotLogger _logger;

public DownstreamAddressesCreator(IOcelotLoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<DownstreamAddressesCreator>();
}
public DownstreamAddressesCreator(IOcelotLoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<DownstreamAddressesCreator>();
}

public List<DownstreamHostAndPort> Create(FileRoute route, FileGlobalConfiguration globalConfiguration)
{
return EnumerateDownstreamHosts(route, globalConfiguration).ToList();
}
public List<DownstreamHostAndPort> Create(FileRoute route, FileGlobalConfiguration globalConfiguration)
{
return EnumerateDownstreamHosts(route, globalConfiguration).ToList();
}

private IEnumerable<DownstreamHostAndPort> EnumerateDownstreamHosts(FileRoute route, FileGlobalConfiguration fileGlobalConfiguration)
private IEnumerable<DownstreamHostAndPort> EnumerateDownstreamHosts(FileRoute route, FileGlobalConfiguration fileGlobalConfiguration)
{
foreach (var downstreamHost in route.DownstreamHostAndPorts)
{
foreach (var downstreamHost in route.DownstreamHostAndPorts)
if (string.IsNullOrWhiteSpace(downstreamHost.GlobalHostKey) == false)
{
if (string.IsNullOrWhiteSpace(downstreamHost.GlobalHostKey) == false)
if (fileGlobalConfiguration.DownstreamHosts.TryGetValue(downstreamHost.GlobalHostKey, out var globalDownstreamHost))
{
if (fileGlobalConfiguration.DownstreamHosts.TryGetValue(downstreamHost.GlobalHostKey, out var globalDownstreamHost))
{
yield return new DownstreamHostAndPort(globalDownstreamHost.Host, globalDownstreamHost.Port);
}
else
{
_logger.LogWarning($"Global configuration doesn't contain the definition of '{downstreamHost.GlobalHostKey}' downstream host");
}
yield return new DownstreamHostAndPort(globalDownstreamHost.Host, globalDownstreamHost.Port);
}
else
{
yield return new DownstreamHostAndPort(downstreamHost.Host, downstreamHost.Port);
_logger.LogWarning($"Global configuration doesn't contain the definition of '{downstreamHost.GlobalHostKey}' downstream host");
}
}
else
{
yield return new DownstreamHostAndPort(downstreamHost.Host, downstreamHost.Port);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

using Ocelot.Configuration.File;

namespace Ocelot.Configuration.Creator
namespace Ocelot.Configuration.Creator;

public interface IDownstreamAddressesCreator
{
public interface IDownstreamAddressesCreator
{
List<DownstreamHostAndPort> Create(FileRoute route, FileGlobalConfiguration globalConfiguration);
}
List<DownstreamHostAndPort> Create(FileRoute route, FileGlobalConfiguration globalConfiguration);
}
31 changes: 15 additions & 16 deletions src/Ocelot/Configuration/Creator/RouteKeyCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
using Ocelot.Configuration.File;
using Ocelot.LoadBalancer.LoadBalancers;

namespace Ocelot.Configuration.Creator
namespace Ocelot.Configuration.Creator;

public class RouteKeyCreator : IRouteKeyCreator
{
public class RouteKeyCreator : IRouteKeyCreator
{
public string Create(FileRoute fileRoute) => IsStickySession(fileRoute)
? $"{nameof(CookieStickySessions)}:{fileRoute.LoadBalancerOptions.Key}"
: $"{fileRoute.UpstreamPathTemplate}|{ToUpstreamHttpMethodPart(fileRoute)}|{ToDownstreamHostPart(fileRoute)}";
public string Create(FileRoute fileRoute) => IsStickySession(fileRoute)
? $"{nameof(CookieStickySessions)}:{fileRoute.LoadBalancerOptions.Key}"
: $"{fileRoute.UpstreamPathTemplate}|{ToUpstreamHttpMethodPart(fileRoute)}|{ToDownstreamHostPart(fileRoute)}";

private static bool IsStickySession(FileRoute fileRoute) =>
!string.IsNullOrEmpty(fileRoute.LoadBalancerOptions.Type)
&& !string.IsNullOrEmpty(fileRoute.LoadBalancerOptions.Key)
&& fileRoute.LoadBalancerOptions.Type == nameof(CookieStickySessions);
private static bool IsStickySession(FileRoute fileRoute) =>
!string.IsNullOrEmpty(fileRoute.LoadBalancerOptions.Type)
&& !string.IsNullOrEmpty(fileRoute.LoadBalancerOptions.Key)
&& fileRoute.LoadBalancerOptions.Type == nameof(CookieStickySessions);

private static string ToDownstreamHostPart(FileRoute fileRoute)
=> string.Join(",", fileRoute.DownstreamHostAndPorts
.Select(x => string.IsNullOrWhiteSpace(x.GlobalHostKey) ? $"{x.Host}:{x.Port}": x.GlobalHostKey));
private static string ToDownstreamHostPart(FileRoute fileRoute)
=> string.Join(",", fileRoute.DownstreamHostAndPorts
.Select(x => string.IsNullOrWhiteSpace(x.GlobalHostKey) ? $"{x.Host}:{x.Port}": x.GlobalHostKey));

private static string ToUpstreamHttpMethodPart(FileRoute fileRoute)
=> string.Join(",", fileRoute.UpstreamHttpMethod);
}
private static string ToUpstreamHttpMethodPart(FileRoute fileRoute)
=> string.Join(",", fileRoute.UpstreamHttpMethod);
}

0 comments on commit 6ac363c

Please sign in to comment.