api-gateway add provider for file in memory
This commit is contained in:
parent
68f6c4988a
commit
69f0b4393d
@ -11,20 +11,20 @@ namespace ApiGw_Base.Controllers
|
|||||||
public class ConfigurationController : ControllerBase
|
public class ConfigurationController : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet("mobile/marketing")]
|
[HttpGet("mobile/marketing")]
|
||||||
public IActionResult MobileMarketing() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Marketing);
|
public IActionResult MobileMarketing() => ReadConfigurationFile(Channel.Mobile, ChannelType.Marketing);
|
||||||
|
|
||||||
[HttpGet("mobile/shopping")]
|
[HttpGet("mobile/shopping")]
|
||||||
public IActionResult MobileShopping() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Shopping);
|
public IActionResult MobileShopping() => ReadConfigurationFile(Channel.Mobile, ChannelType.Shopping);
|
||||||
|
|
||||||
[HttpGet("web/marketing")]
|
[HttpGet("web/marketing")]
|
||||||
public IActionResult WebMarketing() => ReadConfigurationFile(ConfigurationType.Web, ConfigurationBffType.Marketing);
|
public IActionResult WebMarketing() => ReadConfigurationFile(Channel.Web, ChannelType.Marketing);
|
||||||
|
|
||||||
[HttpGet("web/shopping")]
|
[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))
|
using (var streamReader = new StreamReader(path, Encoding.UTF8))
|
||||||
{
|
{
|
||||||
@ -32,7 +32,7 @@ namespace ApiGw_Base.Controllers
|
|||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(jsonString))
|
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);
|
return Ok(jsonString);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
namespace ApiGw_Base.Enums
|
namespace ApiGw_Base.Enums
|
||||||
{
|
{
|
||||||
public enum ConfigurationType
|
public enum Channel
|
||||||
{
|
{
|
||||||
Web,
|
Web,
|
||||||
Mobile
|
Mobile
|
@ -1,6 +1,6 @@
|
|||||||
namespace ApiGw_Base.Enums
|
namespace ApiGw_Base.Enums
|
||||||
{
|
{
|
||||||
public enum ConfigurationBffType
|
public enum ChannelType
|
||||||
{
|
{
|
||||||
Marketing,
|
Marketing,
|
||||||
Shopping
|
Shopping
|
@ -11,20 +11,20 @@ namespace ApiGw_Base.Controllers
|
|||||||
public class ConfigurationController : ControllerBase
|
public class ConfigurationController : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet("mobile/marketing")]
|
[HttpGet("mobile/marketing")]
|
||||||
public IActionResult MobileMarketing() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Marketing);
|
public IActionResult MobileMarketing() => ReadConfigurationFile(Channel.Mobile, ChannelType.Marketing);
|
||||||
|
|
||||||
[HttpGet("mobile/shopping")]
|
[HttpGet("mobile/shopping")]
|
||||||
public IActionResult MobileShopping() => ReadConfigurationFile(ConfigurationType.Mobile, ConfigurationBffType.Shopping);
|
public IActionResult MobileShopping() => ReadConfigurationFile(Channel.Mobile, ChannelType.Shopping);
|
||||||
|
|
||||||
[HttpGet("web/marketing")]
|
[HttpGet("web/marketing")]
|
||||||
public IActionResult WebMarketing() => ReadConfigurationFile(ConfigurationType.Web, ConfigurationBffType.Marketing);
|
public IActionResult WebMarketing() => ReadConfigurationFile(Channel.Web, ChannelType.Marketing);
|
||||||
|
|
||||||
[HttpGet("web/shopping")]
|
[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))
|
using (var streamReader = new StreamReader(path, Encoding.UTF8))
|
||||||
{
|
{
|
||||||
@ -32,7 +32,7 @@ namespace ApiGw_Base.Controllers
|
|||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(jsonString))
|
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);
|
return Ok(jsonString);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
namespace ApiGw_Base.Enums
|
namespace ApiGw_Base.Enums
|
||||||
{
|
{
|
||||||
public enum ConfigurationType
|
public enum Channel
|
||||||
{
|
{
|
||||||
Web,
|
Web,
|
||||||
Mobile
|
Mobile
|
@ -1,6 +1,6 @@
|
|||||||
namespace ApiGw_Base.Enums
|
namespace ApiGw_Base.Enums
|
||||||
{
|
{
|
||||||
public enum ConfigurationBffType
|
public enum ChannelType
|
||||||
{
|
{
|
||||||
Marketing,
|
Marketing,
|
||||||
Shopping
|
Shopping
|
13
src/ApiGateways/ApiGw-Base/Models/ServiceFabricSettings.cs
Normal file
13
src/ApiGateways/ApiGw-Base/Models/ServiceFabricSettings.cs
Normal file
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using OcelotApiGw.Enums;
|
using OcelotApiGw.Enums;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace OcelotApiGw.Orchestrators
|
namespace OcelotApiGw.Orchestrators
|
||||||
{
|
{
|
||||||
@ -8,11 +7,6 @@ namespace OcelotApiGw.Orchestrators
|
|||||||
{
|
{
|
||||||
public OrchestratorType OrchestratorType => OrchestratorType.K8S;
|
public OrchestratorType OrchestratorType => OrchestratorType.K8S;
|
||||||
|
|
||||||
public IConfigurationBuilder ConfigureOrchestrator(IConfigurationBuilder builder)
|
public IConfigurationBuilder ConfigureOrchestrator(IConfigurationBuilder builder) => builder;
|
||||||
{
|
|
||||||
builder.AddJsonFile(Path.Combine("configuration", "configuration.json"));
|
|
||||||
|
|
||||||
return builder;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using OcelotApiGw.Enums;
|
using OcelotApiGw.Enums;
|
||||||
|
using OcelotApiGw.Providers;
|
||||||
using OcelotApiGw.Services;
|
using OcelotApiGw.Services;
|
||||||
|
|
||||||
namespace OcelotApiGw.Orchestrators
|
namespace OcelotApiGw.Orchestrators
|
||||||
@ -19,6 +20,10 @@ namespace OcelotApiGw.Orchestrators
|
|||||||
{
|
{
|
||||||
var config = _settingService.GetConfiguration();
|
var config = _settingService.GetConfiguration();
|
||||||
|
|
||||||
|
var memoryFileProvider = new InMemoryFileProvider(config);
|
||||||
|
|
||||||
|
builder.AddJsonFile(memoryFileProvider, string.Empty, false, false);
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
src/ApiGateways/ApiGw-Base/Providers/InMemoryFile.cs
Normal file
28
src/ApiGateways/ApiGw-Base/Providers/InMemoryFile.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
18
src/ApiGateways/ApiGw-Base/Providers/InMemoryFileProvider.cs
Normal file
18
src/ApiGateways/ApiGw-Base/Providers/InMemoryFileProvider.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Options;
|
||||||
|
using OcelotApiGw.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -10,12 +11,12 @@ namespace OcelotApiGw.Services
|
|||||||
public class SettingService : ISettingService
|
public class SettingService : ISettingService
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly ServiceFabricSettings _serviceFabricSettings;
|
||||||
|
|
||||||
public SettingService(HttpClient httpClient, IConfiguration configuration)
|
public SettingService(HttpClient httpClient, IOptions<ServiceFabricSettings> serviceFabricSettings)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
_configuration = configuration;
|
_serviceFabricSettings = serviceFabricSettings.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetConfiguration()
|
public string GetConfiguration()
|
||||||
@ -25,20 +26,20 @@ namespace OcelotApiGw.Services
|
|||||||
var tasks = new Func<Task>[]
|
var tasks = new Func<Task>[]
|
||||||
{
|
{
|
||||||
async () => {
|
async () => {
|
||||||
var res = await _httpClient.GetStringAsync(_configuration.GetValue<string>("MobilMarketing"));
|
var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlMobilMarketing);
|
||||||
strings.Add(res);
|
strings.Add(configuration);
|
||||||
},
|
},
|
||||||
async () => {
|
async () => {
|
||||||
var res = await _httpClient.GetStringAsync(_configuration.GetValue<string>("MobilShopping"));
|
var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlMobilShopping);
|
||||||
strings.Add(res);
|
strings.Add(configuration);
|
||||||
},
|
},
|
||||||
async () => {
|
async () => {
|
||||||
var res = await _httpClient.GetStringAsync(_configuration.GetValue<string>("WebMarketing"));
|
var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlWebMarketing);
|
||||||
strings.Add(res);
|
strings.Add(configuration);
|
||||||
},
|
},
|
||||||
async () => {
|
async () => {
|
||||||
var res = await _httpClient.GetStringAsync(_configuration.GetValue<string>("WebShopping"));
|
var configuration = await _httpClient.GetStringAsync(_serviceFabricSettings.UrlWebShopping);
|
||||||
strings.Add(res);
|
strings.Add(configuration);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Ocelot.DependencyInjection;
|
using Ocelot.DependencyInjection;
|
||||||
using Ocelot.Middleware;
|
using Ocelot.Middleware;
|
||||||
using OcelotApiGw.Enums;
|
using OcelotApiGw.Enums;
|
||||||
|
using OcelotApiGw.Models;
|
||||||
using OcelotApiGw.Orchestrators;
|
using OcelotApiGw.Orchestrators;
|
||||||
|
using OcelotApiGw.Providers;
|
||||||
using OcelotApiGw.Services;
|
using OcelotApiGw.Services;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
@ -36,6 +39,7 @@ namespace OcelotApiGw
|
|||||||
})
|
})
|
||||||
.AddTransient<IOrchestratorStrategy, Kubernetes>()
|
.AddTransient<IOrchestratorStrategy, Kubernetes>()
|
||||||
.AddTransient<IOrchestratorStrategy, ServiceFabric>()
|
.AddTransient<IOrchestratorStrategy, ServiceFabric>()
|
||||||
|
.AddTransient<IFileProvider, InMemoryFileProvider>()
|
||||||
.AddAuthentication()
|
.AddAuthentication()
|
||||||
.AddJwtBearer(authenticationProviderKey, options =>
|
.AddJwtBearer(authenticationProviderKey, options =>
|
||||||
{
|
{
|
||||||
@ -47,6 +51,7 @@ namespace OcelotApiGw
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
services.Configure<ServiceFabricSettings>(_configuration);
|
||||||
services.AddHttpClient<ISettingService, SettingService>();
|
services.AddHttpClient<ISettingService, SettingService>();
|
||||||
|
|
||||||
var orchestratorType = _configuration.GetValue<OrchestratorType>("OrchestratorType");
|
var orchestratorType = _configuration.GetValue<OrchestratorType>("OrchestratorType");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user