regex for url match, means annoying constructor ocelot configuration object but cant work out a better way to do this at the moment

This commit is contained in:
TomPallister
2016-10-09 15:40:13 +01:00
parent f8ea87c91b
commit 1fddcf0836
38 changed files with 675 additions and 288 deletions

View File

@ -1,14 +0,0 @@
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.Configuration
{
public class Configuration
{
public Configuration()
{
ReRoutes = new List<ReRoute>();
}
public List<ReRoute> ReRoutes { get; set; }
}
}

View File

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration
{
public class DownstreamTemplateAlreadyUsedError : Error
{
public DownstreamTemplateAlreadyUsedError(string message) : base(message)
{
}
}
}

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration
{
public interface IConfigurationValidator
{
Response<ConfigurationValidationResult> IsValid(Configuration configuration);
}
}

View File

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.Configuration
{
public interface IOcelotConfiguration
{
List<ReRoute> ReRoutes { get; }
}
}

View File

@ -0,0 +1,57 @@
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Ocelot.Library.Infrastructure.Configuration.Yaml;
namespace Ocelot.Library.Infrastructure.Configuration
{
public class OcelotConfiguration : IOcelotConfiguration
{
private readonly IOptions<YamlConfiguration> _options;
private readonly List<ReRoute> _reRoutes;
private const string RegExMatchEverything = ".*";
private const string RegExMatchEndString = "$";
public OcelotConfiguration(IOptions<YamlConfiguration> options)
{
_options = options;
_reRoutes = new List<ReRoute>();
SetReRoutes();
}
private void SetReRoutes()
{
foreach(var reRoute in _options.Value.ReRoutes)
{
var upstreamTemplate = reRoute.UpstreamTemplate;
var placeholders = new List<string>();
for (int i = 0; i < upstreamTemplate.Length; i++)
{
if (IsPlaceHolder(upstreamTemplate, i))
{
var postitionOfPlaceHolderClosingBracket = upstreamTemplate.IndexOf('}', i);
var difference = postitionOfPlaceHolderClosingBracket - i + 1;
var variableName = upstreamTemplate.Substring(i, difference);
placeholders.Add(variableName);
}
}
foreach (var placeholder in placeholders)
{
upstreamTemplate = upstreamTemplate.Replace(placeholder, RegExMatchEverything);
}
upstreamTemplate = $"{upstreamTemplate}{RegExMatchEndString}";
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate));
}
}
private static bool IsPlaceHolder(string upstreamTemplate, int i)
{
return upstreamTemplate[i] == '{';
}
public List<ReRoute> ReRoutes => _reRoutes;
}
}

View File

@ -2,8 +2,17 @@
{
public class ReRoute
{
public string DownstreamTemplate { get; set; }
public string UpstreamTemplate { get; set; }
public string UpstreamHttpMethod { get; set; }
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern)
{
DownstreamTemplate = downstreamTemplate;
UpstreamTemplate = upstreamTemplate;
UpstreamHttpMethod = upstreamHttpMethod;
UpstreamTemplatePattern = upstreamTemplatePattern;
}
public string DownstreamTemplate { get; private set; }
public string UpstreamTemplate { get; private set; }
public string UpstreamTemplatePattern { get; private set; }
public string UpstreamHttpMethod { get; private set; }
}
}
}

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class ConfigurationValidationResult
{

View File

@ -2,11 +2,11 @@
using System.Linq;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class ConfigurationValidator : IConfigurationValidator
{
public Response<ConfigurationValidationResult> IsValid(Configuration configuration)
public Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration)
{
var duplicateUpstreamTemplates = configuration.ReRoutes
.Select(r => r.DownstreamTemplate)

View File

@ -0,0 +1,11 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class DownstreamTemplateAlreadyUsedError : Error
{
public DownstreamTemplateAlreadyUsedError(string message) : base(message)
{
}
}
}

View File

@ -0,0 +1,9 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public interface IConfigurationValidator
{
Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration);
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class YamlConfiguration
{
public YamlConfiguration()
{
ReRoutes = new List<YamlReRoute>();
}
public List<YamlReRoute> ReRoutes { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class YamlReRoute
{
public string DownstreamTemplate { get; set; }
public string UpstreamTemplate { get; set; }
public string UpstreamHttpMethod { get; set; }
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Options;
using Ocelot.Library.Infrastructure.Configuration;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
@ -9,24 +10,29 @@ namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
{
public class DownstreamRouteFinder : IDownstreamRouteFinder
{
private readonly IOptions<Configuration.Configuration> _configuration;
private readonly IOcelotConfiguration _configuration;
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
private readonly ITemplateVariableNameAndValueFinder _templateVariableNameAndValueFinder;
public DownstreamRouteFinder(IOptions<Configuration.Configuration> configuration, IUrlPathToUrlTemplateMatcher urlMatcher)
public DownstreamRouteFinder(IOcelotConfiguration configuration, IUrlPathToUrlTemplateMatcher urlMatcher, ITemplateVariableNameAndValueFinder templateVariableNameAndValueFinder)
{
_configuration = configuration;
_urlMatcher = urlMatcher;
_templateVariableNameAndValueFinder = templateVariableNameAndValueFinder;
}
public Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod)
{
foreach (var template in _configuration.Value.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase)))
foreach (var template in _configuration.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase)))
{
var urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplate);
var urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplatePattern);
if (urlMatch.Match)
if (urlMatch.Data.Match)
{
return new OkResponse<DownstreamRoute>(new DownstreamRoute(urlMatch.TemplateVariableNameAndValues, template.DownstreamTemplate));
var templateVariableNameAndValues = _templateVariableNameAndValueFinder.Find(upstreamUrlPath,
template.UpstreamTemplate);
return new OkResponse<DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, template.DownstreamTemplate));
}
}

View File

@ -5,12 +5,13 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.RequestBuilder
{
public class HttpRequestBuilder : IRequestBuilder
{
public async Task<Request> Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
public async Task<Response<Request>> Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
IRequestCookieCollection cookies, string queryString, string contentType)
{
var method = new HttpMethod(httpMethod);
@ -64,9 +65,8 @@ namespace Ocelot.Library.Infrastructure.RequestBuilder
cookieContainer.Add(uri, new Cookie(cookie.Key, cookie.Value));
}
}
return new Request(httpRequestMessage, cookieContainer);
return new OkResponse<Request>(new Request(httpRequestMessage, cookieContainer));
}
}
}

View File

@ -1,12 +1,13 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.RequestBuilder
{
public interface IRequestBuilder
{
Task<Request> Build(string httpMethod,
Task<Response<Request>> Build(string httpMethod,
string downstreamUrl,
Stream content,
IHeaderDictionary headers,

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public interface ITemplateVariableNameAndValueFinder
{
Response<List<TemplateVariableNameAndValue>> Find(string upstreamUrlPath, string upstreamUrlPathTemplate);
}
}

View File

@ -1,7 +1,9 @@
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public interface IUrlPathToUrlTemplateMatcher
{
UrlMatch Match(string upstreamUrlPath, string upstreamUrlPathTemplate);
Response<UrlMatch> Match(string upstreamUrlPath, string upstreamUrlPathTemplate);
}
}

View File

@ -0,0 +1,17 @@
using System.Text.RegularExpressions;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public class RegExUrlMatcher : IUrlPathToUrlTemplateMatcher
{
public Response<UrlMatch> Match(string upstreamUrlPath, string upstreamUrlPathTemplate)
{
var regex = new Regex(upstreamUrlPathTemplate);
return regex.IsMatch(upstreamUrlPath)
? new OkResponse<UrlMatch>(new UrlMatch(true))
: new OkResponse<UrlMatch>(new UrlMatch(false));
}
}
}

View File

@ -1,23 +1,16 @@
using System;
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public class UrlPathToUrlTemplateMatcher : IUrlPathToUrlTemplateMatcher
public class TemplateVariableNameAndValueFinder : ITemplateVariableNameAndValueFinder
{
public UrlMatch Match(string upstreamUrlPath, string upstreamUrlPathTemplate)
public Response<List<TemplateVariableNameAndValue>> Find(string upstreamUrlPath, string upstreamUrlPathTemplate)
{
if (upstreamUrlPath.Length > upstreamUrlPathTemplate.Length)
{
return new UrlMatch(false, new List<TemplateVariableNameAndValue>(), string.Empty);
}
var urlPathTemplateCopy = upstreamUrlPathTemplate;
var templateKeysAndValues = new List<TemplateVariableNameAndValue>();
int counterForUrl = 0;
for (int counterForTemplate = 0; counterForTemplate < upstreamUrlPathTemplate.Length; counterForTemplate++)
{
if (CharactersDontMatch(upstreamUrlPathTemplate[counterForTemplate], upstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,upstreamUrlPath.Length))
@ -37,15 +30,14 @@ namespace Ocelot.Library.Infrastructure.UrlMatcher
counterForUrl = GetNextCounterPosition(upstreamUrlPath, counterForUrl, '/');
continue;
}
else
{
return new UrlMatch(false, templateKeysAndValues, string.Empty);
}
}
return new OkResponse<List<TemplateVariableNameAndValue>>(templateKeysAndValues);
}
counterForUrl++;
}
return new UrlMatch(true, templateKeysAndValues, urlPathTemplateCopy);
return new OkResponse<List<TemplateVariableNameAndValue>>(templateKeysAndValues);
}
private string GetPlaceholderVariableValue(string urlPath, int counterForUrl)

View File

@ -1,17 +1,11 @@
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.UrlMatcher
{
public class UrlMatch
{
public UrlMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues, string downstreamUrlTemplate)
public UrlMatch(bool match)
{
Match = match;
TemplateVariableNameAndValues = templateVariableNameAndValues;
DownstreamUrlTemplate = downstreamUrlTemplate;
}
public bool Match {get;private set;}
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues {get;private set;}
public string DownstreamUrlTemplate {get;private set;}
}
}

View File

@ -1,11 +1,12 @@
using System.Text;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
{
public class DownstreamUrlTemplateVariableReplacer : IDownstreamUrlTemplateVariableReplacer
{
public string ReplaceTemplateVariables(DownstreamRoute downstreamRoute)
public Response<string> ReplaceTemplateVariables(DownstreamRoute downstreamRoute)
{
var upstreamUrl = new StringBuilder();
@ -16,7 +17,7 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
upstreamUrl.Replace(templateVarAndValue.TemplateVariableName, templateVarAndValue.TemplateVariableValue);
}
return upstreamUrl.ToString();
return new OkResponse<string>(upstreamUrl.ToString());
}
}
}

View File

@ -1,10 +1,10 @@
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
{
public interface IDownstreamUrlTemplateVariableReplacer
{
string ReplaceTemplateVariables(DownstreamRoute downstreamRoute);
Response<string> ReplaceTemplateVariables(DownstreamRoute downstreamRoute);
}
}

View File

@ -2,25 +2,22 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responder;
namespace Ocelot.Library.Middleware
{
public class DownstreamRouteFinderMiddleware
public class DownstreamRouteFinderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
private readonly IHttpResponder _responder;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public DownstreamRouteFinderMiddleware(RequestDelegate next,
IDownstreamRouteFinder downstreamRouteFinder,
IHttpResponder responder,
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_downstreamRouteFinder = downstreamRouteFinder;
_responder = responder;
_scopedRequestDataRepository = scopedRequestDataRepository;
}
@ -32,12 +29,12 @@ namespace Ocelot.Library.Middleware
if (downstreamRoute.IsError)
{
await _responder.CreateNotFoundResponse(context);
SetPipelineError(downstreamRoute.Errors);
return;
}
_scopedRequestDataRepository.Add("DownstreamRoute", downstreamRoute.Data);
await _next.Invoke(context);
}
}

View File

@ -7,22 +7,20 @@ using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
namespace Ocelot.Library.Middleware
{
public class DownstreamUrlCreatorMiddleware
public class DownstreamUrlCreatorMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
private readonly IHttpResponder _responder;
public DownstreamUrlCreatorMiddleware(RequestDelegate next,
IDownstreamUrlTemplateVariableReplacer urlReplacer,
IScopedRequestDataRepository scopedRequestDataRepository,
IHttpResponder responder)
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_urlReplacer = urlReplacer;
_scopedRequestDataRepository = scopedRequestDataRepository;
_responder = responder;
}
public async Task Invoke(HttpContext context)
@ -31,13 +29,19 @@ namespace Ocelot.Library.Middleware
if (downstreamRoute.IsError)
{
await _responder.CreateNotFoundResponse(context);
SetPipelineError(downstreamRoute.Errors);
return;
}
var downstreamUrl = _urlReplacer.ReplaceTemplateVariables(downstreamRoute.Data);
_scopedRequestDataRepository.Add("DownstreamUrl", downstreamUrl);
if (downstreamUrl.IsError)
{
SetPipelineError(downstreamUrl.Errors);
return;
}
_scopedRequestDataRepository.Add("DownstreamUrl", downstreamUrl.Data);
await _next.Invoke(context);
}

View File

@ -1,27 +1,22 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Requester;
using Ocelot.Library.Infrastructure.Responder;
using Ocelot.Library.Infrastructure.RequestBuilder;
namespace Ocelot.Library.Middleware
{
using Infrastructure.RequestBuilder;
public class HttpRequestBuilderMiddleware
public class HttpRequestBuilderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpResponder _responder;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
private readonly IRequestBuilder _requestBuilder;
public HttpRequestBuilderMiddleware(RequestDelegate next,
IHttpResponder responder,
IScopedRequestDataRepository scopedRequestDataRepository,
IRequestBuilder requestBuilder)
:base(scopedRequestDataRepository)
{
_next = next;
_responder = responder;
_scopedRequestDataRepository = scopedRequestDataRepository;
_requestBuilder = requestBuilder;
}
@ -32,7 +27,7 @@ namespace Ocelot.Library.Middleware
if (downstreamUrl.IsError)
{
await _responder.CreateNotFoundResponse(context);
SetPipelineError(downstreamUrl.Errors);
return;
}
@ -40,7 +35,13 @@ namespace Ocelot.Library.Middleware
.Build(context.Request.Method, downstreamUrl.Data, context.Request.Body,
context.Request.Headers, context.Request.Cookies, context.Request.QueryString.Value, context.Request.ContentType);
_scopedRequestDataRepository.Add("Request", request);
if (request.IsError)
{
SetPipelineError(request.Errors);
return;
}
_scopedRequestDataRepository.Add("Request", request.Data);
await _next.Invoke(context);
}

View File

@ -1,29 +1,24 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
using Ocelot.Library.Infrastructure.Requester;
using Ocelot.Library.Infrastructure.Responder;
namespace Ocelot.Library.Middleware
{
using Infrastructure.RequestBuilder;
public class HttpRequesterMiddleware
public class HttpRequesterMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpRequester _requester;
private readonly IHttpResponder _responder;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public HttpRequesterMiddleware(RequestDelegate next,
IHttpRequester requester,
IHttpResponder responder,
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_requester = requester;
_responder = responder;
_scopedRequestDataRepository = scopedRequestDataRepository;
}
@ -33,16 +28,19 @@ namespace Ocelot.Library.Middleware
if (request.IsError)
{
await _responder.CreateNotFoundResponse(context);
SetPipelineError(request.Errors);
return;
}
var response = await _requester
.GetResponse(request.Data);
var response = await _requester.GetResponse(request.Data);
_scopedRequestDataRepository.Add("Response", response.Data);
await _next.Invoke(context);
if (response.IsError)
{
SetPipelineError(response.Errors);
return;
}
_scopedRequestDataRepository.Add("Response", response.Data);
}
}
}

View File

@ -6,10 +6,7 @@ using Ocelot.Library.Infrastructure.Responder;
namespace Ocelot.Library.Middleware
{
/// <summary>
/// Terminating middleware that is responsible for returning a http response to the client
/// </summary>
public class HttpResponderMiddleware
public class HttpResponderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpResponder _responder;
@ -18,6 +15,7 @@ namespace Ocelot.Library.Middleware
public HttpResponderMiddleware(RequestDelegate next,
IHttpResponder responder,
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_responder = responder;
@ -26,9 +24,22 @@ namespace Ocelot.Library.Middleware
public async Task Invoke(HttpContext context)
{
var response = _scopedRequestDataRepository.Get<HttpResponseMessage>("Response");
await _next.Invoke(context);
await _responder.CreateResponse(context, response.Data);
if (PipelineError())
{
//todo obviously this needs to be better...prob look at response errors
// and make a decision i guess
var errors = GetPipelineErrors();
await _responder.CreateNotFoundResponse(context);
}
else
{
var response = _scopedRequestDataRepository.Get<HttpResponseMessage>("Response");
await _responder.CreateResponse(context, response.Data);
}
}
}
}

View File

@ -0,0 +1,34 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Middleware
{
public class OcelotMiddleware
{
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public OcelotMiddleware(IScopedRequestDataRepository scopedRequestDataRepository)
{
_scopedRequestDataRepository = scopedRequestDataRepository;
}
public void SetPipelineError(List<Error> errors)
{
_scopedRequestDataRepository.Add("OcelotMiddlewareError", true);
_scopedRequestDataRepository.Add("OcelotMiddlewareErrors", errors);
}
public bool PipelineError()
{
var response = _scopedRequestDataRepository.Get<bool>("OcelotMiddlewareError");
return response.Data;
}
public List<Error> GetPipelineErrors()
{
var response = _scopedRequestDataRepository.Get<List<Error>>("OcelotMiddlewareErrors");
return response.Data;
}
}
}

View File

@ -17,6 +17,7 @@
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.AspNetCore.Http": "1.0.0",
"System.Text.RegularExpressions": "4.1.0",
"YamlDotNet": "3.9.0"
},

View File

@ -1,10 +1,14 @@
using Microsoft.AspNetCore.Builder;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Ocelot.Library.Infrastructure.Configuration;
using Ocelot.Library.Infrastructure.Configuration.Yaml;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
@ -26,6 +30,7 @@ namespace Ocelot
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddYamlFile("configuration.yaml")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
@ -35,11 +40,13 @@ namespace Ocelot
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<Configuration>(Configuration);
services.Configure<YamlConfiguration>(Configuration);
// Add framework services.
services.AddSingleton<IUrlPathToUrlTemplateMatcher, UrlPathToUrlTemplateMatcher>();
services.AddSingleton<IOcelotConfiguration, OcelotConfiguration>();
services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
services.AddSingleton<ITemplateVariableNameAndValueFinder, TemplateVariableNameAndValueFinder>();
services.AddSingleton<IDownstreamUrlTemplateVariableReplacer, DownstreamUrlTemplateVariableReplacer>();
services.AddSingleton<IDownstreamRouteFinder, DownstreamRouteFinder>();
services.AddSingleton<IHttpRequester, HttpClientHttpRequester>();
@ -58,6 +65,8 @@ namespace Ocelot
loggerFactory.AddDebug();
app.UseHttpResponderMiddleware();
app.UseDownstreamRouteFinderMiddleware();
app.UserDownstreamUrlCreatorMiddleware();
@ -65,8 +74,6 @@ namespace Ocelot
app.UseHttpRequestBuilderMiddleware();
app.UseHttpRequesterMiddleware();
app.UseHttpResponderMiddleware();
}
}
}