mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
hacking yaml config
This commit is contained in:
parent
c4e0bae4ce
commit
e7a41fbc4d
@ -0,0 +1,18 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public class Configuration
|
||||||
|
{
|
||||||
|
public Configuration()
|
||||||
|
{
|
||||||
|
Routes = new List<Route>();
|
||||||
|
}
|
||||||
|
public Configuration(List<Route> routes)
|
||||||
|
{
|
||||||
|
Routes = routes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Route> Routes { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
using System.IO;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
using YamlDotNet.Serialization.NamingConventions;
|
||||||
|
|
||||||
|
public class ConfigurationReader : IConfigurationReader
|
||||||
|
{
|
||||||
|
public Configuration Read(string configurationFilePath)
|
||||||
|
{
|
||||||
|
var contents = File.ReadAllText(configurationFilePath);
|
||||||
|
|
||||||
|
var input = new StringReader(contents);
|
||||||
|
|
||||||
|
var deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention());
|
||||||
|
|
||||||
|
var configuration = deserializer.Deserialize<Configuration>(input);
|
||||||
|
|
||||||
|
return configuration;;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public interface IConfigurationReader
|
||||||
|
{
|
||||||
|
Configuration Read(string configurationFilePath);
|
||||||
|
}
|
||||||
|
}
|
18
src/Ocelot.Library/Infrastructure/Configuration/Route.cs
Normal file
18
src/Ocelot.Library/Infrastructure/Configuration/Route.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public class Route
|
||||||
|
{
|
||||||
|
public Route()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
public Route(string downstream, string upstream)
|
||||||
|
{
|
||||||
|
Downstream = downstream;
|
||||||
|
Upstream = upstream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Downstream { get; private set; }
|
||||||
|
public string Upstream { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
using System.IO;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public static class YamlConfigurationExtensions
|
||||||
|
{
|
||||||
|
public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path)
|
||||||
|
{
|
||||||
|
return AddYamlFile(builder, provider: null, path: path, optional: false, reloadOnChange: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, bool optional)
|
||||||
|
{
|
||||||
|
return AddYamlFile(builder, provider: null, path: path, optional: optional, reloadOnChange: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange)
|
||||||
|
{
|
||||||
|
return AddYamlFile(builder, provider: null, path: path, optional: optional, reloadOnChange: reloadOnChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange)
|
||||||
|
{
|
||||||
|
if (provider == null && Path.IsPathRooted(path))
|
||||||
|
{
|
||||||
|
provider = new PhysicalFileProvider(Path.GetDirectoryName(path));
|
||||||
|
path = Path.GetFileName(path);
|
||||||
|
}
|
||||||
|
var source = new YamlConfigurationSource
|
||||||
|
{
|
||||||
|
FileProvider = provider,
|
||||||
|
Path = path,
|
||||||
|
Optional = optional,
|
||||||
|
ReloadOnChange = reloadOnChange
|
||||||
|
};
|
||||||
|
builder.Add(source);
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,121 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using YamlDotNet.RepresentationModel;
|
||||||
|
|
||||||
|
internal class YamlConfigurationFileParser
|
||||||
|
{
|
||||||
|
private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
private readonly Stack<string> _context = new Stack<string>();
|
||||||
|
private string _currentPath;
|
||||||
|
|
||||||
|
public IDictionary<string, string> Parse(Stream input)
|
||||||
|
{
|
||||||
|
_data.Clear();
|
||||||
|
_context.Clear();
|
||||||
|
|
||||||
|
// https://dotnetfiddle.net/rrR2Bb
|
||||||
|
var yaml = new YamlStream();
|
||||||
|
yaml.Load(new StreamReader(input));
|
||||||
|
|
||||||
|
if (yaml.Documents.Any())
|
||||||
|
{
|
||||||
|
var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
|
||||||
|
|
||||||
|
// The document node is a mapping node
|
||||||
|
VisitYamlMappingNode(mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisitYamlNodePair(KeyValuePair<YamlNode, YamlNode> yamlNodePair)
|
||||||
|
{
|
||||||
|
var context = ((YamlScalarNode)yamlNodePair.Key).Value;
|
||||||
|
VisitYamlNode(context, yamlNodePair.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisitYamlNode(string context, YamlNode node)
|
||||||
|
{
|
||||||
|
if (node is YamlScalarNode)
|
||||||
|
{
|
||||||
|
VisitYamlScalarNode(context, (YamlScalarNode)node);
|
||||||
|
}
|
||||||
|
if (node is YamlMappingNode)
|
||||||
|
{
|
||||||
|
VisitYamlMappingNode(context, (YamlMappingNode)node);
|
||||||
|
}
|
||||||
|
if (node is YamlSequenceNode)
|
||||||
|
{
|
||||||
|
VisitYamlSequenceNode(context, (YamlSequenceNode)node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisitYamlScalarNode(string context, YamlScalarNode yamlValue)
|
||||||
|
{
|
||||||
|
//a node with a single 1-1 mapping
|
||||||
|
EnterContext(context);
|
||||||
|
var currentKey = _currentPath;
|
||||||
|
|
||||||
|
if (_data.ContainsKey(currentKey))
|
||||||
|
{
|
||||||
|
throw new FormatException("Key is duplicate ${currentKey}");
|
||||||
|
}
|
||||||
|
|
||||||
|
_data[currentKey] = yamlValue.Value;
|
||||||
|
ExitContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisitYamlMappingNode(YamlMappingNode node)
|
||||||
|
{
|
||||||
|
foreach (var yamlNodePair in node.Children)
|
||||||
|
{
|
||||||
|
VisitYamlNodePair(yamlNodePair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisitYamlMappingNode(string context, YamlMappingNode yamlValue)
|
||||||
|
{
|
||||||
|
//a node with an associated sub-document
|
||||||
|
EnterContext(context);
|
||||||
|
|
||||||
|
VisitYamlMappingNode(yamlValue);
|
||||||
|
|
||||||
|
ExitContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisitYamlSequenceNode(string context, YamlSequenceNode yamlValue)
|
||||||
|
{
|
||||||
|
//a node with an associated list
|
||||||
|
EnterContext(context);
|
||||||
|
|
||||||
|
VisitYamlSequenceNode(yamlValue);
|
||||||
|
|
||||||
|
ExitContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisitYamlSequenceNode(YamlSequenceNode node)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
VisitYamlNode(i.ToString(), node.Children[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EnterContext(string context)
|
||||||
|
{
|
||||||
|
_context.Push(context);
|
||||||
|
_currentPath = ConfigurationPath.Combine(_context.Reverse());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ExitContext()
|
||||||
|
{
|
||||||
|
_context.Pop();
|
||||||
|
_currentPath = ConfigurationPath.Combine(_context.Reverse());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
using System.IO;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
public class YamlConfigurationProvider : FileConfigurationProvider
|
||||||
|
{
|
||||||
|
public YamlConfigurationProvider(YamlConfigurationSource source) : base(source) { }
|
||||||
|
|
||||||
|
public override void Load(Stream stream)
|
||||||
|
{
|
||||||
|
var parser = new YamlConfigurationFileParser();
|
||||||
|
|
||||||
|
Data = parser.Parse(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
public class YamlConfigurationSource : FileConfigurationSource
|
||||||
|
{
|
||||||
|
public override IConfigurationProvider Build(IConfigurationBuilder builder)
|
||||||
|
{
|
||||||
|
FileProvider = FileProvider ?? builder.GetFileProvider();
|
||||||
|
return new YamlConfigurationProvider(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,8 @@ using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
|
|||||||
namespace Ocelot.Library.Middleware
|
namespace Ocelot.Library.Middleware
|
||||||
{
|
{
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using Infrastructure.Configuration;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
public class ProxyMiddleware
|
public class ProxyMiddleware
|
||||||
{
|
{
|
||||||
@ -14,19 +16,23 @@ namespace Ocelot.Library.Middleware
|
|||||||
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
||||||
private readonly IUrlTemplateMapRepository _urlTemplateMapRepository;
|
private readonly IUrlTemplateMapRepository _urlTemplateMapRepository;
|
||||||
private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
|
private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
|
||||||
|
private readonly IOptions<Configuration> _optionsAccessor;
|
||||||
|
|
||||||
public ProxyMiddleware(RequestDelegate next,
|
public ProxyMiddleware(RequestDelegate next,
|
||||||
IUrlPathToUrlTemplateMatcher urlMatcher,
|
IUrlPathToUrlTemplateMatcher urlMatcher,
|
||||||
IUrlTemplateMapRepository urlPathRepository,
|
IUrlTemplateMapRepository urlPathRepository,
|
||||||
IDownstreamUrlTemplateVariableReplacer urlReplacer)
|
IDownstreamUrlTemplateVariableReplacer urlReplacer, IOptions<Configuration> optionsAccessor)
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_urlMatcher = urlMatcher;
|
_urlMatcher = urlMatcher;
|
||||||
_urlTemplateMapRepository = urlPathRepository;
|
_urlTemplateMapRepository = urlPathRepository;
|
||||||
_urlReplacer = urlReplacer;
|
_urlReplacer = urlReplacer;
|
||||||
|
_optionsAccessor = optionsAccessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
|
|
||||||
var downstreamUrlPath = context.Request.Path.ToString();
|
var downstreamUrlPath = context.Request.Path.ToString();
|
||||||
|
|
||||||
var upstreamUrlTemplates = _urlTemplateMapRepository.All;
|
var upstreamUrlTemplates = _urlTemplateMapRepository.All;
|
||||||
|
@ -16,7 +16,8 @@
|
|||||||
"Microsoft.Extensions.Logging.Console": "1.0.0",
|
"Microsoft.Extensions.Logging.Console": "1.0.0",
|
||||||
"Microsoft.Extensions.Logging.Debug": "1.0.0",
|
"Microsoft.Extensions.Logging.Debug": "1.0.0",
|
||||||
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
|
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
|
||||||
"Microsoft.AspNetCore.Http": "1.0.0"
|
"Microsoft.AspNetCore.Http": "1.0.0",
|
||||||
|
"YamlDotNet": "3.9.0"
|
||||||
},
|
},
|
||||||
|
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
|
@ -8,6 +8,7 @@ using Ocelot.Library.Middleware;
|
|||||||
|
|
||||||
namespace Ocelot
|
namespace Ocelot
|
||||||
{
|
{
|
||||||
|
using Library.Infrastructure.Configuration;
|
||||||
using Library.Infrastructure.UrlMatcher;
|
using Library.Infrastructure.UrlMatcher;
|
||||||
using Library.Infrastructure.UrlTemplateReplacer;
|
using Library.Infrastructure.UrlTemplateReplacer;
|
||||||
using Library.Infrastructure.UrlTemplateRepository;
|
using Library.Infrastructure.UrlTemplateRepository;
|
||||||
@ -20,6 +21,7 @@ namespace Ocelot
|
|||||||
.SetBasePath(env.ContentRootPath)
|
.SetBasePath(env.ContentRootPath)
|
||||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||||
|
.AddYamlFile("configuration.yaml")
|
||||||
.AddEnvironmentVariables();
|
.AddEnvironmentVariables();
|
||||||
Configuration = builder.Build();
|
Configuration = builder.Build();
|
||||||
}
|
}
|
||||||
@ -29,6 +31,10 @@ namespace Ocelot
|
|||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddOptions();
|
||||||
|
|
||||||
|
services.Configure<Configuration>(Configuration);
|
||||||
|
|
||||||
// Add framework services.
|
// Add framework services.
|
||||||
services.AddSingleton<IUrlPathToUrlTemplateMatcher, UrlPathToUrlTemplateMatcher>();
|
services.AddSingleton<IUrlPathToUrlTemplateMatcher, UrlPathToUrlTemplateMatcher>();
|
||||||
services.AddSingleton<IDownstreamUrlTemplateVariableReplacer, DownstreamUrlTemplateVariableReplacer>();
|
services.AddSingleton<IDownstreamUrlTemplateVariableReplacer, DownstreamUrlTemplateVariableReplacer>();
|
||||||
|
54
test/Ocelot.AcceptanceTests/ConfigurationReaderTests.cs
Normal file
54
test/Ocelot.AcceptanceTests/ConfigurationReaderTests.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
namespace Ocelot.AcceptanceTests
|
||||||
|
{
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Library.Infrastructure.Configuration;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class ConfigurationReaderTests
|
||||||
|
{
|
||||||
|
private readonly IConfigurationReader _configurationReader;
|
||||||
|
private string _configPath;
|
||||||
|
private Configuration _result;
|
||||||
|
|
||||||
|
public ConfigurationReaderTests()
|
||||||
|
{
|
||||||
|
_configurationReader = new ConfigurationReader();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void can_read_configuration()
|
||||||
|
{
|
||||||
|
const string path = "./ConfigurationReaderTests.can_read_configuration.yaml";
|
||||||
|
|
||||||
|
var expected =
|
||||||
|
new Configuration(new List<Route>
|
||||||
|
{
|
||||||
|
new Route("productservice/category/{categoryId}/products/{productId}/variants/{variantId}",
|
||||||
|
"https://www.moonpig.com/api/products/{categoryId}/{productId}/{variantId}")
|
||||||
|
});
|
||||||
|
|
||||||
|
this.Given(x => x.GivenAConfigPathOf(path))
|
||||||
|
.When(x => x.WhenICallTheConfigurationReader())
|
||||||
|
.Then(x => x.ThenTheFollowingConfigurationIsReturned(expected))
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenAConfigPathOf(string configPath)
|
||||||
|
{
|
||||||
|
_configPath = configPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenICallTheConfigurationReader()
|
||||||
|
{
|
||||||
|
_result = _configurationReader.Read(_configPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheFollowingConfigurationIsReturned(Configuration expected)
|
||||||
|
{
|
||||||
|
_result.Routes[0].Downstream.ShouldBe(expected.Routes[0].Downstream);
|
||||||
|
_result.Routes[0].Upstream.ShouldBe(expected.Routes[0].Upstream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
test/Ocelot.AcceptanceTests/configuration.yaml
Normal file
3
test/Ocelot.AcceptanceTests/configuration.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
routes:
|
||||||
|
- downstream: "productservice/category/{categoryId}/products/{productId}/variants/{variantId}"
|
||||||
|
upstream: "https://www.moonpig.com/api/products/{categoryId}/{productId}/{variantId}"
|
@ -1,6 +1,14 @@
|
|||||||
{
|
{
|
||||||
"version": "1.0.0-*",
|
"version": "1.0.0-*",
|
||||||
|
|
||||||
|
"buildOptions": {
|
||||||
|
"copyToOutput": {
|
||||||
|
"include": [
|
||||||
|
"configuration.yaml"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
"testRunner": "xunit",
|
"testRunner": "xunit",
|
||||||
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -25,7 +33,8 @@
|
|||||||
"Shouldly": "2.8.0",
|
"Shouldly": "2.8.0",
|
||||||
"Ocelot": "1.0.0-*",
|
"Ocelot": "1.0.0-*",
|
||||||
"Microsoft.AspNetCore.TestHost": "1.0.0",
|
"Microsoft.AspNetCore.TestHost": "1.0.0",
|
||||||
"TestStack.BDDfy": "4.3.1"
|
"TestStack.BDDfy": "4.3.1",
|
||||||
|
"YamlDotNet": "3.9.0"
|
||||||
},
|
},
|
||||||
|
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
|
@ -23,7 +23,8 @@
|
|||||||
"xunit": "2.1.0",
|
"xunit": "2.1.0",
|
||||||
"dotnet-test-xunit": "2.2.0-preview2-build1029",
|
"dotnet-test-xunit": "2.2.0-preview2-build1029",
|
||||||
"Shouldly": "2.8.0",
|
"Shouldly": "2.8.0",
|
||||||
"TestStack.BDDfy": "4.3.1"
|
"TestStack.BDDfy": "4.3.1",
|
||||||
|
"YamlDotNet": "3.9.0"
|
||||||
},
|
},
|
||||||
|
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user