Skip to content

Commit

Permalink
Merge pull request #3 from Hekku2/feat/options-validation
Browse files Browse the repository at this point in the history
Validate options
  • Loading branch information
Hekku2 authored Jun 21, 2024
2 parents 91ed294 + 9284fb7 commit 063a5c7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
12 changes: 6 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace DiscordImagePoster.Common.BlobStorageImageService;

/// <summary>
/// Settings for the Azure Blob Storage which hosts the images.
/// This is used to fetch images from the storage account.
/// </summary>
public class BlobStorageImageSourceOptions
{
/// <summary>
Expand All @@ -11,6 +17,7 @@ public class BlobStorageImageSourceOptions
/// <summary>
/// The name of the container where the images are stored.
/// </summary>
[Required]
public required string ContainerName { get; set; }

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Common/Discord/DiscordConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.ComponentModel.DataAnnotations;

namespace DiscordImagePoster.Common.Discord;

/// <summary>
Expand All @@ -10,19 +12,23 @@ public class DiscordConfiguration
/// The token of the bot. This is used to authenticate the bot with the
/// Discord API.
/// </summary>
[Required]
[MinLength(1)]
public required string Token { get; set; }

/// <summary>
/// The ID of the guild / server where the bot will post images.
/// 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.
/// </summary>
[Required]
public ulong GuildId { get; set; }

/// <summary>
/// The ID of the channel where the bot will post images.
/// 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.
/// </summary>
[Required]
public ulong ChannelId { get; set; }
}
15 changes: 15 additions & 0 deletions src/Common/IndexService/ImageIndexOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
using System.ComponentModel.DataAnnotations;

namespace DiscordImagePoster.Common.IndexService;

/// <summary>
/// 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.
/// </summary>
public class ImageIndexOptions
{
/// <summary>
/// The connection string to the Azure Storage account.
/// </summary>
public required string ConnectionString { get; set; }

/// <summary>
/// Container name where the index is stored.
/// </summary>
[Required]
public required string ContainerName { get; set; }
}
1 change: 1 addition & 0 deletions src/FunctionApp.Isolated/FunctionApp.Isolated.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />
</ItemGroup>

<ItemGroup Label="Dependency resolutions">
Expand Down
8 changes: 4 additions & 4 deletions src/FunctionApp.Isolated/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddOptions<BlobStorageImageSourceOptions>().BindConfiguration(nameof(BlobStorageImageSourceOptions));
services.AddOptions<DiscordConfiguration>().BindConfiguration(nameof(DiscordConfiguration));
services.AddOptions<FeatureSettings>().BindConfiguration(nameof(FeatureSettings));
services.AddOptions<ImageIndexOptions>().BindConfiguration(nameof(ImageIndexOptions));
services.AddOptions<BlobStorageImageSourceOptions>().BindConfiguration(nameof(BlobStorageImageSourceOptions)).ValidateDataAnnotations().ValidateOnStart();
services.AddOptions<DiscordConfiguration>().BindConfiguration(nameof(DiscordConfiguration)).ValidateDataAnnotations().ValidateOnStart();
services.AddOptions<FeatureSettings>().BindConfiguration(nameof(FeatureSettings)).ValidateDataAnnotations().ValidateOnStart();
services.AddOptions<ImageIndexOptions>().BindConfiguration(nameof(ImageIndexOptions)).ValidateDataAnnotations().ValidateOnStart();
services.AddTransient<IDiscordImagePoster>(services =>
{
Expand Down

0 comments on commit 063a5c7

Please sign in to comment.