feat(config): add FileConfig model and JSON loader
This commit is contained in:
41
src/ClaudeMailbox/Config/FileConfig.cs
Normal file
41
src/ClaudeMailbox/Config/FileConfig.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ClaudeMailbox.Config;
|
||||
|
||||
public sealed class FileConfig
|
||||
{
|
||||
[JsonPropertyName("port")]
|
||||
public int? Port { get; set; }
|
||||
|
||||
[JsonPropertyName("bind")]
|
||||
public string? Bind { get; set; }
|
||||
|
||||
[JsonPropertyName("dbPath")]
|
||||
public string? DbPath { get; set; }
|
||||
|
||||
private static readonly JsonSerializerOptions Options = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
};
|
||||
|
||||
public static FileConfig Load(string? explicitPath, string? defaultPath)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(explicitPath))
|
||||
{
|
||||
if (!File.Exists(explicitPath))
|
||||
throw new FileNotFoundException($"Config file not found: {explicitPath}", explicitPath);
|
||||
return Parse(File.ReadAllText(explicitPath));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(defaultPath) && File.Exists(defaultPath))
|
||||
return Parse(File.ReadAllText(defaultPath));
|
||||
|
||||
return new FileConfig();
|
||||
}
|
||||
|
||||
private static FileConfig Parse(string json)
|
||||
{
|
||||
return JsonSerializer.Deserialize<FileConfig>(json, Options) ?? new FileConfig();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user