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

@ -0,0 +1,24 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class ConfigurationValidationResult
{
public ConfigurationValidationResult(bool isError)
{
IsError = isError;
Errors = new List<Error>();
}
public ConfigurationValidationResult(bool isError, List<Error> errors)
{
IsError = isError;
Errors = errors;
}
public bool IsError { get; private set; }
public List<Error> Errors { get; private set; }
}
}

View File

@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class ConfigurationValidator : IConfigurationValidator
{
public Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration)
{
var duplicateUpstreamTemplates = configuration.ReRoutes
.Select(r => r.DownstreamTemplate)
.GroupBy(r => r)
.Where(r => r.Count() > 1)
.Select(r => r.Key)
.ToList();
if (duplicateUpstreamTemplates.Count <= 0)
{
return new OkResponse<ConfigurationValidationResult>(new ConfigurationValidationResult(false));
}
var errors = new List<Error>();
foreach (var duplicateUpstreamTemplate in duplicateUpstreamTemplates)
{
var error = new DownstreamTemplateAlreadyUsedError(string.Format("Duplicate DownstreamTemplate: {0}",
duplicateUpstreamTemplate));
errors.Add(error);
}
return new OkResponse<ConfigurationValidationResult>(new ConfigurationValidationResult(true, errors));
}
}
}

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