diff --git a/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Controllers/ConfigurationController.cs b/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Controllers/ConfigurationController.cs index f3d060ce1..d1efd1e04 100644 --- a/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Controllers/ConfigurationController.cs +++ b/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Controllers/ConfigurationController.cs @@ -11,20 +11,20 @@ namespace ApiGw_Base.Controllers public class ConfigurationController : ControllerBase { [HttpGet("mobile/marketing")] - public IActionResult MobileMarketing() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Marketing); + public IActionResult MobileMarketing() => ReadConfigurationFile(Channel.Mobile, ChannelType.Marketing); [HttpGet("mobile/shopping")] - public IActionResult MobileShopping() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Shopping); + public IActionResult MobileShopping() => ReadConfigurationFile(Channel.Mobile, ChannelType.Shopping); [HttpGet("web/marketing")] - public IActionResult WebMarketing() => ReadConfigurationFile(ConfigurationType.Web, ConfigurationBffType.Marketing); + public IActionResult WebMarketing() => ReadConfigurationFile(Channel.Web, ChannelType.Marketing); [HttpGet("web/shopping")] - public IActionResult WebShopping() => ReadConfigurationFile(ConfigurationType.Web, ConfigurationBffType.Shopping); + public IActionResult WebShopping() => ReadConfigurationFile(Channel.Web, ChannelType.Shopping); - private IActionResult ReadConfigurationFile(ConfigurationType configurationType, ConfigurationBffType configurationBffType) + private IActionResult ReadConfigurationFile(Channel channel, ChannelType channelType) { - var path = $"{AppDomain.CurrentDomain.BaseDirectory}/Configurations/configuration.{configurationType}.Bff.{configurationBffType}.json"; + var path = $"{AppDomain.CurrentDomain.BaseDirectory}/Configurations/configuration.{channel}.Bff.{channelType}.json"; using (var streamReader = new StreamReader(path, Encoding.UTF8)) { @@ -32,7 +32,7 @@ namespace ApiGw_Base.Controllers if (string.IsNullOrWhiteSpace(jsonString)) { - return BadRequest($"Configuration file 'configuration.{configurationType}.Bff.{configurationBffType}.json' not found"); + return BadRequest($"Configuration file 'configuration.{channel}.Bff.{channelType}.json' not found"); } return Ok(jsonString); diff --git a/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ConfigurationType.cs b/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/Channel.cs similarity index 67% rename from ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ConfigurationType.cs rename to ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/Channel.cs index 54bb41913..9fe7a915e 100644 --- a/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ConfigurationType.cs +++ b/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/Channel.cs @@ -1,6 +1,6 @@ namespace ApiGw_Base.Enums { - public enum ConfigurationType + public enum Channel { Web, Mobile diff --git a/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ConfigurationBffType.cs b/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ChannelType.cs similarity index 67% rename from ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ConfigurationBffType.cs rename to ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ChannelType.cs index 72de5df8a..6f968027c 100644 --- a/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ConfigurationBffType.cs +++ b/ServiceFabric/Linux/eShopOnServiceFabricApiGateWays/ApiGw_Base/Enums/ChannelType.cs @@ -1,6 +1,6 @@ namespace ApiGw_Base.Enums { - public enum ConfigurationBffType + public enum ChannelType { Marketing, Shopping diff --git a/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Controllers/ConfigurationController.cs b/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Controllers/ConfigurationController.cs index f3d060ce1..d1efd1e04 100644 --- a/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Controllers/ConfigurationController.cs +++ b/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Controllers/ConfigurationController.cs @@ -11,20 +11,20 @@ namespace ApiGw_Base.Controllers public class ConfigurationController : ControllerBase { [HttpGet("mobile/marketing")] - public IActionResult MobileMarketing() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Marketing); + public IActionResult MobileMarketing() => ReadConfigurationFile(Channel.Mobile, ChannelType.Marketing); [HttpGet("mobile/shopping")] - public IActionResult MobileShopping() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Shopping); + public IActionResult MobileShopping() => ReadConfigurationFile(Channel.Mobile, ChannelType.Shopping); [HttpGet("web/marketing")] - public IActionResult WebMarketing() => ReadConfigurationFile(ConfigurationType.Web, ConfigurationBffType.Marketing); + public IActionResult WebMarketing() => ReadConfigurationFile(Channel.Web, ChannelType.Marketing); [HttpGet("web/shopping")] - public IActionResult WebShopping() => ReadConfigurationFile(ConfigurationType.Web, ConfigurationBffType.Shopping); + public IActionResult WebShopping() => ReadConfigurationFile(Channel.Web, ChannelType.Shopping); - private IActionResult ReadConfigurationFile(ConfigurationType configurationType, ConfigurationBffType configurationBffType) + private IActionResult ReadConfigurationFile(Channel channel, ChannelType channelType) { - var path = $"{AppDomain.CurrentDomain.BaseDirectory}/Configurations/configuration.{configurationType}.Bff.{configurationBffType}.json"; + var path = $"{AppDomain.CurrentDomain.BaseDirectory}/Configurations/configuration.{channel}.Bff.{channelType}.json"; using (var streamReader = new StreamReader(path, Encoding.UTF8)) { @@ -32,7 +32,7 @@ namespace ApiGw_Base.Controllers if (string.IsNullOrWhiteSpace(jsonString)) { - return BadRequest($"Configuration file 'configuration.{configurationType}.Bff.{configurationBffType}.json' not found"); + return BadRequest($"Configuration file 'configuration.{channel}.Bff.{channelType}.json' not found"); } return Ok(jsonString); diff --git a/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ConfigurationType.cs b/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/Channel.cs similarity index 67% rename from ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ConfigurationType.cs rename to ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/Channel.cs index 54bb41913..9fe7a915e 100644 --- a/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ConfigurationType.cs +++ b/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/Channel.cs @@ -1,6 +1,6 @@ namespace ApiGw_Base.Enums { - public enum ConfigurationType + public enum Channel { Web, Mobile diff --git a/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ConfigurationBffType.cs b/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ChannelType.cs similarity index 67% rename from ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ConfigurationBffType.cs rename to ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ChannelType.cs index 72de5df8a..6f968027c 100644 --- a/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ConfigurationBffType.cs +++ b/ServiceFabric/Windows/eShopOnServiceFabricApiGateWays/eShopOnServiceFabricApiGW/ApiGw_Base/Enums/ChannelType.cs @@ -1,6 +1,6 @@ namespace ApiGw_Base.Enums { - public enum ConfigurationBffType + public enum ChannelType { Marketing, Shopping diff --git a/src/ApiGateways/ApiGw-Base/Models/ServiceFabricSettings.cs b/src/ApiGateways/ApiGw-Base/Models/ServiceFabricSettings.cs new file mode 100644 index 000000000..898b88676 --- /dev/null +++ b/src/ApiGateways/ApiGw-Base/Models/ServiceFabricSettings.cs @@ -0,0 +1,13 @@ +namespace OcelotApiGw.Models +{ + public class ServiceFabricSettings + { + public string UrlMobilMarketing { get; set; } + + public string UrlMobilShopping { get; set; } + + public string UrlWebMarketing { get; set; } + + public string UrlWebShopping { get; set; } + } +} diff --git a/src/ApiGateways/ApiGw-Base/Orchestrators/Kubernetes.cs b/src/ApiGateways/ApiGw-Base/Orchestrators/Kubernetes.cs index 7b3b2d2a6..1e2a70d69 100644 --- a/src/ApiGateways/ApiGw-Base/Orchestrators/Kubernetes.cs +++ b/src/ApiGateways/ApiGw-Base/Orchestrators/Kubernetes.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Configuration; using OcelotApiGw.Enums; -using System.IO; namespace OcelotApiGw.Orchestrators { @@ -8,11 +7,6 @@ namespace OcelotApiGw.Orchestrators { public OrchestratorType OrchestratorType => OrchestratorType.K8S; - public IConfigurationBuilder ConfigureOrchestrator(IConfigurationBuilder builder) - { - builder.AddJsonFile(Path.Combine("configuration", "configuration.json")); - - return builder; - } + public IConfigurationBuilder ConfigureOrchestrator(IConfigurationBuilder builder) => builder; } } diff --git a/src/ApiGateways/ApiGw-Base/Orchestrators/ServiceFabric.cs b/src/ApiGateways/ApiGw-Base/Orchestrators/ServiceFabric.cs index a3cff1046..4c8b3db3c 100644 --- a/src/ApiGateways/ApiGw-Base/Orchestrators/ServiceFabric.cs +++ b/src/ApiGateways/ApiGw-Base/Orchestrators/ServiceFabric.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Configuration; using OcelotApiGw.Enums; +using OcelotApiGw.Providers; using OcelotApiGw.Services; namespace OcelotApiGw.Orchestrators @@ -19,6 +20,10 @@ namespace OcelotApiGw.Orchestrators { var config = _settingService.GetConfiguration(); + var memoryFileProvider = new InMemoryFileProvider(config); + + builder.AddJsonFile(memoryFileProvider, string.Empty, false, false); + return builder; } } diff --git a/src/ApiGateways/ApiGw-Base/Providers/InMemoryFile.cs b/src/ApiGateways/ApiGw-Base/Providers/InMemoryFile.cs new file mode 100644 index 000000000..46e8fe676 --- /dev/null +++ b/src/ApiGateways/ApiGw-Base/Providers/InMemoryFile.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.FileProviders; +using System; +using System.IO; +using System.Text; + +namespace OcelotApiGw.Providers +{ + public class InMemoryFile : IFileInfo + { + private readonly byte[] _data; + + public InMemoryFile(string data) => _data = Encoding.UTF8.GetBytes(data); + + public Stream CreateReadStream() => new MemoryStream(_data); + + public bool Exists { get; } = true; + + public long Length => _data.Length; + + public string PhysicalPath { get; } = string.Empty; + + public string Name { get; } = string.Empty; + + public DateTimeOffset LastModified { get; } = DateTimeOffset.UtcNow; + + public bool IsDirectory { get; } = false; + } +} diff --git a/src/ApiGateways/ApiGw-Base/Providers/InMemoryFileProvider.cs b/src/ApiGateways/ApiGw-Base/Providers/InMemoryFileProvider.cs new file mode 100644 index 000000000..357e96ac6 --- /dev/null +++ b/src/ApiGateways/ApiGw-Base/Providers/InMemoryFileProvider.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Primitives; + +namespace OcelotApiGw.Providers +{ + public class InMemoryFileProvider : IFileProvider + { + private readonly IFileInfo _fileInfo; + + public InMemoryFileProvider(string data) => _fileInfo = new InMemoryFile(data); + + public IFileInfo GetFileInfo(string subpath) => _fileInfo; + + public IDirectoryContents GetDirectoryContents(string subpath) => null; + + public IChangeToken Watch(string filter) => NullChangeToken.Singleton; + } +} diff --git a/src/ApiGateways/ApiGw-Base/Services/SettingService.cs b/src/ApiGateways/ApiGw-Base/Services/SettingService.cs index 52965adef..88eb89349 100644 --- a/src/ApiGateways/ApiGw-Base/Services/SettingService.cs +++ b/src/ApiGateways/ApiGw-Base/Services/SettingService.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Options; +using OcelotApiGw.Models; using System; using System.Collections.Generic; using System.Linq; @@ -10,12 +11,12 @@ namespace OcelotApiGw.Services public class SettingService : ISettingService { private readonly HttpClient _httpClient; - private readonly IConfiguration _configuration; + private readonly ServiceFabricSettings _serviceFabricSettings; - public SettingService(HttpClient httpClient, IConfiguration configuration) + public SettingService(HttpClient httpClient, IOptions serviceFabricSettings) { _httpClient = httpClient; - _configuration = configuration; + _serviceFabricSettings = serviceFabricSettings.Value; } public string GetConfiguration() @@ -25,20 +26,20 @@ namespace OcelotApiGw.Services var tasks = new Func[] { async () => { - var res = await _httpClient.GetStringAsync(_configuration.GetValue("MobilMarketing")); - strings.Add(res); + var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlMobilMarketing); + strings.Add(configuration); }, async () => { - var res = await _httpClient.GetStringAsync(_configuration.GetValue("MobilShopping")); - strings.Add(res); + var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlMobilShopping); + strings.Add(configuration); }, async () => { - var res = await _httpClient.GetStringAsync(_configuration.GetValue("WebMarketing")); - strings.Add(res); + var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlWebMarketing); + strings.Add(configuration); }, async () => { - var res = await _httpClient.GetStringAsync(_configuration.GetValue("WebShopping")); - strings.Add(res); + var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlWebShopping); + strings.Add(configuration); } }; diff --git a/src/ApiGateways/ApiGw-Base/Startup.cs b/src/ApiGateways/ApiGw-Base/Startup.cs index f3125d89d..5e700817c 100644 --- a/src/ApiGateways/ApiGw-Base/Startup.cs +++ b/src/ApiGateways/ApiGw-Base/Startup.cs @@ -2,11 +2,14 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using Ocelot.DependencyInjection; using Ocelot.Middleware; using OcelotApiGw.Enums; +using OcelotApiGw.Models; using OcelotApiGw.Orchestrators; +using OcelotApiGw.Providers; using OcelotApiGw.Services; using System.Linq; @@ -36,6 +39,7 @@ namespace OcelotApiGw }) .AddTransient() .AddTransient() + .AddTransient() .AddAuthentication() .AddJwtBearer(authenticationProviderKey, options => { @@ -47,6 +51,7 @@ namespace OcelotApiGw }; }); + services.Configure(_configuration); services.AddHttpClient(); var orchestratorType = _configuration.GetValue("OrchestratorType");