Skip to content

Commit

Permalink
Merge pull request ThreeMammals#18 from TomPallister/split-downstream…
Browse files Browse the repository at this point in the history
…-template-into-parts

Split downstream template into parts
  • Loading branch information
TomPallister authored Jan 21, 2017
2 parents 622c49d + d7ad6df commit 3f71bd5
Show file tree
Hide file tree
Showing 58 changed files with 1,041 additions and 218 deletions.
2 changes: 2 additions & 0 deletions Ocelot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Ocelot.nuspec = Ocelot.nuspec
push-to-nuget.bat = push-to-nuget.bat
README.md = README.md
run-acceptance-tests.bat = run-acceptance-tests.bat
run-benchmarks.bat = run-benchmarks.bat
run-tests.bat = run-tests.bat
run-unit-tests.bat = run-unit-tests.bat
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Ocelot", "src\Ocelot\Ocelot.xproj", "{D6DF4206-0DBA-41D8-884D-C3E08290FDBB}"
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,20 @@ In order to set up a ReRoute you need to add one to the json array called ReRout
the following.

{
"DownstreamTemplate": "http://jsonplaceholder.typicode.com/posts/{postId}",
"DownstreamPathTemplate": "/api/posts/{postId}",
"DownstreamScheme": "https",
"DownstreamPort": 80,
"DownstreamHost" "localhost"
"UpstreamTemplate": "/posts/{postId}",
"UpstreamHttpMethod": "Put"
}

The DownstreamTemplate is the URL that this request will be forwarded to.
The DownstreamPathTemplate,Scheme, Port and Host make the URL that this request will be forwarded to.
The UpstreamTemplate is the URL that Ocelot will use to identity which
DownstreamTemplate to use for a given request. Finally the UpstreamHttpMethod is used so
DownstreamPathTemplate to use for a given request. Finally the UpstreamHttpMethod is used so
Ocelot can distinguish between requests to the same URL and is obviously needed to work :)
In Ocelot you can add placeholders for variables to your Templates in the form of {something}.
The placeholder needs to be in both the DownstreamTemplate and UpstreamTemplate. If it is
The placeholder needs to be in both the DownstreamPathTemplate and UpstreamTemplate. If it is
Ocelot will attempt to replace the placeholder with the correct variable value from the
Upstream URL when the request comes in.

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ build_script:
test_script:
- run-tests.bat
after_test:
- push-to-nuget.bat %appveyor_build_version%-rc1 %nugetApiKey%
- push-to-nuget.bat %appveyor_build_version% %nugetApiKey%
cache:
- '%USERPROFILE%\.nuget\packages'
19 changes: 14 additions & 5 deletions configuration-explanation.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{
"ReRoutes": [
{
# The url we are forwarding the request to, ocelot will not add a trailing slash
"DownstreamTemplate": "http://somehost.com/identityserverexample",
# The path we are listening on for this re route, Ocelot will add a trailing slash to
# this property. Then when a request is made Ocelot makes sure a trailing slash is added
# to that so everything matches
# The downstream path we are forwarding the request to, ocelot will not add a trailing slash.
# Ocelot replaces any placeholders {etc} with matched values from the incoming request.
"DownstreamPathTemplate": "/identityserverexample/{someid}/something",
# The scheme you want Ocelot to use when making the downstream request
"DownstreamScheme": "https",
# The port you want Ocelot to use when making the downstream request, will default to
# scheme if nothing set
"DownstreamPort": 80,
# The host address of the downstream service, should not have a trailing slash or scheme
# if there is a trailing slash Ocelot will remove it.
"DownstreamHost" "localhost"
# The path template we are listening on for this re route, Ocelot will add a trailing
# slash to this property. Then when a request is made Ocelot makes sure a trailing
# slash is added, so everything matches
"UpstreamTemplate": "/identityserverexample",
# The method we are listening for on this re route
"UpstreamHttpMethod": "Get",
Expand Down
8 changes: 8 additions & 0 deletions run-acceptance-tests.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
echo Running Ocelot.AcceptanceTests
cd test/Ocelot.AcceptanceTests/
dotnet restore
dotnet test
cd ../../

echo Restoring Ocelot.ManualTest
dotnet restore test/Ocelot.ManualTest/
19 changes: 2 additions & 17 deletions run-tests.bat
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@
echo -------------------------

echo Restoring Ocelot
dotnet restore src/Ocelot

echo Restoring Ocelot.ManualTest
dotnet restore test/Ocelot.ManualTest/

echo Running Ocelot.UnitTests
dotnet restore test/Ocelot.UnitTests/
dotnet test test/Ocelot.UnitTests/

echo Running Ocelot.AcceptanceTests
cd test/Ocelot.AcceptanceTests/
dotnet restore
dotnet test
cd ../../
./run-unit-tests.bat
./run-acceptance-tests.bat
8 changes: 8 additions & 0 deletions run-unit-tests.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
echo -------------------------

echo Restoring Ocelot
dotnet restore src/Ocelot

echo Running Ocelot.UnitTests
dotnet restore test/Ocelot.UnitTests/
dotnet test test/Ocelot.UnitTests/
66 changes: 60 additions & 6 deletions src/Ocelot/Configuration/Builder/ReRouteBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Ocelot.Values;

namespace Ocelot.Configuration.Builder
{
public class ReRouteBuilder
{
private string _downstreamTemplate;
private string _downstreamPathTemplate;
private string _upstreamTemplate;
private string _upstreamTemplatePattern;
private string _upstreamHttpMethod;
Expand All @@ -23,15 +25,58 @@ public class ReRouteBuilder
private string _requestIdHeaderKey;
private bool _isCached;
private CacheOptions _fileCacheOptions;
private bool _useServiceDiscovery;
private string _serviceName;
private string _serviceDiscoveryProvider;
private string _serviceDiscoveryAddress;
private string _downstreamScheme;
private string _downstreamHost;
private int _dsPort;

public ReRouteBuilder()
{
_additionalScopes = new List<string>();
}

public ReRouteBuilder WithDownstreamTemplate(string input)
public ReRouteBuilder WithDownstreamScheme(string downstreamScheme)
{
_downstreamTemplate = input;
_downstreamScheme = downstreamScheme;
return this;
}

public ReRouteBuilder WithDownstreamHost(string downstreamHost)
{
_downstreamHost = downstreamHost;
return this;
}

public ReRouteBuilder WithServiceDiscoveryAddress(string serviceDiscoveryAddress)
{
_serviceDiscoveryAddress = serviceDiscoveryAddress;
return this;
}

public ReRouteBuilder WithServiceDiscoveryProvider(string serviceDiscoveryProvider)
{
_serviceDiscoveryProvider = serviceDiscoveryProvider;
return this;
}

public ReRouteBuilder WithServiceName(string serviceName)
{
_serviceName = serviceName;
return this;
}

public ReRouteBuilder WithUseServiceDiscovery(bool useServiceDiscovery)
{
_useServiceDiscovery = useServiceDiscovery;
return this;
}

public ReRouteBuilder WithDownstreamPathTemplate(string input)
{
_downstreamPathTemplate = input;
return this;
}

Expand Down Expand Up @@ -141,12 +186,21 @@ public ReRouteBuilder WithCacheOptions(CacheOptions input)
return this;
}

public ReRouteBuilder WithDownstreamPort(int port)
{
_dsPort = port;
return this;
}

public ReRoute Build()
{
return new ReRoute(_downstreamTemplate, _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern,
Func<HostAndPort> downstreamHostFunc = () => new HostAndPort(_downstreamHost, _dsPort);

return new ReRoute(new DownstreamPathTemplate(_downstreamPathTemplate), _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern,
_isAuthenticated, new AuthenticationOptions(_authenticationProvider, _authenticationProviderUrl, _scopeName,
_requireHttps, _additionalScopes, _scopeSecret), _configHeaderExtractorProperties, _claimToClaims, _routeClaimRequirement,
_isAuthorised, _claimToQueries, _requestIdHeaderKey, _isCached, _fileCacheOptions);
_isAuthorised, _claimToQueries, _requestIdHeaderKey, _isCached, _fileCacheOptions, _serviceName,
_useServiceDiscovery, _serviceDiscoveryAddress, _serviceDiscoveryProvider, downstreamHostFunc, _downstreamScheme);
}
}
}
20 changes: 16 additions & 4 deletions src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Ocelot.Configuration.Validator;
using Ocelot.Responses;
using Ocelot.Utilities;
using Ocelot.Values;

namespace Ocelot.Configuration.Creator
{
Expand Down Expand Up @@ -91,6 +92,13 @@ private ReRoute SetUpReRoute(FileReRoute reRoute, FileGlobalConfiguration global
? globalConfiguration.RequestIdKey
: reRoute.RequestIdKey;

var useServiceDiscovery = !string.IsNullOrEmpty(reRoute.ServiceName)
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Address)
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Provider);


Func<HostAndPort> downstreamHostAndPortFunc = () => new HostAndPort(reRoute.DownstreamHost.Trim('/'), reRoute.DownstreamPort);

if (isAuthenticated)
{
var authOptionsForRoute = new AuthenticationOptions(reRoute.AuthenticationOptions.Provider,
Expand All @@ -102,18 +110,22 @@ private ReRoute SetUpReRoute(FileReRoute reRoute, FileGlobalConfiguration global
var claimsToClaims = GetAddThingsToRequest(reRoute.AddClaimsToRequest);
var claimsToQueries = GetAddThingsToRequest(reRoute.AddQueriesToRequest);

return new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate,
return new ReRoute(new DownstreamPathTemplate(reRoute.DownstreamPathTemplate), reRoute.UpstreamTemplate,
reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
authOptionsForRoute, claimsToHeaders, claimsToClaims,
reRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries,
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds));
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostAndPortFunc, reRoute.DownstreamScheme);
}

return new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate,
return new ReRoute(new DownstreamPathTemplate(reRoute.DownstreamPathTemplate), reRoute.UpstreamTemplate,
reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
null, new List<ClaimToThing>(), new List<ClaimToThing>(),
reRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>(),
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds));
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostAndPortFunc, reRoute.DownstreamScheme);
}

private string BuildUpstreamTemplate(FileReRoute reRoute)
Expand Down
5 changes: 5 additions & 0 deletions src/Ocelot/Configuration/File/FileGlobalConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
{
public class FileGlobalConfiguration
{
public FileGlobalConfiguration()
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider();
}
public string RequestIdKey { get; set; }
public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;}
}
}
6 changes: 5 additions & 1 deletion src/Ocelot/Configuration/File/FileReRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public FileReRoute()
FileCacheOptions = new FileCacheOptions();
}

public string DownstreamTemplate { get; set; }
public string DownstreamPathTemplate { get; set; }
public string UpstreamTemplate { get; set; }
public string UpstreamHttpMethod { get; set; }
public FileAuthenticationOptions AuthenticationOptions { get; set; }
Expand All @@ -25,5 +25,9 @@ public FileReRoute()
public string RequestIdKey { get; set; }
public FileCacheOptions FileCacheOptions { get; set; }
public bool ReRouteIsCaseSensitive { get; set; }
public string ServiceName { get; set; }
public string DownstreamScheme {get;set;}
public string DownstreamHost {get;set;}
public int DownstreamPort { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/Ocelot/Configuration/File/FileServiceDiscoveryProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Ocelot.Configuration.File
{
public class FileServiceDiscoveryProvider
{
public string Provider {get;set;}
public string Address {get;set;}
}
}
25 changes: 20 additions & 5 deletions src/Ocelot/Configuration/ReRoute.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Ocelot.Values;

namespace Ocelot.Configuration
{
public class ReRoute
{
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern,
public ReRoute(DownstreamPathTemplate downstreamPathTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern,
bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToThing> configurationHeaderExtractorProperties,
List<ClaimToThing> claimsToClaims, Dictionary<string, string> routeClaimsRequirement, bool isAuthorised, List<ClaimToThing> claimsToQueries,
string requestIdKey, bool isCached, CacheOptions fileCacheOptions)
string requestIdKey, bool isCached, CacheOptions fileCacheOptions, string serviceName, bool useServiceDiscovery,
string serviceDiscoveryProvider, string serviceDiscoveryAddress, Func<HostAndPort> downstreamHostAndPort, string downstreamScheme)
{
DownstreamTemplate = downstreamTemplate;
DownstreamPathTemplate = downstreamPathTemplate;
UpstreamTemplate = upstreamTemplate;
UpstreamHttpMethod = upstreamHttpMethod;
UpstreamTemplatePattern = upstreamTemplatePattern;
Expand All @@ -26,9 +29,15 @@ public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstre
?? new List<ClaimToThing>();
ClaimsToHeaders = configurationHeaderExtractorProperties
?? new List<ClaimToThing>();
ServiceName = serviceName;
UseServiceDiscovery = useServiceDiscovery;
ServiceDiscoveryProvider = serviceDiscoveryProvider;
ServiceDiscoveryAddress = serviceDiscoveryAddress;
DownstreamHostAndPort = downstreamHostAndPort;
DownstreamScheme = downstreamScheme;
}

public string DownstreamTemplate { get; private set; }
public DownstreamPathTemplate DownstreamPathTemplate { get; private set; }
public string UpstreamTemplate { get; private set; }
public string UpstreamTemplatePattern { get; private set; }
public string UpstreamHttpMethod { get; private set; }
Expand All @@ -42,5 +51,11 @@ public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstre
public string RequestIdKey { get; private set; }
public bool IsCached { get; private set; }
public CacheOptions FileCacheOptions { get; private set; }
public string ServiceName { get; private set;}
public bool UseServiceDiscovery { get; private set;}
public string ServiceDiscoveryProvider { get; private set;}
public string ServiceDiscoveryAddress { get; private set;}
public Func<HostAndPort> DownstreamHostAndPort {get;private set;}
public string DownstreamScheme {get;private set;}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Ocelot.Errors;

namespace Ocelot.Configuration.Validator
{
public class DownstreamPathTemplateAlreadyUsedError : Error
{
public DownstreamPathTemplateAlreadyUsedError(string message) : base(message, OcelotErrorCode.DownstreampathTemplateAlreadyUsedError)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Ocelot.Errors;

namespace Ocelot.Configuration.Validator
{
public class DownstreamPathTemplateContainsSchemeError : Error
{
public DownstreamPathTemplateContainsSchemeError(string message)
: base(message, OcelotErrorCode.DownstreamPathTemplateContainsSchemeError)
{
}
}
}

This file was deleted.

Loading

0 comments on commit 3f71bd5

Please sign in to comment.