mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	all tests passing, now to do authentication config provider
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
namespace Ocelot.Configuration.File
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.File
 | 
			
		||||
{
 | 
			
		||||
    public class FileGlobalConfiguration
 | 
			
		||||
    {
 | 
			
		||||
@@ -6,6 +7,7 @@
 | 
			
		||||
        {
 | 
			
		||||
            ServiceDiscoveryProvider = new FileServiceDiscoveryProvider();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string RequestIdKey { get; set; }
 | 
			
		||||
        public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;}
 | 
			
		||||
        public string AdministrationPath {get;set;}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ namespace Ocelot.Configuration.File
 | 
			
		||||
{
 | 
			
		||||
    public class FileServiceDiscoveryProvider
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        public string Provider {get;set;}
 | 
			
		||||
        public string Host {get;set;}
 | 
			
		||||
        public int Port { get; set; }
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,25 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using Newtonsoft.Json;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Configuration.Repository;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Provider
 | 
			
		||||
{
 | 
			
		||||
    public class FileConfigurationProvider : IFileConfigurationProvider
 | 
			
		||||
    {
 | 
			
		||||
        private IFileConfigurationRepository _repo;
 | 
			
		||||
 | 
			
		||||
        public FileConfigurationProvider(IFileConfigurationRepository repo)
 | 
			
		||||
        {
 | 
			
		||||
            _repo = repo;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public Response<FileConfiguration> Get()
 | 
			
		||||
        {
 | 
			
		||||
            var fileConfig = _repo.Get();
 | 
			
		||||
            return new OkResponse<FileConfiguration>(fileConfig.Data);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Services
 | 
			
		||||
namespace Ocelot.Configuration.Provider
 | 
			
		||||
{
 | 
			
		||||
    public interface IFileConfigurationProvider
 | 
			
		||||
    {
 | 
			
		||||
@@ -0,0 +1,42 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Newtonsoft.Json;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Repository
 | 
			
		||||
{
 | 
			
		||||
    public class FileConfigurationRepository : IFileConfigurationRepository
 | 
			
		||||
    {
 | 
			
		||||
        private static readonly object _lock = new object();
 | 
			
		||||
        public Response<FileConfiguration> Get()
 | 
			
		||||
        {
 | 
			
		||||
            var configFilePath = $"{AppContext.BaseDirectory}/configuration.json";
 | 
			
		||||
            string json = string.Empty;
 | 
			
		||||
            lock(_lock)
 | 
			
		||||
            {
 | 
			
		||||
                json = System.IO.File.ReadAllText(configFilePath);
 | 
			
		||||
            }
 | 
			
		||||
            var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(json);
 | 
			
		||||
            return new OkResponse<FileConfiguration>(fileConfiguration);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Response Set(FileConfiguration fileConfiguration)
 | 
			
		||||
        {
 | 
			
		||||
            var configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
 | 
			
		||||
 | 
			
		||||
            var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
 | 
			
		||||
 | 
			
		||||
            lock(_lock)
 | 
			
		||||
            {
 | 
			
		||||
                if (System.IO.File.Exists(configurationPath))
 | 
			
		||||
                {
 | 
			
		||||
                    System.IO.File.Delete(configurationPath);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                System.IO.File.WriteAllText(configurationPath, jsonConfiguration);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            return new OkResponse();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,11 @@
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration.Repository
 | 
			
		||||
{
 | 
			
		||||
    public interface IFileConfigurationRepository
 | 
			
		||||
    {
 | 
			
		||||
        Response<FileConfiguration> Get();
 | 
			
		||||
        Response Set(FileConfiguration fileConfiguration);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -10,15 +10,25 @@ namespace Ocelot.Configuration.Setter
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IOcelotConfigurationRepository _configRepo;
 | 
			
		||||
        private readonly IOcelotConfigurationCreator _configCreator;
 | 
			
		||||
        private readonly IFileConfigurationRepository _repo;
 | 
			
		||||
 | 
			
		||||
        public  FileConfigurationSetter(IOcelotConfigurationRepository configRepo, IOcelotConfigurationCreator configCreator)
 | 
			
		||||
        public  FileConfigurationSetter(IOcelotConfigurationRepository configRepo, 
 | 
			
		||||
            IOcelotConfigurationCreator configCreator, IFileConfigurationRepository repo)
 | 
			
		||||
        {
 | 
			
		||||
            _configRepo = configRepo;
 | 
			
		||||
            _configCreator = configCreator;
 | 
			
		||||
            _repo = repo;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Response> Set(FileConfiguration fileConfig)
 | 
			
		||||
        {
 | 
			
		||||
            var response = _repo.Set(fileConfig);
 | 
			
		||||
 | 
			
		||||
            if(response.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                return new ErrorResponse(response.Errors);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var config = await _configCreator.Create(fileConfig);
 | 
			
		||||
 | 
			
		||||
            if(!config.IsError)
 | 
			
		||||
 
 | 
			
		||||
@@ -2,8 +2,8 @@ using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Authorization;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Configuration.Provider;
 | 
			
		||||
using Ocelot.Configuration.Setter;
 | 
			
		||||
using Ocelot.Services;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Controllers
 | 
			
		||||
{
 | 
			
		||||
@@ -34,7 +34,7 @@ namespace Ocelot.Controllers
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        public async Task<IActionResult> Post(FileConfiguration fileConfiguration)
 | 
			
		||||
        public async Task<IActionResult> Post([FromBody]FileConfiguration fileConfiguration)
 | 
			
		||||
        {
 | 
			
		||||
            var response = await _configSetter.Set(fileConfiguration);
 | 
			
		||||
              
 | 
			
		||||
 
 | 
			
		||||
@@ -34,7 +34,6 @@ using Ocelot.Requester;
 | 
			
		||||
using Ocelot.Requester.QoS;
 | 
			
		||||
using Ocelot.Responder;
 | 
			
		||||
using Ocelot.ServiceDiscovery;
 | 
			
		||||
using Ocelot.Services;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.DependencyInjection
 | 
			
		||||
{
 | 
			
		||||
@@ -112,8 +111,9 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
                .AddAuthorization()
 | 
			
		||||
                .AddJsonFormatters();
 | 
			
		||||
            services.AddLogging();
 | 
			
		||||
            services.AddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
 | 
			
		||||
            services.AddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();
 | 
			
		||||
            services.AddSingleton<IFileConfigurationProvider, Services.FileConfigurationProvider>();
 | 
			
		||||
            services.AddSingleton<Configuration.Provider.IFileConfigurationProvider, Configuration.Provider.FileConfigurationProvider>();
 | 
			
		||||
            services.AddSingleton<IQosProviderHouse, QosProviderHouse>();
 | 
			
		||||
            services.AddSingleton<IQoSProviderFactory, QoSProviderFactory>();
 | 
			
		||||
            services.AddSingleton<IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();
 | 
			
		||||
 
 | 
			
		||||
@@ -1,19 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using Newtonsoft.Json;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Services
 | 
			
		||||
{
 | 
			
		||||
    public class FileConfigurationProvider : IFileConfigurationProvider
 | 
			
		||||
    {
 | 
			
		||||
        public Response<FileConfiguration> Get()
 | 
			
		||||
        {
 | 
			
		||||
            var configFilePath = $"{AppContext.BaseDirectory}/configuration.json";
 | 
			
		||||
            var json = File.ReadAllText(configFilePath);
 | 
			
		||||
            var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(json);
 | 
			
		||||
            return new OkResponse<FileConfiguration>(fileConfiguration);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user