Files
FsToolbox/Cli/Helper/Settings.cs
2025-12-12 08:23:19 +01:00

101 lines
2.6 KiB
C#

using Microsoft.Extensions.Configuration;
namespace FsToolbox.Cli.Helper
{
/// <summary>
/// Represents the root configuration settings for the application, including API and credentials.
/// </summary>
public class AppSettings
{
#region Public Properties
/// <summary>
/// Gets the API-related settings.
/// </summary>
public ApiSettings Api { get; init; } = new();
/// <summary>
/// Gets the credentials settings for authentication.
/// </summary>
public CredentialsSettings Credentials { get; init; } = new();
#endregion
}
/// <summary>
/// Contains settings related to the API endpoints.
/// </summary>
public class ApiSettings
{
#region Public Properties
/// <summary>
/// Gets the base URL for the API, without the /api path segment.
/// </summary>
public string BaseUrl { get; init; } = "https://beta.foodsharing.de";
#endregion
}
/// <summary>
/// Contains credentials and authentication settings.
/// </summary>
public class CredentialsSettings
{
#region Public Properties
/// <summary>
/// Gets the email address used for login.
/// </summary>
public string Email { get; init; } = string.Empty;
/// <summary>
/// Gets the password used for login.
/// </summary>
public string Password { get; init; } = string.Empty;
/// <summary>
/// Gets a value indicating whether two-factor authentication is enabled.
/// </summary>
public bool TwoFactorEnabled { get; init; }
#endregion
}
/// <summary>
/// Provides access to the current application settings loaded from configuration.
/// </summary>
public static class SettingsProvider
{
#region Constants
private static AppSettings? _current;
#endregion
#region Public Properties
/// <summary>
/// Gets the current application settings instance.
/// </summary>
public static AppSettings Current => _current ??= Load();
#endregion
#region Private Method Load
private static AppSettings Load()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", false, true)
.Build();
var settings = new AppSettings();
configuration.Bind(settings);
return settings;
}
#endregion
}
}