Skip to content

Commit

Permalink
Update Settings class & Docker setup, add TwitchWorker tests
Browse files Browse the repository at this point in the history
Renamed `OpenAI` and `Twitch` classes to `OpenAISettings` and `TwitchSettings` respectively, and updated the `Settings` class and all related code as needed. The Docker setup has been adjusted to use a two-staged build and run process to better separate build and runtime environments. Added a new empty test class `TwitchWorkerTests` for future use.
  • Loading branch information
BillChirico committed Dec 11, 2023
1 parent e17f953 commit 3249144
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 27 deletions.
29 changes: 13 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
FROM mcr.microsoft.com/dotnet/runtime:8.0-bookworm-slim AS base
USER $APP_UID
# Stage 1: Build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0-bookworm-slim AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["GolfClapBot/GolfClapBot.Runner.csproj", "GolfClapBot/"]
RUN dotnet restore "GolfClapBot/GolfClapBot.Runner.csproj"
COPY . .
WORKDIR "/src/Volvox.Twitch.Responder"
RUN dotnet build "GolfClapBot.Runner.csproj" -c $BUILD_CONFIGURATION -o /app/build
# Copy csproj and restore as distinct layers
COPY src/GolfClapBot.Runner/*.csproj ./src/GolfClapBot.Runner/
RUN dotnet restore src/GolfClapBot.Runner/GolfClapBot.Runner.csproj

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "GolfClapBot.Runner.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# Copy everything else and build
COPY src/GolfClapBot.Runner/ ./src/GolfClapBot.Runner/
WORKDIR /app/src/GolfClapBot.Runner
RUN dotnet build -c Release -o out

FROM base AS final
# Stage 2: Run the application
FROM mcr.microsoft.com/dotnet/runtime:8.0
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GolfClapBot.dll"]
COPY --from=build-env /app/src/GolfClapBot.Runner/out .
ENTRYPOINT ["dotnet", "GolfClapBot.Runner.dll"]
10 changes: 10 additions & 0 deletions build-and-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Define the image name
IMAGE_NAME=GolfClapBot

# Build the Docker image
docker build -t $IMAGE_NAME .

# Run the Docker container
docker run -d -p 8080:80 --name GolfClapBot $IMAGE_NAME
2 changes: 1 addition & 1 deletion src/GolfClapBot.Bot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Bot : IBot
public Bot(IOptions<Data> data, IOptions<Settings> settings)
{
_data = data.Value;
_openAiClient = new OpenAIAPI(settings.Value.OpenAi.ApiKey);
_openAiClient = new OpenAIAPI(settings.Value.OpenAiSettings.ApiKey);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace GolfClapBot.Domain.Configuration;

public class OpenAI
public class OpenAISettings
{
public required string ApiKey { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions src/GolfClapBot.Domain/Configuration/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class Settings
{
public required Twitch Twitch { get; set; }
public required TwitchSettings TwitchSettings { get; set; }

public required OpenAI OpenAi { get; set; }
public required OpenAISettings OpenAiSettings { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace GolfClapBot.Domain.Configuration;

public class Twitch
public class TwitchSettings
{
public required string Channel { get; set; }

Expand Down
9 changes: 5 additions & 4 deletions src/GolfClapBot.Runner/TwitchWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public TwitchWorker(ILogger<TwitchWorker> logger, ILoggerFactory loggerFactory,
_client.OnMessageReceived += TwitchClientOnMessageReceived;
_client.OnJoinedChannel += TwitchClientOnJoinedChannel;

var credentials = new ConnectionCredentials(_settings.Value.Twitch.BotUser, _settings.Value.Twitch.OAuthToken);
var credentials = new ConnectionCredentials(_settings.Value.TwitchSettings.BotUser,
_settings.Value.TwitchSettings.OAuthToken);

_client.Initialize(credentials, _settings.Value.Twitch.Channel);
_client.Initialize(credentials, _settings.Value.TwitchSettings.Channel);
_client.ConnectAsync();
}

Expand Down Expand Up @@ -184,14 +185,14 @@ private static string RemoveEmotes(ChatMessage message)
/// </summary>
/// <param name="message">The chat message to be deleted.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
private async Task DeleteMessage(ChatMessage message)
public async Task DeleteMessage(ChatMessage message)
{
try
{
_logger.LogWarning("Bot is deleting message: {MessageId}", message.Id);

await _twitchApi.Helix.Moderation.DeleteChatMessagesAsync(message.RoomId, "425816290", message.Id,
_settings.Value.Twitch.OAuthToken);
_settings.Value.TwitchSettings.OAuthToken);
}
catch (Exception exception)
{
Expand Down
4 changes: 2 additions & 2 deletions test/GolfClapBot.Test/BotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public void Constructor_InitializesWithValidSettings_ShouldSetProperties()
};
var expectedSettings = new Settings
{
OpenAi = new OpenAI
OpenAiSettings = new OpenAISettings
{
ApiKey = "test-key", Model = null

Check warning on line 22 in test/GolfClapBot.Test/BotTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 22 in test/GolfClapBot.Test/BotTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
},
Twitch = null
TwitchSettings = null

Check warning on line 24 in test/GolfClapBot.Test/BotTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 24 in test/GolfClapBot.Test/BotTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
};
mockData.Setup(m => m.Value).Returns(expectedData);
mockSettings.Setup(m => m.Value).Returns(expectedSettings);
Expand Down
5 changes: 5 additions & 0 deletions test/GolfClapBot.Test/TwitchWorkerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace GolfClapBot.Test;

public class TwitchWorkerTests
{
}

0 comments on commit 3249144

Please sign in to comment.