44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
|
|
using System.Text.Json;
|
||
|
|
using GhostImageManager.Models;
|
||
|
|
|
||
|
|
namespace GhostImageManager.Services;
|
||
|
|
|
||
|
|
public class GhostConfigService
|
||
|
|
{
|
||
|
|
private readonly string _configPath;
|
||
|
|
private readonly object _lock = new();
|
||
|
|
|
||
|
|
public GhostConfigService(IWebHostEnvironment env)
|
||
|
|
{
|
||
|
|
_configPath = Path.Combine(env.ContentRootPath, "ghost-instances.json");
|
||
|
|
}
|
||
|
|
|
||
|
|
public GhostConfig Load()
|
||
|
|
{
|
||
|
|
if (!File.Exists(_configPath))
|
||
|
|
{
|
||
|
|
var empty = new GhostConfig();
|
||
|
|
Save(empty);
|
||
|
|
return empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
var json = File.ReadAllText(_configPath);
|
||
|
|
return JsonSerializer.Deserialize<GhostConfig>(json, GetOptions()) ?? new GhostConfig();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Save(GhostConfig config)
|
||
|
|
{
|
||
|
|
lock (_lock)
|
||
|
|
{
|
||
|
|
var json = JsonSerializer.Serialize(config, GetOptions());
|
||
|
|
File.WriteAllText(_configPath, json);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static JsonSerializerOptions GetOptions() => new()
|
||
|
|
{
|
||
|
|
WriteIndented = true,
|
||
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||
|
|
};
|
||
|
|
}
|