mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-14 17:54:42 +00:00
Replace raw environment variable reads with IConfiguration in backend
This commit is contained in:
@@ -8,5 +8,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MailKit" Version="4.16.0" />
|
||||
<PackageReference
|
||||
Include="Microsoft.Extensions.Configuration.Abstractions"
|
||||
Version="10.0.1"
|
||||
/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MimeKit;
|
||||
|
||||
namespace Infrastructure.Email;
|
||||
|
||||
/// <summary>
|
||||
/// SMTP email service implementation using MailKit.
|
||||
/// Configured via environment variables.
|
||||
/// Configured via application configuration (backed by environment variables).
|
||||
/// </summary>
|
||||
public class SmtpEmailProvider : IEmailProvider
|
||||
{
|
||||
@@ -20,35 +21,35 @@ public class SmtpEmailProvider : IEmailProvider
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SmtpEmailProvider" /> class, reading SMTP configuration
|
||||
/// from environment variables (<c>SMTP_HOST</c>, <c>SMTP_PORT</c>, <c>SMTP_USERNAME</c>,
|
||||
/// <c>SMTP_PASSWORD</c>, <c>SMTP_USE_SSL</c>, <c>SMTP_FROM_EMAIL</c>, <c>SMTP_FROM_NAME</c>).
|
||||
/// (<c>SMTP_HOST</c>, <c>SMTP_PORT</c>, <c>SMTP_USERNAME</c>, <c>SMTP_PASSWORD</c>, <c>SMTP_USE_SSL</c>,
|
||||
/// <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.
|
||||
/// </exception>
|
||||
public SmtpEmailProvider()
|
||||
public SmtpEmailProvider(IConfiguration configuration)
|
||||
{
|
||||
_host =
|
||||
Environment.GetEnvironmentVariable("SMTP_HOST")
|
||||
configuration["SMTP_HOST"]
|
||||
?? throw new InvalidOperationException("SMTP_HOST environment variable is not set");
|
||||
|
||||
string portString = Environment.GetEnvironmentVariable("SMTP_PORT") ?? "587";
|
||||
string portString = configuration["SMTP_PORT"] ?? "587";
|
||||
if (!int.TryParse(portString, out _port))
|
||||
throw new InvalidOperationException($"SMTP_PORT '{portString}' is not a valid integer");
|
||||
|
||||
_username = Environment.GetEnvironmentVariable("SMTP_USERNAME");
|
||||
_password = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
|
||||
_username = configuration["SMTP_USERNAME"];
|
||||
_password = configuration["SMTP_PASSWORD"];
|
||||
|
||||
string useSslString = Environment.GetEnvironmentVariable("SMTP_USE_SSL") ?? "true";
|
||||
string useSslString = configuration["SMTP_USE_SSL"] ?? "true";
|
||||
_useSsl = bool.Parse(useSslString);
|
||||
|
||||
_fromEmail =
|
||||
Environment.GetEnvironmentVariable("SMTP_FROM_EMAIL")
|
||||
configuration["SMTP_FROM_EMAIL"]
|
||||
?? throw new InvalidOperationException(
|
||||
"SMTP_FROM_EMAIL environment variable is not set"
|
||||
);
|
||||
|
||||
_fromName = Environment.GetEnvironmentVariable("SMTP_FROM_NAME") ?? "The Biergarten";
|
||||
_fromName = configuration["SMTP_FROM_NAME"] ?? "The Biergarten";
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -20,21 +20,21 @@ public class DefaultSqlConnectionFactory(IConfiguration configuration) : ISqlCon
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the SQL Server connection string, preferring (in order): the <c>DB_CONNECTION_STRING</c>
|
||||
/// environment variable, a connection string built from individual <c>DB_*</c> environment variables,
|
||||
/// configuration value, a connection string built from individual <c>DB_*</c> configuration values,
|
||||
/// and finally the <c>"Default"</c> connection string from configuration.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">Thrown when no connection string can be resolved from any source.</exception>
|
||||
private static string GetConnectionString(IConfiguration configuration)
|
||||
{
|
||||
// Check for full connection string first
|
||||
string? fullConnectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
|
||||
string? fullConnectionString = configuration["DB_CONNECTION_STRING"];
|
||||
if (!string.IsNullOrEmpty(fullConnectionString))
|
||||
return fullConnectionString;
|
||||
|
||||
// Try to build from individual environment variables (preferred method for Docker)
|
||||
// Try to build from individual configuration values (preferred method for Docker)
|
||||
try
|
||||
{
|
||||
return SqlConnectionStringHelper.BuildConnectionString();
|
||||
return SqlConnectionStringHelper.BuildConnectionString(configuration);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Infrastructure.Sql;
|
||||
|
||||
/// <summary>
|
||||
/// Helper for building SQL Server connection strings from environment variables.
|
||||
/// Helper for building SQL Server connection strings from configuration
|
||||
/// (backed by the <c>DB_*</c> environment variables).
|
||||
/// </summary>
|
||||
public static class SqlConnectionStringHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds a SQL Server connection string from environment variables.
|
||||
/// Builds a SQL Server connection string from configuration.
|
||||
/// </summary>
|
||||
/// <remarks>Throws <see cref="InvalidOperationException" /> if DB_SERVER, DB_NAME, DB_USER, or DB_PASSWORD is unset.</remarks>
|
||||
/// <param name="databaseName">If null, falls back to the DB_NAME environment variable.</param>
|
||||
public static string BuildConnectionString(string? databaseName = null)
|
||||
/// <param name="configuration">The configuration to read the <c>DB_*</c> values from.</param>
|
||||
/// <param name="databaseName">If null, falls back to the DB_NAME configuration value.</param>
|
||||
public static string BuildConnectionString(
|
||||
IConfiguration configuration,
|
||||
string? databaseName = null
|
||||
)
|
||||
{
|
||||
string server = Required("DB_SERVER");
|
||||
string dbName = databaseName ?? Required("DB_NAME");
|
||||
string user = Required("DB_USER");
|
||||
string password = Required("DB_PASSWORD");
|
||||
string server = Required(configuration, "DB_SERVER");
|
||||
string dbName = databaseName ?? Required(configuration, "DB_NAME");
|
||||
string user = Required(configuration, "DB_USER");
|
||||
string password = Required(configuration, "DB_PASSWORD");
|
||||
|
||||
SqlConnectionStringBuilder builder = new()
|
||||
{
|
||||
@@ -25,7 +31,7 @@ public static class SqlConnectionStringHelper
|
||||
InitialCatalog = dbName,
|
||||
UserID = user,
|
||||
Password = password,
|
||||
TrustServerCertificate = ResolveTrustServerCertificate(),
|
||||
TrustServerCertificate = ResolveTrustServerCertificate(configuration),
|
||||
Encrypt = true,
|
||||
};
|
||||
|
||||
@@ -33,29 +39,27 @@ public static class SqlConnectionStringHelper
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a connection string to the master database using environment variables.
|
||||
/// Builds a connection string to the master database using configuration.
|
||||
/// </summary>
|
||||
public static string BuildMasterConnectionString()
|
||||
public static string BuildMasterConnectionString(IConfiguration configuration)
|
||||
{
|
||||
return BuildConnectionString("master");
|
||||
return BuildConnectionString(configuration, "master");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the <c>DB_TRUST_SERVER_CERTIFICATE</c> environment variable, defaulting to
|
||||
/// Resolves the <c>DB_TRUST_SERVER_CERTIFICATE</c> configuration value, defaulting to
|
||||
/// <c>true</c> when it is unset or not a valid boolean.
|
||||
/// </summary>
|
||||
private static bool ResolveTrustServerCertificate()
|
||||
private static bool ResolveTrustServerCertificate(IConfiguration configuration)
|
||||
{
|
||||
return !bool.TryParse(
|
||||
Environment.GetEnvironmentVariable("DB_TRUST_SERVER_CERTIFICATE"),
|
||||
configuration["DB_TRUST_SERVER_CERTIFICATE"],
|
||||
out bool trustServerCertificate
|
||||
)
|
||||
|| trustServerCertificate;
|
||||
}
|
||||
|
||||
private static string Required(string variableName) =>
|
||||
Environment.GetEnvironmentVariable(variableName)
|
||||
?? throw new InvalidOperationException(
|
||||
$"The {variableName} environment variable is not set."
|
||||
);
|
||||
private static string Required(IConfiguration configuration, string key) =>
|
||||
configuration[key]
|
||||
?? throw new InvalidOperationException($"The {key} environment variable is not set.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user