Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed May 6, 2024
1 parent 5858720 commit 80470dc
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<PackageVersion Include="Akka.Hosting.TestKit" Version="1.5.20" />
<PackageVersion Include="Akka.Streams" Version="1.5.20" />
<PackageVersion Include="Akka.Streams.TestKit" Version="1.5.20" />
<PackageVersion Include="FluentEmail.Mailgun" Version="3.0.2" />
<PackageVersion Include="FluentEmail.Razor" Version="3.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.4" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.4" />
</ItemGroup>
<!-- ASP.NET Package Versions -->
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ A collaborative browser-based drawing program written in .NET.
Please see [REQUIREMENTS](docs/requirements.md) for more information.

## UI
![DrawTogether.NET UI](/docs/images/paintarea-ui.png)
![DrawTogether.NET UI](/docs/images/paintarea-ui.png)

## MailGun Configuration

NugetUpdates uses [MailGun](https://mailgun.com/) to send outbound emails (via `FluentEmail.Mailgun`) - and the following two secrets need to be configured in order for that sending to work:

```shell
cd ./src/NuGetUpdates.Web/
dotnet user-secrets set "EmailSettings:MailgunDomain" "<mailgun-domain>"
dotnet user-secrets set "EmailSettings:MailgunApiKey" "<mailgun-api-key>"
```
5 changes: 0 additions & 5 deletions src/DrawTogether.Email/Class1.cs

This file was deleted.

6 changes: 6 additions & 0 deletions src/DrawTogether.Email/DrawTogether.Email.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentEmail.Mailgun" />
<PackageReference Include="FluentEmail.Razor" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions src/DrawTogether.Email/EmailSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace DrawTogether.Email;

public class EmailSettings
{
public required string MailgunDomain { get; set; }

public required string MailgunApiKey { get; set; }

public required string FromAddress { get; set; }

public required string FromName { get; set; }
}
36 changes: 36 additions & 0 deletions src/DrawTogether.Email/MailGunEmailSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using FluentEmail.Core.Interfaces;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace DrawTogether.Email;

/// <summary>
/// Wraps FluentEmail to send emails using MailGun within the context of ASP.NET Core Identity.
/// </summary>
public sealed class MailGunEmailSender : IEmailSender
{
private readonly ILogger<MailGunEmailSender> _logger;
private readonly ISender _mailgunSender;
private readonly EmailSettings _emailSettings;

public MailGunEmailSender(ISender mailgunSender, IOptions<EmailSettings> emailSettings, ILogger<MailGunEmailSender> logger)
{
_logger = logger;
_mailgunSender = mailgunSender;
_emailSettings = emailSettings.Value;
}

public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
_logger.LogDebug("Sending email to {Email} with subject {Subject}", email, subject);

var mailMessage = FluentEmail.Core.Email
.From(_emailSettings.FromAddress, _emailSettings.FromName)
.To(email)
.Subject(subject)
.Body(htmlMessage, true);

return _mailgunSender.SendAsync(mailMessage);
}
}
29 changes: 29 additions & 0 deletions src/DrawTogether.Email/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace DrawTogether.Email;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration)
{
// check to see if the EmaiLSettings section is configured in the appsettings.json file
if (!configuration.GetSection("EmailSettings").Exists())
{
// bail out early if email is not configured
return services;
}

services.Configure<EmailSettings>(configuration.GetSection("EmailSettings"));

services.AddFluentEmail(configuration["EmailSettings:FromAddress"], configuration["EmailSettings:FromName"])
.AddRazorRenderer()
.AddMailGunSender(configuration["EmailSettings:MailgunDomain"],
configuration["EmailSettings:MailgunApiKey"]);

services.AddTransient<IEmailSender, MailGunEmailSender>();

return services;
}
}
4 changes: 4 additions & 0 deletions src/DrawTogether.UI/DrawTogether/DrawTogether.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\DrawTogether.Email\DrawTogether.Email.csproj" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/DrawTogether.UI/DrawTogether/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using DrawTogether.Components;
using DrawTogether.Components.Account;
using DrawTogether.Data;
using DrawTogether.Email;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -15,6 +16,7 @@
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
builder.Services.AddEmailServices(builder.Configuration); // add email services

builder.Services.AddAuthentication(options =>
{
Expand Down

0 comments on commit 80470dc

Please sign in to comment.