Newsbot.Collector/Newsbot.Collector.Services/EnvLoader.cs

77 lines
1.6 KiB
C#
Raw Normal View History

2023-02-14 17:51:22 -08:00
using Newsbot.Collector.Domain.Models;
namespace Newsbot.Collector.Services;
public static class EnvLoader
{
public static ConfigModel Load()
{
var reddit = new RedditConfigModel
{
IsEnabled = Bool("FEATURE_ENABLE_REDDIT_BACKEND"),
PullHot = Bool("REDDIT_PULL_HOT"),
PullNsfw = Bool("REDDIT_PULL_NSFW"),
PullTop = Bool("REDDIT_PULL_TOP")
};
return new ConfigModel
{
ServerAddress = String("SERVER_ADDRESS"),
SqlConnectionString = String("SQL_CONNECTION_STRING"),
Reddit = reddit,
};
}
public static void LoadEnvFile()
{
var curDir = Directory.GetCurrentDirectory();
var filePath = Path.Combine(curDir, ".env");
if (!File.Exists(filePath))
return;
foreach (var line in File.ReadAllLines(filePath))
{
var parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
continue;
if (parts[1].Contains("'") == true ){
parts[1] = parts[1].Replace("'", "");
}
Environment.SetEnvironmentVariable(parts[0], parts[1]);
}
}
private static string String(string Key)
{
var result = Environment.GetEnvironmentVariable(Key);
if (result is null)
{
return "";
}
return result;
}
private static bool Bool(string Key)
{
var result = String(Key);
if (result == "")
{
return false;
}
if (result.ToLower() == "true")
{
return true;
}
else
{
return false;
}
}
}