Skip to content

Commit

Permalink
Added a background voting bot service (#9)
Browse files Browse the repository at this point in the history
* Added a background voting bot service

* Added VoteBot image build

* Added the votebot to docker-compose
  • Loading branch information
bravecobra committed Sep 26, 2021
1 parent 18afc63 commit 2970a35
Show file tree
Hide file tree
Showing 19 changed files with 720 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:
svcDockerName: bravecobra/emoji-voting-svc
- svcProjectName: EmojiUI
svcDockerName: bravecobra/emoji-ui-svc
- svcProjectName: EmojiVoteBot
svcDockerName: bravecobra/emoji-votebot
needs: [build]
steps:
- name: Checkout
Expand Down
20 changes: 9 additions & 11 deletions EmojiUI/Controllers/EmojisController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EmojiUI.Services;
using Emojivoto.V1;
using Microsoft.AspNetCore.Mvc;
using Emoji = EmojiUI.Controllers.Dtos.Emoji;

Expand All @@ -23,16 +24,13 @@ public async Task<IEnumerable<Emoji>> ListEmojis()
return await _voteService.ListEmojis();
}

// [HttpGet("{shortcode}")]
// public async Task<Emoji> FindByShortCode(string shortcode)
// {
// var response = await _client.FindByShortcodeAsync(new FindByShortcodeRequest()
// {
// Shortcode = shortcode
// });
// return response.Emoji != null ?
// new Emoji { Shortcode = response.Emoji.Shortcode, Unicode = response.Emoji.Unicode } :
// null;
// }
[HttpGet("{shortcode}")]
public async Task<Emoji> FindByShortCode(string shortcode)
{
var response = await _voteService.FindByShortCode(shortcode);
return response != null ?
new Emoji { Shortcode = response.Shortcode, Unicode = response.Unicode } :
null;
}
}
}
11 changes: 9 additions & 2 deletions EmojiUI/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
<div class="col-md-12">
@if (VoteState.Value.Error == string.Empty)
{
<h1 class="headline">🗳</h1>
@if (VoteState.Value.SelectedEmoji != null)
{
<h1 class="headline">@VoteState.Value.SelectedEmoji.Unicode</h1>
}
else
{
<h1 class="headline">🗳</h1>
}
}
else
{
Expand All @@ -23,7 +30,7 @@
@if (VoteState.Value.SelectedEmoji?.Shortcode == ":doughnut:")
{
<div>
<p class="doughnut-explanation">For the sake of this demo, voting for 🍩<br />always returns an error.
<p class="doughnut-explanation">For the sake of this demo, voting for 🍩<br />randomly returns an error.
</p>
<p>
<NavLink class="nav-link" href="/" @onclick="() => Dispatcher.Dispatch(new ResetAction())" ><div class="btn btn-white">Pick another</div></NavLink>
Expand Down
8 changes: 6 additions & 2 deletions EmojiUI/Pages/Leaderboard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
<NavLink href="/">
<div class="btn btn-blue">Vote on your favorite</div></NavLink>
<div class="emoji-list">
@if (!LeaderBoardState.Value.IsLoading)
@if (LeaderBoardState.Value.IsLoading)
{
foreach (var result in LeaderBoardState.Value?.Results)
<p><em>Loading results...</em></p>
}
else
{
foreach (var result in LeaderBoardState?.Value?.Results)
{
var title = $"{result.Votes} votes";
<div class="emoji" title="@title">
Expand Down
5 changes: 4 additions & 1 deletion EmojiUI/Shared/Store/LeaderBoard/LeaderBoardFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ public class LeaderBoardFeature: Feature<LeaderBoardState>

protected override LeaderBoardState GetInitialState()
{
return new LeaderBoardState(false, null);
return new LeaderBoardState(
isLoading: true,
results: null,
error:string.Empty);
}
}
}
21 changes: 21 additions & 0 deletions EmojiVoteBot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["EmojiVoteBot/EmojiVoteBot.csproj", "EmojiVoteBot/"]
RUN dotnet restore "EmojiVoteBot/EmojiVoteBot.csproj"
COPY ./EmojiVoteBot ./EmojiVoteBot
COPY ./protos ./protos
WORKDIR "/src/EmojiVoteBot"
RUN dotnet build "EmojiVoteBot.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "EmojiVoteBot.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "EmojiVoteBot.dll"]
28 changes: 28 additions & 0 deletions EmojiVoteBot/EmojiVoteBot.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<UserSecretsId>dotnet-EmojiVoteBot-0BD6347B-842F-470D-A520-B5B3B79983FD</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.13.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.32.0" />
<PackageReference Include="Grpc.Tools" Version="2.32.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="..\protos\Emoji.proto" GrpcServices="Client">
<Link>Protos\Emoji.proto</Link>
</Protobuf>
<Protobuf Include="..\protos\Voting.proto" GrpcServices="Client">
<Link>Protos\Voting.proto</Link>
</Protobuf>
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions EmojiVoteBot/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using EmojiVoteBot.Services;
using EmojiVoteBot.Services.Impl;
using System;
using System.Text;

namespace EmojiVoteBot
{
public static class Program
{
public static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHttpClient();
services.AddTransient<IEmojiVoteService, EmojiVoteRestService>();
services.AddHostedService<VotingBot>();
});
}
}
15 changes: 15 additions & 0 deletions EmojiVoteBot/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"profiles": {
"EmojiVoteBot": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development",
"WEB_HOST": "http://localhost:8080"
},
"dotnetRunMessages": "true"
},
"Docker": {
"commandName": "Docker"
}
}
}
14 changes: 14 additions & 0 deletions EmojiVoteBot/Services/Emoji.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmojiVoteBot.Services
{
public class Emoji
{
public string Unicode { get; set; } = null!;
public string Shortcode { get; set; } = null!;
}
}
12 changes: 12 additions & 0 deletions EmojiVoteBot/Services/IEmojiVoteService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EmojiVoteBot.Services
{
public interface IEmojiVoteService
{
Task<IEnumerable<Emoji>> ListEmojis();
Task<Emoji> FindByShortCode(string shortcode);
Task<bool> Vote(string choice);
}
}
Loading

0 comments on commit 2970a35

Please sign in to comment.