Refactor YAML parsing logic and settings handling across Client and Server projects, improve request building in the client.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace VirtualDDNSRouter.Client.Context;
|
||||
|
||||
[YamlStaticContext]
|
||||
[YamlSerializable(typeof(Settings))]
|
||||
public partial class YamlStaticContextClient : StaticContext
|
||||
{
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
using VirtualDDNSRouter.Client.Context;
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
using Settings = VirtualDDNSRouter.Client.Models.Settings;
|
||||
@@ -16,7 +17,7 @@ public class Helpers
|
||||
var yamlContent = await File.ReadAllTextAsync(YamlFilePath).ConfigureAwait(false);
|
||||
|
||||
// Build the deserializer with explicit naming convention
|
||||
var deserializer = new StaticDeserializerBuilder(new YamlStaticContext())
|
||||
var deserializer = new StaticDeserializerBuilder(new YamlStaticContextClient())
|
||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||
.IgnoreUnmatchedProperties()
|
||||
.Build();
|
||||
@@ -24,16 +25,10 @@ public class Helpers
|
||||
// Deserialize into Settings
|
||||
var settings = deserializer.Deserialize<Settings>(yamlContent);
|
||||
|
||||
if (settings is null || string.IsNullOrWhiteSpace(settings.Host) || string.IsNullOrWhiteSpace(settings.Path) ||
|
||||
string.IsNullOrWhiteSpace(settings.ApiKey))
|
||||
if (settings is null || string.IsNullOrWhiteSpace(settings.host) || string.IsNullOrWhiteSpace(settings.path) ||
|
||||
string.IsNullOrWhiteSpace(settings.apiKey))
|
||||
throw new Exception("Invalid settings file");
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
[YamlStaticContext]
|
||||
[YamlSerializable(typeof(Settings))]
|
||||
private partial class YamlStaticContext : StaticContext
|
||||
{
|
||||
}
|
||||
}
|
@@ -1,10 +1,17 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace VirtualDDNSRouter.Client.Models;
|
||||
|
||||
public class Settings
|
||||
[YamlSerializable]
|
||||
public record Settings
|
||||
{
|
||||
public string Host { get; set; } = string.Empty;
|
||||
public string Path { get; set; } = string.Empty;
|
||||
public ushort DestinationPort { get; set; } = 80;
|
||||
public string ApiKey { get; set; } = string.Empty;
|
||||
public ushort RefreshIntervalMinutes { get; set; } = 5;
|
||||
public string host { get; set; } = string.Empty;
|
||||
public string path { get; set; } = string.Empty;
|
||||
public ushort destinationPort { get; set; } = 80;
|
||||
public string apiKey { get; set; } = string.Empty;
|
||||
public ushort refreshIntervalMinutes { get; set; } = 5;
|
||||
|
||||
public Settings()
|
||||
{
|
||||
}
|
||||
}
|
@@ -10,17 +10,34 @@ Console.CancelKeyPress += (s, e) =>
|
||||
cts.Cancel();
|
||||
};
|
||||
|
||||
var timer = new PeriodicTimer(TimeSpan.FromMinutes(settings.RefreshIntervalMinutes));
|
||||
var timer = new PeriodicTimer(TimeSpan.FromMinutes(settings.refreshIntervalMinutes));
|
||||
|
||||
var client = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
|
||||
var request = new HttpRequestMessage(HttpMethod.Get,
|
||||
new Uri($"{settings.Host}/setip/{settings.Path}/{settings.DestinationPort}/{settings.ApiKey}"));
|
||||
while (await timer.WaitForNextTickAsync())
|
||||
var builder = new UriBuilder
|
||||
{
|
||||
if (cts.IsCancellationRequested) break;
|
||||
var response = await client.SendAsync(request, cts.Token);
|
||||
if (response.IsSuccessStatusCode)
|
||||
Console.WriteLine("[INFO] IP address updated at " + DateTime.Now.ToString("HH:mm:ss"));
|
||||
else
|
||||
Console.WriteLine("[ERROR] IP address update failed at " + DateTime.Now.ToString("HH:mm:ss"));
|
||||
Scheme = "http",
|
||||
Host = settings.host,
|
||||
Path = $"setip/{settings.path}/{settings.destinationPort}/{settings.apiKey}"
|
||||
};
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, builder.Uri);
|
||||
|
||||
Console.WriteLine("[INFO] App started. Scheduling IP address update every " + settings.refreshIntervalMinutes +
|
||||
" minutes.");
|
||||
|
||||
try
|
||||
{
|
||||
while (await timer.WaitForNextTickAsync(cts.Token))
|
||||
{
|
||||
if (cts.IsCancellationRequested) break;
|
||||
var response = await client.SendAsync(request, cts.Token);
|
||||
if (response.IsSuccessStatusCode)
|
||||
Console.WriteLine("[INFO] IP address updated at " + DateTime.Now.ToString("HH:mm:ss"));
|
||||
else
|
||||
Console.WriteLine("[ERROR] IP address update failed at " + DateTime.Now.ToString("HH:mm:ss"));
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Console.WriteLine("[INFO] Shutdown complete.");
|
||||
}
|
@@ -18,6 +18,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="YamlDotNet" Version="16.3.0"/>
|
||||
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.3.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Reference in New Issue
Block a user