Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

59 řádky
1.6KB

  1. namespace Campaign_Tracker.Server.Configuration;
  2. public static class DotEnvConfiguration
  3. {
  4. public static void Load(ConfigurationManager configuration, string contentRootPath)
  5. {
  6. var rootEnvPath = Path.GetFullPath(Path.Combine(contentRootPath, "..", ".env"));
  7. var serverEnvPath = Path.Combine(contentRootPath, ".env");
  8. var values = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
  9. AddFileValues(rootEnvPath, values);
  10. AddFileValues(serverEnvPath, values);
  11. if (values.Count > 0)
  12. {
  13. configuration.AddInMemoryCollection(values);
  14. }
  15. }
  16. private static void AddFileValues(string path, IDictionary<string, string?> values)
  17. {
  18. if (!File.Exists(path))
  19. {
  20. return;
  21. }
  22. foreach (var line in File.ReadLines(path))
  23. {
  24. var trimmed = line.Trim();
  25. if (trimmed.Length == 0 || trimmed.StartsWith('#'))
  26. {
  27. continue;
  28. }
  29. var separatorIndex = trimmed.IndexOf('=');
  30. if (separatorIndex <= 0)
  31. {
  32. continue;
  33. }
  34. var key = trimmed[..separatorIndex].Trim().Replace("__", ":");
  35. var value = trimmed[(separatorIndex + 1)..].Trim();
  36. values[key] = Unquote(value);
  37. }
  38. }
  39. private static string Unquote(string value)
  40. {
  41. if (value.Length >= 2 &&
  42. ((value.StartsWith('"') && value.EndsWith('"')) ||
  43. (value.StartsWith('\'') && value.EndsWith('\''))))
  44. {
  45. return value[1..^1];
  46. }
  47. return value;
  48. }
  49. }

Powered by TurnKey Linux.