36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using DTLib.Filesystem;
|
|
using Newtonsoft.Json;
|
|
|
|
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";
|
|
|
|
public void Save(IOPath file_path)
|
|
{
|
|
string serialized = JsonConvert.SerializeObject(this);
|
|
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 JsonConvert.DeserializeObject<ServerConfig>(serialized)
|
|
?? throw new Exception("can't deserialize config");
|
|
}
|
|
|
|
var c = new ServerConfig();
|
|
c.Save(file_path);
|
|
return c;
|
|
}
|
|
} |