From 3072a2d116f596433aca8aff51c55a9c7866b7a0 Mon Sep 17 00:00:00 2001 From: Heikki-Jussi Niemi Date: Fri, 21 Jun 2024 23:30:58 +0300 Subject: [PATCH 1/3] Add validation for Discord Configuration --- .../BlobStorageImageSourceOptions.cs | 3 +++ src/Common/Discord/DiscordConfiguration.cs | 6 ++++++ src/FunctionApp.Isolated/FunctionApp.Isolated.csproj | 1 + src/FunctionApp.Isolated/Program.cs | 2 +- 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs b/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs index 3a1aba6..419d1d5 100644 --- a/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs +++ b/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs @@ -1,3 +1,5 @@ +using System.ComponentModel.DataAnnotations; + namespace DiscordImagePoster.Common.BlobStorageImageService; public class BlobStorageImageSourceOptions @@ -11,6 +13,7 @@ public class BlobStorageImageSourceOptions /// /// The name of the container where the images are stored. /// + [Required] public required string ContainerName { get; set; } /// diff --git a/src/Common/Discord/DiscordConfiguration.cs b/src/Common/Discord/DiscordConfiguration.cs index 85984eb..b6182d2 100644 --- a/src/Common/Discord/DiscordConfiguration.cs +++ b/src/Common/Discord/DiscordConfiguration.cs @@ -1,3 +1,5 @@ +using System.ComponentModel.DataAnnotations; + namespace DiscordImagePoster.Common.Discord; /// @@ -10,6 +12,8 @@ public class DiscordConfiguration /// The token of the bot. This is used to authenticate the bot with the /// Discord API. /// + [Required] + [MinLength(1)] public required string Token { get; set; } /// @@ -17,6 +21,7 @@ public class DiscordConfiguration /// This can be fetched from the Discord server url. /// For example, the ID of the guild in the url https://discord.com/channels/123123/666666 is 123123. /// + [Required] public ulong GuildId { get; set; } /// @@ -24,5 +29,6 @@ public class DiscordConfiguration /// This can be fetched from the Discord server url. /// For example, the ID of the guild in the url https://discord.com/channels/123123/666666 is 666666. /// + [Required] public ulong ChannelId { get; set; } } diff --git a/src/FunctionApp.Isolated/FunctionApp.Isolated.csproj b/src/FunctionApp.Isolated/FunctionApp.Isolated.csproj index 6ea7f86..acfa44c 100644 --- a/src/FunctionApp.Isolated/FunctionApp.Isolated.csproj +++ b/src/FunctionApp.Isolated/FunctionApp.Isolated.csproj @@ -15,6 +15,7 @@ + diff --git a/src/FunctionApp.Isolated/Program.cs b/src/FunctionApp.Isolated/Program.cs index b7abc0c..31a4728 100644 --- a/src/FunctionApp.Isolated/Program.cs +++ b/src/FunctionApp.Isolated/Program.cs @@ -15,7 +15,7 @@ .ConfigureServices(services => { services.AddOptions().BindConfiguration(nameof(BlobStorageImageSourceOptions)); - services.AddOptions().BindConfiguration(nameof(DiscordConfiguration)); + services.AddOptions().BindConfiguration(nameof(DiscordConfiguration)).ValidateDataAnnotations().ValidateOnStart(); services.AddOptions().BindConfiguration(nameof(FeatureSettings)); services.AddOptions().BindConfiguration(nameof(ImageIndexOptions)); From 0df96cb13d2b31ac8fd0bd25f80b35dc04e04f8d Mon Sep 17 00:00:00 2001 From: Heikki-Jussi Niemi Date: Fri, 21 Jun 2024 23:48:58 +0300 Subject: [PATCH 2/3] Add validaiton for other settings --- .../BlobStorageImageSourceOptions.cs | 4 ++++ src/Common/IndexService/ImageIndexOptions.cs | 15 +++++++++++++++ src/FunctionApp.Isolated/Program.cs | 6 +++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs b/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs index 419d1d5..4f76621 100644 --- a/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs +++ b/src/Common/BlobStorageImageService/BlobStorageImageSourceOptions.cs @@ -2,6 +2,10 @@ namespace DiscordImagePoster.Common.BlobStorageImageService; +/// +/// Settings for the Azure Blob Storage which hosts the images. +/// This is used to fetch images from the storage account. +/// public class BlobStorageImageSourceOptions { /// diff --git a/src/Common/IndexService/ImageIndexOptions.cs b/src/Common/IndexService/ImageIndexOptions.cs index a325851..531dee2 100644 --- a/src/Common/IndexService/ImageIndexOptions.cs +++ b/src/Common/IndexService/ImageIndexOptions.cs @@ -1,7 +1,22 @@ +using System.ComponentModel.DataAnnotations; + namespace DiscordImagePoster.Common.IndexService; +/// +/// Image index options decide where the index of the images is stored. +/// This is used to keep track of the images that have been posted to Discord +/// and what images are available to post. +/// public class ImageIndexOptions { + /// + /// The connection string to the Azure Storage account. + /// public required string ConnectionString { get; set; } + + /// + /// Container name where the index is stored. + /// + [Required] public required string ContainerName { get; set; } } diff --git a/src/FunctionApp.Isolated/Program.cs b/src/FunctionApp.Isolated/Program.cs index 31a4728..634805f 100644 --- a/src/FunctionApp.Isolated/Program.cs +++ b/src/FunctionApp.Isolated/Program.cs @@ -14,10 +14,10 @@ .ConfigureFunctionsWorkerDefaults() .ConfigureServices(services => { - services.AddOptions().BindConfiguration(nameof(BlobStorageImageSourceOptions)); + services.AddOptions().BindConfiguration(nameof(BlobStorageImageSourceOptions)).ValidateDataAnnotations().ValidateOnStart(); services.AddOptions().BindConfiguration(nameof(DiscordConfiguration)).ValidateDataAnnotations().ValidateOnStart(); - services.AddOptions().BindConfiguration(nameof(FeatureSettings)); - services.AddOptions().BindConfiguration(nameof(ImageIndexOptions)); + services.AddOptions().BindConfiguration(nameof(FeatureSettings)).ValidateDataAnnotations().ValidateOnStart(); + services.AddOptions().BindConfiguration(nameof(ImageIndexOptions)).ValidateDataAnnotations().ValidateOnStart(); services.AddTransient(services => { From 9284fb75daf95dd401e6b1f4286d963223078467 Mon Sep 17 00:00:00 2001 From: Heikki-Jussi Niemi Date: Sat, 22 Jun 2024 00:07:00 +0300 Subject: [PATCH 3/3] Fix docker compose --- docker-compose.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index fa155f4..dc0d2c6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,9 +12,9 @@ services: - ASPNETCORE_ENVIRONMENT=Development - AzureWebJobsStorage=AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=https;BlobEndpoint=http://azurite:local.storage.emulator/devstoreaccount1;QueueEndpoint=http://local.storage.emulator:10001/devstoreaccount1;TableEndpoint=http://local.storage.emulator:10002/devstoreaccount1; - AzureWebJobsSecretStorageType=files - - DiscordOptions__Token=${DISCORD_TOKEN} - - DiscordOptions__GuildId=${DISCORD_GUILDID} - - DiscordOptions__ChannelId=${DISCORD_CHANNELID} + - DiscordConfiguration__Token=${DISCORD_TOKEN} + - DiscordConfiguration__GuildId=${DISCORD_GUILDID} + - DiscordConfiguration__ChannelId=${DISCORD_CHANNELID} - BlobStorageImageSourceOptions__ConnectionString=DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://local.storage.emulator:10000/devstoreaccount1;QueueEndpoint=http://local.storage.emulator:10001/devstoreaccount1; - BlobStorageImageSourceOptions__ContainerName=images - BlobStorageImageSourceOptions__FolderPath=testfolder @@ -32,6 +32,6 @@ services: container_name: local.storage.emulator command: azurite --loose --disableProductStyleUrl --blobHost 0.0.0.0 --blobPort 10000 --queueHost 0.0.0.0 --queuePort 10001 --location /workspace --debug /workspace/debug.log ports: - - 10000:10000 - - 10001:10001 - - 10002:10002 + - 10000:10000 + - 10001:10001 + - 10002:10002