mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 07:18:16 +08:00
Initial draft for the issue 147 fix (#155)
* Initial draft for the issue 147 fix * Added unit tests for scenarios for getting and setting file configuration if the environment name is unavailable.
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Responses;
|
||||
@ -8,33 +9,41 @@ namespace Ocelot.Configuration.Repository
|
||||
{
|
||||
public class FileConfigurationRepository : IFileConfigurationRepository
|
||||
{
|
||||
private readonly string _configFilePath;
|
||||
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
public FileConfigurationRepository(IHostingEnvironment hostingEnvironment)
|
||||
{
|
||||
_configFilePath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
|
||||
}
|
||||
|
||||
public async Task<Response<FileConfiguration>> Get()
|
||||
{
|
||||
var configFilePath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
string json = string.Empty;
|
||||
string jsonConfiguration;
|
||||
|
||||
lock(_lock)
|
||||
{
|
||||
json = System.IO.File.ReadAllText(configFilePath);
|
||||
jsonConfiguration = System.IO.File.ReadAllText(_configFilePath);
|
||||
}
|
||||
var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(json);
|
||||
|
||||
var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(jsonConfiguration);
|
||||
|
||||
return new OkResponse<FileConfiguration>(fileConfiguration);
|
||||
}
|
||||
|
||||
public async Task<Response> Set(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
string jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
lock(_lock)
|
||||
{
|
||||
if (System.IO.File.Exists(configurationPath))
|
||||
if (System.IO.File.Exists(_configFilePath))
|
||||
{
|
||||
System.IO.File.Delete(configurationPath);
|
||||
System.IO.File.Delete(_configFilePath);
|
||||
}
|
||||
|
||||
System.IO.File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
System.IO.File.WriteAllText(_configFilePath, jsonConfiguration);
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
|
Reference in New Issue
Block a user