Skip to content

Commit 208005f

Browse files
committed
Validate options on start
1 parent 35a4eee commit 208005f

File tree

8 files changed

+59
-16
lines changed

8 files changed

+59
-16
lines changed

src/Core/Handlers/UpdateHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static class UpdateHandler
99
{
1010
public static async Task<IResult> Handle(
1111
CloudflareClient cloudflareClient,
12-
IOptions<List<RecordOptions>> recordOptions,
12+
IOptions<DdnsOptions> ddnsOptions,
1313
[FromQuery] string key,
1414
[FromQuery] string ipv4
1515
)
1616
{
17-
var record = recordOptions.Value.Single(r => r.Key == key);
17+
var record = ddnsOptions.Value.Records.Single(r => r.Key == key);
1818

1919
await cloudflareClient.UpdateDnsRecord(record.ZoneId, record.DnsRecordId, ipv4);
2020

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
using System.ComponentModel.DataAnnotations;
2+
13
namespace Core.Options;
24

35
public record CloudflareOptions
46
{
57
public const string Cloudflare = "Cloudflare";
68

7-
public required string ApiToken { get; set; }
9+
[Required]
10+
public string ApiToken { get; set; } = null!;
811
}

src/Core/Options/DdnsOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Microsoft.Extensions.Options;
3+
4+
namespace Core.Options;
5+
6+
public record DdnsOptions
7+
{
8+
public const string Ddns = "DDNS";
9+
10+
[Required]
11+
[ValidateEnumeratedItems]
12+
public IList<DdnsRecord> Records { get; set; } = null!;
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
using System.ComponentModel.DataAnnotations;
2+
13
namespace Core.Options;
24

3-
public record RecordOptions
5+
public record DdnsRecord
46
{
5-
public const string Records = "Records";
6-
7+
[Required]
78
public string Key { get; set; } = null!;
89

10+
[Required]
911
public string ZoneId { get; set; } = null!;
1012

13+
[Required]
1114
public string DnsRecordId { get; set; } = null!;
1215
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Core.Options;
2+
using Microsoft.Extensions.Options;
3+
4+
namespace Core.OptionsValidators;
5+
6+
[OptionsValidator]
7+
public partial class CloudflareOptionsValidator : IValidateOptions<CloudflareOptions>
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Core.Options;
2+
using Microsoft.Extensions.Options;
3+
4+
namespace Core.OptionsValidators;
5+
6+
[OptionsValidator]
7+
public partial class DdnsOptionsValidator : IValidateOptions<DdnsOptions>
8+
{
9+
}

src/Core/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Core.Handlers;
33
using Core.MessageHandlers;
44
using Core.Options;
5+
using Core.OptionsValidators;
6+
using Microsoft.Extensions.Options;
57

68
namespace Core;
79

@@ -11,11 +13,13 @@ public static void Main(string[] args)
1113
{
1214
var builder = WebApplication.CreateSlimBuilder(args);
1315

14-
builder.Services.AddOptions<CloudflareOptions>()
16+
builder.Services.AddOptionsWithValidateOnStart<CloudflareOptions>()
1517
.Bind(builder.Configuration.GetSection(CloudflareOptions.Cloudflare));
18+
builder.Services.AddSingleton<IValidateOptions<CloudflareOptions>, CloudflareOptionsValidator>();
1619

17-
builder.Services.AddOptions<List<RecordOptions>>()
18-
.Bind(builder.Configuration.GetSection(RecordOptions.Records));
20+
builder.Services.AddOptionsWithValidateOnStart<DdnsOptions>()
21+
.Bind(builder.Configuration.GetSection(DdnsOptions.Ddns));
22+
builder.Services.AddSingleton<IValidateOptions<DdnsOptions>, DdnsOptionsValidator>();
1923

2024
builder.Services.AddTransient<CloudflareApiTokenMessageHandler>();
2125

src/Core/appsettings.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
"Cloudflare": {
33
"ApiToken": ""
44
},
5-
"Records": [
6-
{
7-
"Key": "",
8-
"ZoneId": "",
9-
"DnsRecordId": ""
10-
}
11-
]
5+
"DDNS": {
6+
"Records": [
7+
{
8+
"Key": "",
9+
"ZoneId": "",
10+
"DnsRecordId": ""
11+
}
12+
]
13+
}
1214
}

0 commit comments

Comments
 (0)