ConfigAdapter.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Runtime.CompilerServices;
  5. namespace Rac.Tools
  6. {
  7. internal class ConfigAdapter
  8. {
  9. private readonly Dictionary<string, string> _dicConf;
  10. public ConfigAdapter(Dictionary<string, string> dicConf)
  11. {
  12. _dicConf = dicConf;
  13. }
  14. private string GetValue([CallerMemberName] string confKey = "")
  15. {
  16. return _dicConf.ContainsKey(confKey)
  17. ? _dicConf[confKey]
  18. : null;
  19. }
  20. private string[] GetValues([CallerMemberName] string confKey = "")
  21. {
  22. // ReSharper disable once ExplicitCallerInfoArgument
  23. return (GetValue(confKey) ?? "").Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
  24. }
  25. private int GetInt32Value([CallerMemberName] string confKey = "")
  26. {
  27. // ReSharper disable once ExplicitCallerInfoArgument
  28. var value = GetValue(confKey);
  29. if (int.TryParse(value, out var int32Value))
  30. {
  31. return int32Value;
  32. }
  33. throw new ConfigurationErrorsException($"Bad config {confKey}");
  34. }
  35. public string HomeUrl => GetValue();
  36. public string[] HostsInclude => GetValues();
  37. public string[] UrlPrefixExclude => GetValues();
  38. public int Parallel => GetInt32Value();
  39. public int OwsPort => GetInt32Value();
  40. public string DefaultCharset => GetValue();
  41. }
  42. }