using System.Text.Json; using DTLib.Filesystem; namespace Meum.Server; public class ServerConfig { public string listener_ip { get; set; } = "127.0.0.1"; public int listener_port { get; set; } = Constants.ServerPortDefault; public string certificate_path { get; set; } = "self-signed.pem"; public string? key_path { get; set; } = "self-signed.key"; private static JsonSerializerOptions _serializerOptions = new() { WriteIndented = true, }; public void Save(IOPath file_path) { string serialized = JsonSerializer.Serialize(this, _serializerOptions); if (string.IsNullOrEmpty(serialized)) throw new Exception("can't serialize config"); File.WriteAllText(file_path, serialized); } public static ServerConfig LoadOrCreate(IOPath file_path) { if(File.Exists(file_path)) { string serialized = File.ReadAllText(file_path); return JsonSerializer.Deserialize(serialized) ?? throw new Exception("can't deserialize config"); } var c = new ServerConfig(); c.Save(file_path); return c; } }