From e7a6e03a3d631e96e2383afbde30c86f51ed09e5 Mon Sep 17 00:00:00 2001 From: Heikki-Jussi Niemi Date: Fri, 12 Jul 2024 17:57:46 +0300 Subject: [PATCH] Developer usablity improvements (#21) * Add partial implementation of secrets * Add secrets * Add process flow chart * Ignore unnecessary dev secrets --- .gitignore | 1 + Create-Settings.ps1 | 20 +++++++- README.md | 22 +++++++- docker-compose.yml | 1 + src/ConsoleTester/ConsoleTester.csproj | 51 ++++++++++--------- src/ConsoleTester/Program.cs | 6 ++- src/ConsoleTester/appsettings.json | 4 +- .../Functions/ImageSendFunction.cs | 4 -- 8 files changed, 75 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 84619ad..94aa54b 100644 --- a/.gitignore +++ b/.gitignore @@ -403,3 +403,4 @@ FodyWeavers.xsd .env developer-settings.json publish.zip +src/FunctionApp.Isolated/dev_secrets/sendimage.json diff --git a/Create-Settings.ps1 b/Create-Settings.ps1 index 682b9ab..808b6e2 100644 --- a/Create-Settings.ps1 +++ b/Create-Settings.ps1 @@ -19,6 +19,13 @@ Set-StrictMode -Version Latest Write-Host "Reading settings from file $SettingsFile" $settingsJson = Get-Content -Raw -Path $SettingsFile | ConvertFrom-Json +$cognitiveServicesEndpoint = '' +if (-not [string]::IsNullOrEmpty($settingsJson.ExistingCognitiveServicesAccountName) -and -not [string]::IsNullOrEmpty($settingsJson.ExistingCognitiveServicesResourceGroup)) { + Write-Host 'Retrieving cognitive services endpoint from existing account' + $service = Get-AzCognitiveServicesAccount -ResourceGroupName hjni-discord-image-poster -AccountName aisa-hjni-discord-image-poster + $cognitiveServicesEndpoint = $service.Endpoint +} + # Docker compose support $dockerEnvFile = "$PSScriptRoot/.env" $dockerEnvFileContent = " @@ -27,12 +34,14 @@ DISCORD_TOKEN=$($settingsJson.DiscordToken) DISCORD_GUILDID=$($settingsJson.DiscordGuildId) DISCORD_CHANNELID=$($settingsJson.DiscordChannelId) DISCORD_PUBLICKEY=$($settingsJson.DiscordPublicKey) +COGNITIVESERVICES_ENDPOINT=$cognitiveServicesEndpoint " -Write-Host "Writing $dockerEnvFile" +Write-Host "Writing Docker ENV $dockerEnvFile" $dockerEnvFileContent | Out-File -FilePath $dockerEnvFile -Encoding utf8 # Function Core Tools support $funcSettingsFile = "$PSScriptRoot/src/FunctionApp.Isolated/local.settings.json" +Write-Host "Writing Function Core Toole support $funcSettingsFile" $localSettings = @{ IsEncrypted = $false Values = @{ @@ -41,3 +50,12 @@ $localSettings = @{ } } $localSettings | ConvertTo-Json | Out-File -FilePath $funcSettingsFile -Encoding utf8 + +Write-Host "Writing user-secrets for console tester." + +# NOTE: For some reason __ didn't work in the key names, so I had to use : instead. +dotnet user-secrets --project src/ConsoleTester set "DiscordConfiguration:Token" "$($settingsJson.DiscordToken)" +dotnet user-secrets --project src/ConsoleTester set "DiscordConfiguration:GuildId" "$($settingsJson.DiscordGuildId)" +dotnet user-secrets --project src/ConsoleTester set "DiscordConfiguration:ChannelId" "$($settingsJson.DiscordChannelId)" +dotnet user-secrets --project src/ConsoleTester set "DiscordConfiguration:PublicKey" "$($settingsJson.DiscordPublicKey)" +dotnet user-secrets --project src/ConsoleTester set "ImageAnalysisConfiguration:Endpoint" "$($cognitiveServicesEndpoint)" diff --git a/README.md b/README.md index f6a8cd1..3783e98 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,26 @@ In depth 1. Caption and tags from Azure AI Vision response are saved to index. 1. Image is sent to discord with received caption. If image analysis is not used, file name is used as caption. +Same with flowchart: + +```mermaid +flowchart TD + A[Timer triggered] --> B{Is index built?}; + B -- Yes --> D[Get random image from index]; + B -- No --> C[Build index!]; + C ----> D; + D ----> E{Does Image exist?}; + E -- No --> X[Process finished]; + E -- Yes --> F[Download image]; + F ----> G{Image analysis enabled}; + G -- Yes --> H[Send to Image Analysis Service]; + G -- No --> I{Is Discord sending enabled?}; + H ----> I; + I -- Yes --> K[Send image to Discord]; + I -- No --> X; + K ----> X; +``` + Indexing * Indexing is performed automatically if index doesn't exist. * Image index can be regenerated by calling the related function. @@ -37,7 +57,7 @@ Indexing ## Tools This section lists tools that are used in developing and deploying this -software. Some are not strictly +software. Some are not strictly needed, but make developing easier. Development * Dotnet 8.0 (or later) diff --git a/docker-compose.yml b/docker-compose.yml index dc0d2c6..bc9bf35 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,6 +20,7 @@ services: - BlobStorageImageSourceOptions__FolderPath=testfolder - ImageIndexOptions__ConnectionString=DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://local.storage.emulator:10000/devstoreaccount1;QueueEndpoint=http://local.storage.emulator:10001/devstoreaccount1; - ImageIndexOptions__ContainerName=index + - ImageAnalysisConfiguration__Endpoint=${COGNITIVESERVICES_ENDPOINT} - FeatureSettings__DisableTimedSending=true - FeatureSettings__DisableDiscordSending=true depends_on: diff --git a/src/ConsoleTester/ConsoleTester.csproj b/src/ConsoleTester/ConsoleTester.csproj index 2ecd138..a513f50 100644 --- a/src/ConsoleTester/ConsoleTester.csproj +++ b/src/ConsoleTester/ConsoleTester.csproj @@ -1,25 +1,26 @@ - - - - Exe - net8.0 - enable - enable - - - - - - - - - - PreserveNewest - - - - - - - - + + + + Exe + net8.0 + enable + enable + ff4c4b81-7829-41ef-9739-2a99df22e531 + + + + + + + + + + PreserveNewest + + + + + + + + diff --git a/src/ConsoleTester/Program.cs b/src/ConsoleTester/Program.cs index 4a82190..98bbcf0 100644 --- a/src/ConsoleTester/Program.cs +++ b/src/ConsoleTester/Program.cs @@ -15,7 +15,11 @@ public class Program public static async Task Main(string[] args) { // Note: CreateApplicationBuilder is mainly used for easier access to config, DI, etc. - var builder = Host.CreateApplicationBuilder(args); + var settings = new HostApplicationBuilderSettings + { + EnvironmentName = "Development" + }; + var builder = Host.CreateApplicationBuilder(settings); await Parser.Default.ParseArguments(args) .MapResult( diff --git a/src/ConsoleTester/appsettings.json b/src/ConsoleTester/appsettings.json index efa1164..4c8b6db 100644 --- a/src/ConsoleTester/appsettings.json +++ b/src/ConsoleTester/appsettings.json @@ -6,7 +6,7 @@ } }, "DiscordConfiguration": { - "Token": "", + "Token": "read from secrets", "GuildId": "", "ChannelId": "", "PublicKey": "" @@ -22,6 +22,6 @@ "IndexFileName": "index.json" }, "ImageAnalysisConfiguration": { - "Endpoint": "" + "Endpoint": "read from secrets" } } diff --git a/src/FunctionApp.Isolated/Functions/ImageSendFunction.cs b/src/FunctionApp.Isolated/Functions/ImageSendFunction.cs index c3aebaf..c8ee1e9 100644 --- a/src/FunctionApp.Isolated/Functions/ImageSendFunction.cs +++ b/src/FunctionApp.Isolated/Functions/ImageSendFunction.cs @@ -1,8 +1,4 @@ using System.Net; -using DiscordImagePoster.Common.BlobStorageImageService; -using DiscordImagePoster.Common.Discord; -using DiscordImagePoster.Common.ImageAnalysis; -using DiscordImagePoster.Common.IndexService; using DiscordImagePoster.Common.RandomizationService; using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Http;