Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified route config #24

Merged
merged 3 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion samples/IslandGateway.Sample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
{
"RouteId": "backend1/route1",
"BackendId": "backend1",
"Rule": "Host('localhost') && Path('/{**catchall}')"
"Methods": [ "GET", "POST" ],
"Host": "localhost",
"Path": "/{**catchall}"
}
]
}
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 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 <see cref="Methods"/>, <see cref="Host"/>, and <see cref="Path"/>
/// and proxies matching requests to the backend identified by its <see cref="BackendId"/>.
/// </summary>
public sealed class GatewayRoute : IDeepCloneable<GatewayRoute>
Expand All @@ -18,9 +18,31 @@ public sealed class GatewayRoute : IDeepCloneable<GatewayRoute>
public string RouteId { get; set; }

/// <summary>
/// Rule that incoming requests must match for this route to apply. E.g. <c>Host('example.com')</c>.
/// Only match requests that use these optional HTTP methods. E.g. GET, POST.
/// </summary>
public string Rule { get; set; }
public string[] Methods { get; set; }
Tratcher marked this conversation as resolved.
Show resolved Hide resolved

/// <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; }

/// <summary>
/// Optionally, a priority value for this route. Routes with higher numbers take precedence over lower numbers.
Expand All @@ -44,7 +66,10 @@ GatewayRoute IDeepCloneable<GatewayRoute>.DeepClone()
return new GatewayRoute
{
RouteId = RouteId,
Rule = Rule,
Methods = (string[])Methods?.Clone(),
Host = Host,
Path = Path,
// Headers = Headers.DeepClone(); // TODO:
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.Methods,
Host = route.Host,
Path = route.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