50 lines
2.1 KiB
C#
50 lines
2.1 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using DotNetEnv;
|
|
|
|
namespace Inventory.Core
|
|
{
|
|
/// <summary>
|
|
/// Sets up application secrets from .env file in Development or HashiCorp Vault in Production.
|
|
/// </summary>
|
|
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";
|
|
|
|
/// <summary>
|
|
/// Loads secrets into static properties based on the hosting environment.
|
|
/// </summary>
|
|
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
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A static class to hold application secrets.
|
|
/// </summary>
|
|
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; }
|
|
}
|
|
} |