mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 04:18:16 +08:00
method to match urls and template urls
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
@ -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")});
|
||||
}
|
||||
}
|
||||
}
|
@ -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;}
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Router
|
||||
{
|
||||
public class RouteKeyAlreadyExists : Error
|
||||
{
|
||||
public RouteKeyAlreadyExists(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Router
|
||||
{
|
||||
public class RouteKeyDoesNotExist : Error
|
||||
{
|
||||
public RouteKeyDoesNotExist(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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()});
|
||||
}
|
||||
}
|
||||
}
|
@ -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;}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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()});
|
||||
}
|
||||
}
|
||||
}
|
@ -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;}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user