Skip to content

Commit

Permalink
Simplified route config (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher authored Mar 25, 2020
1 parent cdeefb0 commit 8428ae4
Show file tree
Hide file tree
Showing 30 changed files with 266 additions and 964 deletions.
6 changes: 5 additions & 1 deletion samples/IslandGateway.Sample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
{
"RouteId": "backend1/route1",
"BackendId": "backend1",
"Rule": "Host('localhost') && Path('/{**catchall}')"
"Match": {
"Methods": [ "GET", "POST" ],
"Host": "localhost",
"Path": "/{**catchall}"
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;

namespace IslandGateway.Core.Abstractions
{
/// <summary>
/// Describes the matching criteria for a route.
/// </summary>
public class GatewayMatch : IDeepCloneable<GatewayMatch>
{
/// <summary>
/// Only match requests that use these optional HTTP methods. E.g. GET, POST.
/// </summary>
public IReadOnlyList<string> Methods { get; set; }

/// <summary>
/// Only match requests with the given Host header.
/// </summary>
public string Host { get; set; }

/// <summary>
/// Only match requests with the given Path pattern.
/// </summary>
public string Path { get; set; }

// TODO:
/// <summary>
/// Only match requests that contain all of these query parameters.
/// </summary>
// public ICollection<KeyValuePair<string, string>> QueryParameters { get; set; }

// TODO:
/// <summary>
/// Only match requests that contain all of these request headers.
/// </summary>
// public ICollection<KeyValuePair<string, string>> Headers { get; set; }

GatewayMatch IDeepCloneable<GatewayMatch>.DeepClone()
{
return new GatewayMatch()
{
Methods = Methods?.ToArray(),
Host = Host,
Path = Path,
// Headers = Headers.DeepClone(); // TODO:
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand All @@ -7,7 +7,7 @@
namespace IslandGateway.Core.Abstractions
{
/// <summary>
/// Describes a route that matches incoming requests based on a <see cref="Rule"/>
/// Describes a route that matches incoming requests based on a the <see cref="Match"/> criteria
/// and proxies matching requests to the backend identified by its <see cref="BackendId"/>.
/// </summary>
public sealed class GatewayRoute : IDeepCloneable<GatewayRoute>
Expand All @@ -17,10 +17,7 @@ public sealed class GatewayRoute : IDeepCloneable<GatewayRoute>
/// </summary>
public string RouteId { get; set; }

/// <summary>
/// Rule that incoming requests must match for this route to apply. E.g. <c>Host('example.com')</c>.
/// </summary>
public string Rule { get; set; }
public GatewayMatch Match { get; private set; } = new GatewayMatch();

/// <summary>
/// Optionally, a priority value for this route. Routes with higher numbers take precedence over lower numbers.
Expand All @@ -44,7 +41,7 @@ GatewayRoute IDeepCloneable<GatewayRoute>.DeepClone()
return new GatewayRoute
{
RouteId = RouteId,
Rule = Rule,
Match = Match.DeepClone(),
Priority = Priority,
BackendId = BackendId,
Metadata = Metadata?.DeepClone(StringComparer.Ordinal),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using IslandGateway.Common.Abstractions.Telemetry;
Expand Down Expand Up @@ -43,8 +43,6 @@ public static IIslandGatewayBuilder AddInMemoryRepos(this IIslandGatewayBuilder
public static IIslandGatewayBuilder AddConfigBuilder(this IIslandGatewayBuilder builder)
{
builder.Services.AddSingleton<IDynamicConfigBuilder, DynamicConfigBuilder>();
builder.Services.AddSingleton<IRuleParser, RuleParser>();
builder.Services.AddSingleton<IRouteParser, RouteParser>();
builder.Services.AddSingleton<IRouteValidator, RouteValidator>();
builder.Services.AddSingleton<IRuntimeRouteBuilder, RuntimeRouteBuilder>();
return builder;
Expand Down
4 changes: 0 additions & 4 deletions src/IslandGateway.Core/IslandGateway.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Superpower" Version="2.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IslandGateway.Common\IslandGateway.Common.csproj" />
<ProjectReference Include="..\IslandGateway.Utilities\IslandGateway.Utilities.csproj" />
Expand Down
3 changes: 1 addition & 2 deletions src/IslandGateway.Core/Service/Config/ConfigErrors.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace IslandGateway.Core.Service
Expand All @@ -14,7 +14,6 @@ internal static class ConfigErrors
internal const string RouteUnknownBackend = "Route_UnknownBackend";
internal const string RouteNoBackends = "Route_NoBackends";
internal const string RouteUnsupportedAction = "Route_UnsupportedAction";
internal const string RouteBadRule = "Route_BadRule";

internal const string ParsedRouteMissingId = "ParsedRoute_MissingId";
internal const string ParsedRouteRuleHasNoMatchers = "ParsedRoute_RuleHasNoMatchers";
Expand Down
22 changes: 10 additions & 12 deletions src/IslandGateway.Core/Service/Config/DynamicConfigBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand All @@ -16,26 +16,22 @@ internal class DynamicConfigBuilder : IDynamicConfigBuilder
private readonly IBackendsRepo _backendsRepo;
private readonly IBackendEndpointsRepo _endpointsRepo;
private readonly IRoutesRepo _routesRepo;
private readonly IRouteParser _routeParser;
private readonly IRouteValidator _parsedRouteValidator;

public DynamicConfigBuilder(
IBackendsRepo backendsRepo,
IBackendEndpointsRepo endpointsRepo,
IRoutesRepo routesRepo,
IRouteParser routeParser,
IRouteValidator parsedRouteValidator)
{
Contracts.CheckValue(backendsRepo, nameof(backendsRepo));
Contracts.CheckValue(endpointsRepo, nameof(endpointsRepo));
Contracts.CheckValue(routesRepo, nameof(routesRepo));
Contracts.CheckValue(routeParser, nameof(routeParser));
Contracts.CheckValue(parsedRouteValidator, nameof(parsedRouteValidator));

_backendsRepo = backendsRepo;
_endpointsRepo = endpointsRepo;
_routesRepo = routesRepo;
_routeParser = routeParser;
_parsedRouteValidator = parsedRouteValidator;
}

Expand Down Expand Up @@ -118,14 +114,16 @@ private async Task<IList<ParsedRoute>> GetRoutesAsync(IConfigErrorReporter error
continue;
}

var parsedResult = _routeParser.ParseRoute(route, errorReporter);
if (!parsedResult.IsSuccess)
{
// routeParser already reported error message
continue;
}
var parsedRoute = new ParsedRoute {
RouteId = route.RouteId,
Methods = route.Match.Methods,
Host = route.Match.Host,
Path = route.Match.Path,
Priority = route.Priority,
BackendId = route.BackendId,
Metadata = route.Metadata,
};

var parsedRoute = parsedResult.Value;
if (!_parsedRouteValidator.ValidateRoute(parsedRoute, errorReporter))
{
// parsedRouteValidator already reported error message
Expand Down
19 changes: 0 additions & 19 deletions src/IslandGateway.Core/Service/Config/IRouteParser.cs

This file was deleted.

48 changes: 0 additions & 48 deletions src/IslandGateway.Core/Service/Config/RouteParser.cs

This file was deleted.

22 changes: 0 additions & 22 deletions src/IslandGateway.Core/Service/Config/RuleParsing/IRuleParser.cs

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8428ae4

Please sign in to comment.