mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-14 17:54:42 +00:00
Extract shared configuration helpers into Infrastructure.Configuration library (#248)
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Infrastructure.Configuration;
|
||||
|
||||
public static class ConfigurationHelpers
|
||||
{
|
||||
public static string GetKeyOrThrow(IConfiguration configuration, string key) =>
|
||||
configuration[key]
|
||||
?? throw new InvalidOperationException($"The {key} environment variable is not set.");
|
||||
|
||||
public static bool GetBooleanKeyOrThrow(IConfiguration configuration, string key)
|
||||
{
|
||||
string value = GetKeyOrThrow(configuration, key);
|
||||
|
||||
return bool.TryParse(value, out bool result)
|
||||
? result
|
||||
: throw new InvalidOperationException(
|
||||
$"The {key} environment variable is set to '{value}', which is not a valid boolean."
|
||||
);
|
||||
}
|
||||
|
||||
public static int GetIntKeyOrThrow(IConfiguration configuration, string key)
|
||||
{
|
||||
string value = GetKeyOrThrow(configuration, key);
|
||||
|
||||
return int.TryParse(value, out int result)
|
||||
? result
|
||||
: throw new InvalidOperationException(
|
||||
$"The {key} environment variable is set to '{value}', which is not a valid integer."
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace Infrastructure.Configuration;
|
||||
|
||||
public static class ConfigurationKeys
|
||||
{
|
||||
public const string AccessTokenSecret = "ACCESS_TOKEN_SECRET";
|
||||
public const string RefreshTokenSecret = "REFRESH_TOKEN_SECRET";
|
||||
public const string ConfirmationTokenSecret = "CONFIRMATION_TOKEN_SECRET";
|
||||
|
||||
/// <summary>
|
||||
/// Fallback configuration section path for the access-token signing key, checked when
|
||||
/// <see cref="AccessTokenSecret" /> is not set.
|
||||
/// </summary>
|
||||
public const string JwtSecretKeyFallback = "Jwt:SecretKey";
|
||||
|
||||
public const string WebsiteBaseUrl = "WEBSITE_BASE_URL";
|
||||
|
||||
public const string SmtpHost = "SMTP_HOST";
|
||||
public const string SmtpPort = "SMTP_PORT";
|
||||
public const string SmtpUsername = "SMTP_USERNAME";
|
||||
public const string SmtpPassword = "SMTP_PASSWORD";
|
||||
public const string SmtpUseSsl = "SMTP_USE_SSL";
|
||||
public const string SmtpFromEmail = "SMTP_FROM_EMAIL";
|
||||
public const string SmtpFromName = "SMTP_FROM_NAME";
|
||||
|
||||
public const string DbServer = "DB_SERVER";
|
||||
public const string DbUser = "DB_USER";
|
||||
public const string DbPassword = "DB_PASSWORD";
|
||||
public const string DbName = "DB_NAME";
|
||||
public const string DbConnectionString = "DB_CONNECTION_STRING";
|
||||
public const string DbTrustServerCertificate = "DB_TRUST_SERVER_CERTIFICATE";
|
||||
|
||||
public const string ClearDatabase = "CLEAR_DATABASE";
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>Infrastructure.Configuration</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference
|
||||
Include="Microsoft.Extensions.Configuration.Abstractions"
|
||||
Version="10.0.1"
|
||||
/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -13,4 +13,8 @@
|
||||
Version="10.0.1"
|
||||
/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Infrastructure.Configuration\Infrastructure.Configuration.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using MailKit.Net.Smtp;
|
||||
using Infrastructure.Configuration;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MimeKit;
|
||||
@@ -25,31 +26,27 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
/// <c>SMTP_FROM_EMAIL</c>, <c>SMTP_FROM_NAME</c>) from <paramref name="configuration" />.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when <c>SMTP_HOST</c> or <c>SMTP_FROM_EMAIL</c> is not set, or when <c>SMTP_PORT</c> is not a valid integer.
|
||||
/// Thrown when <c>SMTP_HOST</c>, <c>SMTP_PORT</c>, or <c>SMTP_FROM_EMAIL</c> is not set,
|
||||
/// or when <c>SMTP_PORT</c> is not a valid integer.
|
||||
/// </exception>
|
||||
public SmtpEmailProvider(IConfiguration configuration)
|
||||
{
|
||||
_host =
|
||||
configuration["SMTP_HOST"]
|
||||
?? throw new InvalidOperationException("SMTP_HOST environment variable is not set");
|
||||
_host = ConfigurationHelpers.GetKeyOrThrow(configuration, ConfigurationKeys.SmtpHost);
|
||||
|
||||
string portString = configuration["SMTP_PORT"] ?? "587";
|
||||
if (!int.TryParse(portString, out _port))
|
||||
throw new InvalidOperationException($"SMTP_PORT '{portString}' is not a valid integer");
|
||||
_port = ConfigurationHelpers.GetIntKeyOrThrow(configuration, ConfigurationKeys.SmtpPort);
|
||||
|
||||
_username = configuration["SMTP_USERNAME"];
|
||||
_password = configuration["SMTP_PASSWORD"];
|
||||
_username = configuration[ConfigurationKeys.SmtpUsername];
|
||||
_password = configuration[ConfigurationKeys.SmtpPassword];
|
||||
|
||||
string useSslString = configuration["SMTP_USE_SSL"] ?? "true";
|
||||
string useSslString = configuration[ConfigurationKeys.SmtpUseSsl] ?? "true";
|
||||
_useSsl = bool.Parse(useSslString);
|
||||
|
||||
_fromEmail =
|
||||
configuration["SMTP_FROM_EMAIL"]
|
||||
?? throw new InvalidOperationException(
|
||||
"SMTP_FROM_EMAIL environment variable is not set"
|
||||
);
|
||||
_fromEmail = ConfigurationHelpers.GetKeyOrThrow(
|
||||
configuration,
|
||||
ConfigurationKeys.SmtpFromEmail
|
||||
);
|
||||
|
||||
_fromName = configuration["SMTP_FROM_NAME"] ?? "The Biergarten";
|
||||
_fromName = configuration[ConfigurationKeys.SmtpFromName] ?? "The Biergarten";
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
Reference in New Issue
Block a user