ConfigurationManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using WingServerCommon.Config.Parameters;
  5. namespace WingServerCommon.Config
  6. {
  7. /// <summary>
  8. /// This class will load a glabal server configuration
  9. /// </summary>
  10. public class ConfigurationManager
  11. {
  12. private static IEnumerable<IParameter> _parameters;
  13. /// <summary>
  14. /// The listening server gateway port
  15. /// </summary>
  16. /// <value></value>
  17. public static string Host { get; private set; }
  18. /// <summary>
  19. /// Indicates the server will be deployed as distributed system
  20. /// </summary>
  21. /// <value></value>
  22. public static bool IsDistributed { get; set; }
  23. /// <summary>
  24. /// Indicates the server is master
  25. /// </summary>
  26. /// <value></value>
  27. public static bool IsMaster { get; set; }
  28. /// <summary>
  29. /// The master server url
  30. /// </summary>
  31. /// <value></value>
  32. public static string MasterUrl { get; set; }
  33. /// <summary>
  34. /// Log settings from configuration file
  35. /// </summary>
  36. /// <value></value>
  37. public static LogSettings? LogSettings { get; private set; }
  38. /// <summary>
  39. /// Email settings from configuration file
  40. /// </summary>
  41. /// <value></value>
  42. public static EmailSettings? EmailSettings { get; private set; }
  43. /// <summary>
  44. /// Storage settings is from configuration file
  45. /// </summary>
  46. /// <value></value>
  47. public static StorageSettings? StorageSettings { get; private set; }
  48. /// <summary>
  49. /// Remedical settings is from configuration file
  50. /// </summary>
  51. /// <value></value>
  52. public static RemedicalSettings? RemedicalSettings { get; private set; }
  53. /// <summary>
  54. /// Notification Service Settings is from configuration file
  55. /// </summary>
  56. /// <value></value>
  57. public static NotificationSettings? NotificationSettings { get; private set; }
  58. static ConfigurationManager()
  59. {
  60. var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
  61. _parameters = ParameterSerializer.Deserialize(configFilePath);
  62. LoadConfiguration();
  63. }
  64. public static void ReloadConfig()
  65. {
  66. var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
  67. _parameters = ParameterSerializer.Deserialize(configFilePath);
  68. LoadConfiguration();
  69. }
  70. /// <summary>
  71. /// Get parameter by specifed section name and parameter name
  72. /// </summary>
  73. /// <typeparam name="T"></typeparam>
  74. public static T GetParammeter<T>(string sectionName, string paramName) where T : class
  75. {
  76. var parameter = _parameters.FirstOrDefault(x => x.Section == sectionName && x.Name == paramName) as T;
  77. if (parameter != null)
  78. {
  79. return parameter;
  80. }
  81. throw new System.ArgumentException($"{sectionName} - {paramName} not set correct");
  82. }
  83. /// <summary>
  84. /// Get value by section name and item key
  85. /// </summary>
  86. /// <typeparam name="T"></typeparam>
  87. /// <param name="sectionName">The parameter section name</param>
  88. /// <param name="key">The parameter key</param>
  89. /// <param name="defaultValue">The parameter default value</param>
  90. /// <returns></returns>
  91. public static T GetValue<T>(string sectionName, string key, T defaultValue)
  92. {
  93. var t = typeof(T);
  94. var itemValue = _parameters.FirstOrDefault(q => q.Section == sectionName && q.Name == key);
  95. if (itemValue == null)
  96. {
  97. return defaultValue;
  98. }
  99. object value = null;
  100. var type = itemValue.GetType();
  101. if (type == typeof(StringParameter))
  102. {
  103. value = ((StringParameter)itemValue).Value;
  104. }
  105. if (type == typeof(IntParameter))
  106. {
  107. value = ((IntParameter)itemValue).Value.ToString();
  108. }
  109. if (type == typeof(BoolParameter))
  110. {
  111. value = ((BoolParameter)itemValue).Value.ToString();
  112. }
  113. if (type == typeof(EnumStringParameter))
  114. {
  115. value = ((EnumStringParameter)itemValue).Source;
  116. }
  117. if (t == typeof(bool) && (value?.ToString() == "0" || value?.ToString() == "1"))
  118. {
  119. return (T)(object)(value?.ToString() != "0");
  120. }
  121. return (T)Convert.ChangeType(value, t);
  122. }
  123. private static void LoadConfiguration()
  124. {
  125. Host = GetParammeter<StringParameter>("Gateway", "Host").Value;
  126. IsDistributed = GetParammeter<BoolParameter>("General", "IsDistributed").Value;
  127. IsMaster = GetParammeter<BoolParameter>("General", "IsMaster").Value;
  128. MasterUrl = GetParammeter<StringParameter>("General", "MasterUrl").Value;
  129. EmailSettings = new EmailSettings(); //TODO load from settings
  130. //TODO load log settings
  131. var debugMode = ConfigurationManager.GetParammeter<BoolParameter>("Log", "Debug").Value;
  132. var traceLevelString = ConfigurationManager.GetParammeter<StringParameter>("Log", "Level").Value;
  133. if (!Enum.TryParse(traceLevelString, true, out TraceLevel traceLevel))
  134. {
  135. traceLevel = TraceLevel.Info;
  136. }
  137. LogSettings = new LogSettings(debugMode, traceLevel);
  138. LoadRemedicalConfig();
  139. LoadStorageConfig();
  140. //TODO others
  141. }
  142. /// <summary>
  143. /// load remedical config
  144. /// </summary>
  145. private static void LoadRemedicalConfig()
  146. {
  147. RemedicalSettings = new RemedicalSettings()
  148. {
  149. IsUploadThumbnail = GetParammeter<BoolParameter>("Remedical", "IsUploadThumbnail").Value,
  150. };
  151. }
  152. /// <summary>
  153. /// 加载 存储服务 配置
  154. /// </summary>
  155. /// <returns></returns>
  156. private static void LoadStorageConfig()
  157. {
  158. StorageSettings = new StorageSettings()
  159. {
  160. DefaultStorageServer = GetParammeter<StringParameter>("Storage", "StorageServer").Value == "Vinno" ?
  161. StorageServerEnum.Vinno : StorageServerEnum.Tencent,
  162. CFSServerUrl = GetParammeter<StringParameter>("Storage", "CFSServerUrl").Value,
  163. CDNServerUrl = GetParammeter<StringParameter>("Storage", "CDNServerUrl").Value,
  164. Sercret = GetParammeter<StringParameter>("Storage", "Sercret").Value,
  165. AppId = GetParammeter<StringParameter>("Storage", "AppId").Value,
  166. };
  167. }
  168. /// <summary>
  169. /// 重新设置cfs地址
  170. /// </summary>
  171. /// <returns></returns>
  172. public static void SetStorageCFSServerUrl(string cfsUrl)
  173. {
  174. StorageSettings.CFSServerUrl = cfsUrl;
  175. }
  176. }
  177. }