using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using System; using DotNetEnv; namespace Inventory.Core { /// /// Sets up application secrets from .env file in Development or HashiCorp Vault in Production. /// public static class EnvironmentBuilder { // Define the keys for your secrets private const string _dbConnectionString = "DB_CONNECTION_STRING"; private const string _slurpitUrl = "SLURPIT_URL"; private const string _slurpitApiKey = "SLURPIT_API_KEY"; private const string _updateIntervalMinutes = "UPDATE_INTERVAL_MINUTES"; /// /// Loads secrets into static properties based on the hosting environment. /// public static void SetupEnvironment(IHostEnvironment environment) { // Load secrets from the .env file. // Env.TraversePath() looks for .env in the current directory and parent directories. // This is suitable for both development and when the agent is installed. // The .env file should be placed in the same directory as the executable (e.g., Agent, AdminTool). Env.TraversePath().Load(); // Assign to static properties Secrets.DbConnectionString = Environment.GetEnvironmentVariable(_dbConnectionString); Secrets.SlurpitUrl = Environment.GetEnvironmentVariable(_slurpitUrl); Secrets.SlurpitApiKey = Environment.GetEnvironmentVariable(_slurpitApiKey); int.TryParse(Environment.GetEnvironmentVariable(_updateIntervalMinutes), out var interval); Secrets.UpdateIntervalMinutes = interval > 0 ? interval : 60; // Default to 60 minutes } } /// /// A static class to hold application secrets. /// public static class Secrets { public static string? DbConnectionString { get; set; } public static string? SlurpitUrl { get; set; } public static string? SlurpitApiKey { get; set; } public static int UpdateIntervalMinutes { get; set; } } }