108 lines
3.1 KiB
C#
108 lines
3.1 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using ZA.CoreService.ESBCertificateManager.Models;
|
|
|
|
namespace ZA.CoreService.ESBCertificateManager.Configuration;
|
|
|
|
public static class AppSettingsLoader
|
|
{
|
|
public static AppSettings Load()
|
|
{
|
|
string basePath = Directory.Exists(AppContext.BaseDirectory)
|
|
? AppContext.BaseDirectory
|
|
: Directory.GetCurrentDirectory();
|
|
|
|
string settingsPath = Path.Combine(basePath, "appsettings.json");
|
|
|
|
if (!File.Exists(settingsPath))
|
|
{
|
|
string cwdPath = Path.Combine(
|
|
Directory.GetCurrentDirectory(),
|
|
"appsettings.json");
|
|
|
|
if (File.Exists(cwdPath))
|
|
{
|
|
basePath = Directory.GetCurrentDirectory();
|
|
}
|
|
else
|
|
{
|
|
throw new FileNotFoundException(
|
|
"Die Datei appsettings.json wurde nicht gefunden.",
|
|
settingsPath);
|
|
}
|
|
}
|
|
|
|
IConfigurationRoot configuration = new ConfigurationBuilder()
|
|
.SetBasePath(basePath)
|
|
.AddJsonFile(
|
|
"appsettings.json",
|
|
optional: false,
|
|
reloadOnChange: false)
|
|
.Build();
|
|
|
|
AppSettings settings = new();
|
|
configuration.Bind(settings);
|
|
|
|
Validate(settings);
|
|
|
|
return settings;
|
|
}
|
|
|
|
private static void Validate(
|
|
AppSettings settings)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(settings.EnvironmentCode))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"EnvironmentCode fehlt in appsettings.json.");
|
|
}
|
|
|
|
settings.EnvironmentCode =
|
|
settings.EnvironmentCode
|
|
.Trim()
|
|
.ToUpperInvariant();
|
|
|
|
string[] allowedEnvironmentCodes =
|
|
[
|
|
"DEV",
|
|
"TEST",
|
|
"PROD"
|
|
];
|
|
|
|
if (!allowedEnvironmentCodes.Contains(
|
|
settings.EnvironmentCode,
|
|
StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"EnvironmentCode '{settings.EnvironmentCode}' ist ungültig. " +
|
|
"Erlaubt sind DEV, TEST und PROD.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(
|
|
settings.Database.ConnectionString))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Database:ConnectionString fehlt in appsettings.json.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(
|
|
settings.Runtime.JavaExecutablePath))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Runtime:JavaExecutablePath fehlt in appsettings.json.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(
|
|
settings.Runtime.SonicClientLibraryPath))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Runtime:SonicClientLibraryPath fehlt in appsettings.json.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(
|
|
settings.AlwaysEncrypted.CertificateThumbprint))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"AlwaysEncrypted:CertificateThumbprint fehlt.");
|
|
}
|
|
}
|
|
} |