Meum/Meum.Server/ServerConfig.cs
2025-05-09 16:32:35 +05:00

39 lines
1.2 KiB
C#

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<ServerConfig>(serialized)
?? throw new Exception("can't deserialize config");
}
var c = new ServerConfig();
c.Save(file_path);
return c;
}
}