method to match urls and template urls

This commit is contained in:
Tom Gardham-Pallister
2016-07-08 19:33:22 +01:00
parent 7332da7230
commit 4f2d94ceba
21 changed files with 718 additions and 96 deletions

View File

@ -1,10 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router
{
public interface IRouterService
{
Response AddRoute(string apiKey, string upstreamApiBaseUrl);
Response<Route> GetRoute(string apiKey);
}
}

View File

@ -1,41 +0,0 @@
using System;
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router
{
public class InMemoryRouterService : IRouterService
{
private readonly Dictionary<string, string> _routes;
public InMemoryRouterService()
{
_routes = new Dictionary<string,string>();
}
public Response AddRoute(string apiKey, string upstreamApiBaseUrl)
{
if(_routes.ContainsKey(apiKey))
{
return new ErrorResponse(new List<Error>(){new RouteKeyAlreadyExists("This key has already been used")});
}
_routes.Add(apiKey, upstreamApiBaseUrl);
return new OkResponse();
}
public Response<Route> GetRoute(string apiKey)
{
Console.WriteLine("looking for {0}", apiKey);
string upstreamApiBaseUrl = null;
if(_routes.TryGetValue(apiKey, out upstreamApiBaseUrl))
{
return new OkResponse<Route>(new Route(apiKey, upstreamApiBaseUrl));
}
Console.WriteLine("Couldnt find it");
return new ErrorResponse<Route>(new List<Error>(){new RouteKeyDoesNotExist("This key does not exist")});
}
}
}

View File

@ -1,14 +0,0 @@
namespace Ocelot.Library.Infrastructure.Router
{
public class Route
{
public Route(string apiKey, string upstreamRoute)
{
ApiKey = apiKey;
UpstreamRoute = upstreamRoute;
}
public string ApiKey {get;private set;}
public string UpstreamRoute {get;private set;}
}
}

View File

@ -1,12 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router
{
public class RouteKeyAlreadyExists : Error
{
public RouteKeyAlreadyExists(string message)
: base(message)
{
}
}
}

View File

@ -1,12 +0,0 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router
{
public class RouteKeyDoesNotExist : Error
{
public RouteKeyDoesNotExist(string message)
: base(message)
{
}
}
}

View File

@ -0,0 +1,10 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public interface IUpstreamRouter
{
Response AddRoute(string downstreamUrl, string upstreamUrl);
Response<Route> GetRoute(string downstreamUrl);
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public class InMemoryUpstreamRouter : IUpstreamRouter
{
private readonly Dictionary<string, string> _routes;
public InMemoryUpstreamRouter()
{
_routes = new Dictionary<string,string>();
}
public Response AddRoute(string downstreamUrl, string upstreamUrl)
{
if(_routes.ContainsKey(downstreamUrl))
{
return new ErrorResponse(new List<Error>(){new RouteKeyAlreadyExists()});
}
_routes.Add(downstreamUrl, upstreamUrl);
return new OkResponse();
}
public Response<Route> GetRoute(string downstreamUrl)
{
string upstreamUrl = null;
if(_routes.TryGetValue(downstreamUrl, out upstreamUrl))
{
return new OkResponse<Route>(new Route(downstreamUrl, upstreamUrl));
}
return new ErrorResponse<Route>(new List<Error>(){new RouteKeyDoesNotExist()});
}
}
}

View File

@ -0,0 +1,14 @@
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public class Route
{
public Route(string downstreamUrl, string upstreamUrl)
{
DownstreamUrl = downstreamUrl;
UpstreamUrl = upstreamUrl;
}
public string DownstreamUrl {get;private set;}
public string UpstreamUrl {get;private set;}
}
}

View File

@ -0,0 +1,12 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public class RouteKeyAlreadyExists : Error
{
public RouteKeyAlreadyExists()
: base("This key has already been used")
{
}
}
}

View File

@ -0,0 +1,12 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter
{
public class RouteKeyDoesNotExist : Error
{
public RouteKeyDoesNotExist()
: base("This key does not exist")
{
}
}
}

View File

@ -0,0 +1,12 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
{
public class DownstreamUrlPathTemplateAlreadyExists : Error
{
public DownstreamUrlPathTemplateAlreadyExists()
: base("This key has already been used")
{
}
}
}

View File

@ -0,0 +1,12 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
{
public class DownstreamUrlPathTemplateDoesNotExist : Error
{
public DownstreamUrlPathTemplateDoesNotExist()
: base("This key does not exist")
{
}
}
}

View File

@ -0,0 +1,10 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
{
public interface IUrlPathRouter
{
Response AddRoute(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate);
Response<UrlPath> GetRoute(string downstreamUrlPathTemplate);
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
{
public class InMemoryUrlPathRouter : IUrlPathRouter
{
private readonly Dictionary<string, string> _routes;
public InMemoryUrlPathRouter()
{
_routes = new Dictionary<string,string>();
}
public Response AddRoute(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate)
{
if(_routes.ContainsKey(downstreamUrlPathTemplate))
{
return new ErrorResponse(new List<Error>(){new DownstreamUrlPathTemplateAlreadyExists()});
}
_routes.Add(downstreamUrlPathTemplate, upstreamUrlPathTemplate);
return new OkResponse();
}
public Response<UrlPath> GetRoute(string downstreamUrlPathTemplate)
{
string upstreamUrlPathTemplate = null;
if(_routes.TryGetValue(downstreamUrlPathTemplate, out upstreamUrlPathTemplate))
{
return new OkResponse<UrlPath>(new UrlPath(downstreamUrlPathTemplate, upstreamUrlPathTemplate));
}
return new ErrorResponse<UrlPath>(new List<Error>(){new DownstreamUrlPathTemplateDoesNotExist()});
}
}
}

View File

@ -0,0 +1,14 @@
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
{
public class UrlPath
{
public UrlPath(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate)
{
DownstreamUrlPathTemplate = downstreamUrlPathTemplate;
UpstreamUrlPathTemplate = upstreamUrlPathTemplate;
}
public string DownstreamUrlPathTemplate {get;private set;}
public string UpstreamUrlPathTemplate {get;private set;}
}
}