Skip to content

Commit

Permalink
Developer usablity improvements (#21)
Browse files Browse the repository at this point in the history
* Add partial implementation of secrets

* Add secrets

* Add process flow chart

* Ignore unnecessary dev secrets
  • Loading branch information
Hekku2 authored Jul 12, 2024
1 parent 4cc27c6 commit e7a6e03
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,4 @@ FodyWeavers.xsd
.env
developer-settings.json
publish.zip
src/FunctionApp.Isolated/dev_secrets/sendimage.json
20 changes: 19 additions & 1 deletion Create-Settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "
Expand All @@ -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 = @{
Expand All @@ -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)"
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
51 changes: 26 additions & 25 deletions src/ConsoleTester/ConsoleTester.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>ff4c4b81-7829-41ef-9739-2a99df22e531</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion src/ConsoleTester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiscordSendVerb, GetIndexVerb, RefreshIndexVerb, AnalyzeImageVerb, RegisterCommandVerb>(args)
.MapResult(
Expand Down
4 changes: 2 additions & 2 deletions src/ConsoleTester/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
},
"DiscordConfiguration": {
"Token": "",
"Token": "read from secrets",
"GuildId": "",
"ChannelId": "",
"PublicKey": ""
Expand All @@ -22,6 +22,6 @@
"IndexFileName": "index.json"
},
"ImageAnalysisConfiguration": {
"Endpoint": ""
"Endpoint": "read from secrets"
}
}
4 changes: 0 additions & 4 deletions src/FunctionApp.Isolated/Functions/ImageSendFunction.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit e7a6e03

Please sign in to comment.