Skip to content

Commit 11d75dc

Browse files
committed
Fix environment variable things, add dev docker compose
1 parent c48648c commit 11d75dc

9 files changed

Lines changed: 92 additions & 25 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
.idea
33
obj/
44
bin/
5-
*.user
5+
*.user
6+
Dev/dragonfly
7+
Dev/postgres

API/Options/MailOptions.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using Microsoft.Extensions.Options;
1+
using System.ComponentModel.DataAnnotations;
22
using OpenShock.API.Services.Email.Mailjet.Mail;
3-
using System.ComponentModel.DataAnnotations;
43

54
namespace OpenShock.API.Options;
65

@@ -13,19 +12,12 @@ public sealed class MailOptions
1312
[Required]
1413
public required MailType Type { get; init; }
1514

16-
[Required]
17-
[ValidateObjectMembers]
18-
public required Contact Sender { get; init; }
19-
20-
[ValidateObjectMembers]
21-
public MailJetOptions? Mailjet { get; init; }
22-
23-
[ValidateObjectMembers]
24-
public SmtpOptions? Smtp { get; init; }
25-
2615
public enum MailType
2716
{
2817
Mailjet = 0,
29-
Smtp = 1
18+
Smtp = 1,
19+
None = 2
3020
}
21+
22+
public sealed class MailSenderContact : Contact;
3123
}

API/Services/Email/EmailServiceExtension.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
using OpenShock.API.Services.Email.Mailjet;
33
using OpenShock.API.Services.Email.Mailjet.Mail;
44
using OpenShock.API.Services.Email.Smtp;
5-
using System.Configuration;
65

76
namespace OpenShock.API.Services.Email;
87

98
public static class EmailServiceExtension
109
{
1110
public static WebApplicationBuilder AddEmailService(this WebApplicationBuilder builder)
1211
{
13-
builder.Services.Configure<Contact>(MailOptions.SenderOptionName, builder.Configuration.GetRequiredSection(MailOptions.SenderSectionName));
14-
1512
var mailOptions = builder.Configuration.GetRequiredSection(MailOptions.SectionName).Get<MailOptions>() ?? throw new NullReferenceException();
13+
14+
if(mailOptions.Type == MailOptions.MailType.None)
15+
{
16+
builder.Services.AddSingleton<IEmailService, NoneEmailService>(); // Add a dummy email service
17+
return builder;
18+
}
19+
20+
// Add sender contact configuration
21+
builder.AddSenderContactConfiguration();
22+
1623
switch (mailOptions.Type)
1724
{
1825
case MailOptions.MailType.Mailjet:
@@ -27,4 +34,11 @@ public static WebApplicationBuilder AddEmailService(this WebApplicationBuilder b
2734

2835
return builder;
2936
}
37+
38+
private static WebApplicationBuilder AddSenderContactConfiguration(this WebApplicationBuilder builder)
39+
{
40+
builder.Services.Configure<Contact>(MailOptions.SenderSectionName,
41+
builder.Configuration.GetRequiredSection(MailOptions.SectionName));
42+
return builder;
43+
}
3044
}

API/Services/Email/Mailjet/Mail/Contact.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace OpenShock.API.Services.Email.Mailjet.Mail;
55

6-
public sealed class Contact
6+
public class Contact
77
{
88
[Required(AllowEmptyStrings = false)]
99
public required string Email { get; set; }

API/Services/Email/Mailjet/MailjetEmailService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@ namespace OpenShock.API.Services.Email.Mailjet;
1010
public sealed class MailjetEmailService : IEmailService, IDisposable
1111
{
1212
private readonly HttpClient _httpClient;
13-
private readonly Contact _sender;
1413
private readonly MailJetOptions _options;
1514
private readonly ILogger<MailjetEmailService> _logger;
15+
private readonly MailOptions.MailSenderContact _sender;
1616

1717
/// <summary>
1818
/// DI Constructor
1919
/// </summary>
2020
/// <param name="httpClient"></param>
21-
/// <param name="senderOptions"></param>
2221
/// <param name="options"></param>
22+
/// <param name="sender"></param>
2323
/// <param name="logger"></param>
2424
public MailjetEmailService(
2525
HttpClient httpClient,
26-
IOptionsSnapshot<Contact> senderOptions,
2726
IOptions<MailJetOptions> options,
27+
IOptions<MailOptions.MailSenderContact> sender,
2828
ILogger<MailjetEmailService> logger
2929
)
3030
{
3131
_httpClient = httpClient;
32-
_sender = senderOptions.Get(MailOptions.SenderOptionName);
32+
_sender = sender.Value;
3333
_options = options.Value;
3434
_logger = logger;
3535
}

API/Services/Email/Mailjet/MailjetEmailServiceExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OpenShock.API.Options;
22
using System.Net.Http.Headers;
33
using System.Text;
4+
using Microsoft.Extensions.Options;
45

56
namespace OpenShock.API.Services.Email.Mailjet;
67

@@ -11,7 +12,7 @@ public static WebApplicationBuilder AddMailjetEmailService(this WebApplicationBu
1112
var section = builder.Configuration.GetRequiredSection(MailJetOptions.SectionName);
1213

1314
builder.Services.Configure<MailJetOptions>(section);
14-
builder.Services.AddSingleton<MailJetOptionsValidator>();
15+
builder.Services.AddSingleton<IValidateOptions<MailJetOptions>, MailJetOptionsValidator>();
1516

1617
var options = section.Get<MailJetOptions>() ?? throw new NullReferenceException("MailJetOptions is null!");
1718
var basicAuthValue = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{options.Key}:{options.Secret}"));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using OpenShock.API.Services.Email.Mailjet.Mail;
2+
3+
namespace OpenShock.API.Services.Email;
4+
5+
/// <summary>
6+
/// This is a noop implementation of the email service. It does nothing.
7+
/// Consumers should properly handle when this service is used, so realistaically this should never be used.
8+
/// But we need it for DI satisfaction.
9+
/// </summary>
10+
public class NoneEmailService : IEmailService
11+
{
12+
public Task PasswordReset(Contact to, Uri resetLink, CancellationToken cancellationToken = default)
13+
{
14+
return Task.CompletedTask;
15+
}
16+
17+
public Task VerifyEmail(Contact to, Uri activationLink, CancellationToken cancellationToken = default)
18+
{
19+
return Task.CompletedTask;
20+
}
21+
}

API/Services/Email/Smtp/SmtpEmailServiceExtension.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using OpenShock.API.Options;
1+
using Microsoft.Extensions.Options;
2+
using OpenShock.API.Options;
23

34
namespace OpenShock.API.Services.Email.Smtp;
45

@@ -9,7 +10,7 @@ public static WebApplicationBuilder AddSmtpEmailService(this WebApplicationBuild
910
var section = builder.Configuration.GetRequiredSection(SmtpOptions.SectionName);
1011

1112
builder.Services.Configure<SmtpOptions>(section);
12-
builder.Services.AddSingleton<SmtpOptionsValidator>();
13+
builder.Services.AddSingleton<IValidateOptions<SmtpOptions>, SmtpOptionsValidator>();
1314

1415
builder.Services.AddSingleton(new SmtpServiceTemplates
1516
{

Dev/docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# THIS IS MEANT FOR DEVELOPMENT; NOT PRODUCTION; DATA SAVED BY THIS STACK IS NOT CONSIDERED SECRET AND SAFE
2+
3+
services:
4+
postgres:
5+
image: postgres:17
6+
container_name: openshock-postgres
7+
healthcheck:
8+
test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
9+
start_period: 20s
10+
interval: 30s
11+
retries: 5
12+
timeout: 5s
13+
networks:
14+
- openshock
15+
environment:
16+
POSTGRES_PASSWORD: openshock # This is not safe for production
17+
POSTGRES_USER: openshock
18+
POSTGRES_DB: openshock
19+
volumes:
20+
- ./postgres:/var/lib/postgresql/data
21+
ports:
22+
- 5432:5432
23+
24+
dragonfly:
25+
image: ghcr.io/dragonflydb/dragonfly:latest
26+
container_name: openshock-dragonfly
27+
command: '--notify_keyspace_events=Ex'
28+
volumes:
29+
- ./dragonfly:/data
30+
networks:
31+
- openshock
32+
ports:
33+
- 6379:6379
34+
35+
networks:
36+
openshock:

0 commit comments

Comments
 (0)