mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-08-01 02:35:58 +08:00
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:
@ -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; }
|
||||
}
|
||||
}
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
{
|
||||
public interface IOcelotConfiguration
|
||||
{
|
||||
List<ReRoute> ReRoutes { get; }
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
@ -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)
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
{
|
||||
public interface IConfigurationValidator
|
||||
{
|
||||
Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration);
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user