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