Skip to content

Commit

Permalink
Merge pull request #37 from EpicOfficer/ENHANCEMENT/Kubernetes
Browse files Browse the repository at this point in the history
Prepare solution for potential migration to kubernetes
  • Loading branch information
EpicOfficer authored Oct 7, 2024
2 parents 6b3bd21 + 6074340 commit c69d087
Show file tree
Hide file tree
Showing 33 changed files with 340 additions and 281 deletions.
9 changes: 5 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ DISCORD_CLIENT_SECRET=***
DISCORD_BOT_TOKEN=***
DISCORD_DEV_GUILD=0000000000000000000

POSTGRES_USER=blink
POSTGRES_USER=blink3
POSTGRES_PASSWORD=password

REDIS_NAME=blink

BLINK_RUN_MIGRATIONS=true

API_ALLOWED_ORIGIN=http://localhost:5280
API_ALLOWED_ORIGIN=http://localhost:8280
API_URL=http://localhost:8288/

WORDS_API_KEY=***

ENCRYPTION_KEY="<<Base64 encoded aes encryption key>>"
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
run: dotnet test --no-build --verbosity normal
6 changes: 6 additions & 0 deletions .idea/.idea.Blink3/.idea/git_toolbox_blame.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions .run/Blink3 Backend.run.xml → .run/Blink3 Development.run.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Blink3 Backend" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<configuration default="false" name="Blink3 Development" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<deployment type="docker-compose.yml">
<settings>
<option name="envFilePath" value="" />
<option name="commandLineOptions" value="--build" />
<option name="secondarySourceFiles">
<list>
<option value="docker-compose.development.yml" />
</list>
</option>
<option name="services">
<list>
<option value="blink3.api" />
<option value="blink3.bot" />
<option value="blink3.cache" />
<option value="blink3.db" />
</list>
</option>
<option name="sourceFilePath" value="docker-compose.yml" />
</settings>
</deployment>
Expand Down
24 changes: 0 additions & 24 deletions .run/Blink3.API_Dockerfile.run.xml

This file was deleted.

23 changes: 0 additions & 23 deletions .run/Blink3.Activity_Dockerfile.run.xml

This file was deleted.

15 changes: 0 additions & 15 deletions .run/Blink3.Bot_Dockerfile.run.xml

This file was deleted.

27 changes: 11 additions & 16 deletions Blink3.API/Blink3.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,24 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNet.Security.OAuth.Discord" Version="8.0.0"/>
<PackageReference Include="Discord.Addons.Hosting" Version="6.0.0" />
<PackageReference Include="Discord.Net" Version="3.14.1" />
<PackageReference Include="Discord.Net.Rest" Version="3.14.1"/>
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageReference Include="Serilog" Version="3.1.1"/>
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1"/>
<PackageReference Include="AspNet.Security.OAuth.Discord" Version="8.2.0" />
<PackageReference Include="Discord.Net" Version="3.16.0" />
<PackageReference Include="Discord.Net.Rest" Version="3.16.0" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Serilog" Version="4.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.8.1" />
</ItemGroup>

<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
<Content Include="..\Configuration\appsettings.Development.json">
<Link>appsettings.Development.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\Configuration\appsettings.json">
<Link>appsettings.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
43 changes: 22 additions & 21 deletions Blink3.API/Controllers/ApiControllerBase.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Net.Mime;
using Blink3.API.Interfaces;
using Blink3.Core.Caching;
using Blink3.Core.DiscordAuth.Extensions;
using Blink3.Core.Models;
using Discord;
using Discord.Addons.Hosting.Util;
using Discord.Rest;
using Discord.WebSocket;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
Expand All @@ -24,11 +23,12 @@ namespace Blink3.API.Controllers;
[Route("api/[controller]")]
[ApiController]
[Authorize]
public abstract class ApiControllerBase(DiscordSocketClient discordSocketClient, ICachingService cachingService) : ControllerBase
public abstract class ApiControllerBase(DiscordRestClient botClient,
Func<DiscordRestClient> userClientFactory,
ICachingService cachingService,
IEncryptionService encryptionService) : ControllerBase
{
protected readonly ICachingService CachingService = cachingService;
protected DiscordRestClient? Client;
protected readonly DiscordSocketClient DiscordBotClient = discordSocketClient;
private readonly DiscordRestClient _userClient = userClientFactory();

/// <summary>
/// Represents an Unauthorized Access message.
Expand Down Expand Up @@ -90,28 +90,26 @@ private ObjectResult ProblemForUnauthorizedAccess()
return userId != UserId ? ProblemForUnauthorizedAccess() : null;
}

protected async Task InitDiscordClientAsync()
private async Task AuthenticateUserClientAsync()
{
await DiscordBotClient.WaitForReadyAsync(CancellationToken.None);
if (Client is not null) return;
string? accessToken = await CachingService.GetAsync<string>($"token:{UserId}");
if (accessToken is null) return;

Client = new DiscordRestClient();
await Client.LoginAsync(TokenType.Bearer, accessToken);
string? encryptedToken = await cachingService.GetAsync<string>($"token:{UserId}");
string? iv = await cachingService.GetAsync<string>($"token:{UserId}:iv");
if (encryptedToken is null || iv is null) return;

string accessToken = encryptionService.Decrypt(encryptedToken, iv);
await _userClient.LoginAsync(TokenType.Bearer, accessToken);
}

protected async Task<List<DiscordPartialGuild>> GetUserGuilds()
{
await InitDiscordClientAsync();
await AuthenticateUserClientAsync();

List<DiscordPartialGuild> managedGuilds = await CachingService.GetOrAddAsync($"discord:guilds:{UserId}",
List<DiscordPartialGuild> managedGuilds = await cachingService.GetOrAddAsync($"discord:guilds:{UserId}",
async () =>
{
List<DiscordPartialGuild> manageable = [];
if (Client is null) return manageable;

IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> guilds = Client.GetGuildSummariesAsync();
IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> guilds = _userClient.GetGuildSummariesAsync();
await foreach (IReadOnlyCollection<RestUserGuild> guildCollection in guilds)
{
manageable.AddRange(guildCollection.Where(g => g.Permissions.ManageGuild).Select(g =>
Expand All @@ -126,7 +124,10 @@ protected async Task<List<DiscordPartialGuild>> GetUserGuilds()
return manageable;
}, TimeSpan.FromMinutes(5));

List<ulong> discordGuildIds = DiscordBotClient.Guilds.Select(b => b.Id).ToList();
List<ulong> discordGuildIds = await botClient.GetGuildSummariesAsync()
.SelectMany(guildCollection => guildCollection.ToAsyncEnumerable())
.Select(guild => guild.Id)
.ToListAsync();
return managedGuilds.Where(g => discordGuildIds.Contains(g.Id)).ToList();
}

Expand All @@ -145,7 +146,7 @@ protected async Task<List<DiscordPartialGuild>> GetUserGuilds()

~ApiControllerBase()
{
Client?.Dispose();
Client = null;
_userClient.LogoutAsync().Wait();
_userClient.Dispose();
}
}
12 changes: 9 additions & 3 deletions Blink3.API/Controllers/BlinkGuildsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Blink3.API.Interfaces;
using Blink3.Core.Caching;
using Blink3.Core.DTOs;
using Blink3.Core.Entities;
using Blink3.Core.Models;
using Blink3.Core.Repositories.Interfaces;
using Discord.WebSocket;
using Discord.Rest;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
Expand All @@ -14,7 +14,12 @@ namespace Blink3.API.Controllers;
/// Controller for performing CRUD operations on BlinkGuild items.
/// </summary>
[SwaggerTag("All CRUD operations for BlinkGuild items")]
public class BlinkGuildsController(DiscordSocketClient discordSocketClient, ICachingService cachingService, IBlinkGuildRepository blinkGuildRepository) : ApiControllerBase(discordSocketClient, cachingService)
public class BlinkGuildsController(DiscordRestClient botClient,
Func<DiscordRestClient> userClientFactory,
ICachingService cachingService,
IEncryptionService encryptionService,
IBlinkGuildRepository blinkGuildRepository)
: ApiControllerBase(botClient, userClientFactory, cachingService, encryptionService)
{
/// <summary>
/// Retrieves all BlinkGuild items that are manageable by the logged in user.
Expand Down Expand Up @@ -62,6 +67,7 @@ public async Task<ActionResult<UserTodo>> GetBlinkGuild(ulong id)
/// </summary>
/// <param name="id">The ID of the BlinkGuild item to update.</param>
/// <param name="blinkGuild">The updated BlinkGuild item data.</param>
/// <param name="cancellationToken"></param>
/// <returns>
/// No content.
/// </returns>
Expand Down
Loading

0 comments on commit c69d087

Please sign in to comment.