123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using WingServerCommon.Config.Parameters;
- namespace WingServerCommon.Config
- {
- /// <summary>
- /// This class will load a glabal server configuration
- /// </summary>
- public class ConfigurationManager
- {
- private static IEnumerable<IParameter> _parameters;
- /// <summary>
- /// The listening server gateway port
- /// </summary>
- /// <value></value>
- public static string Host { get; private set; }
- /// <summary>
- /// Indicates the server will be deployed as distributed system
- /// </summary>
- /// <value></value>
- public static bool IsDistributed { get; set; }
- /// <summary>
- /// Indicates the server is master
- /// </summary>
- /// <value></value>
- public static bool IsMaster { get; set; }
- /// <summary>
- /// The master server url
- /// </summary>
- /// <value></value>
- public static string MasterUrl { get; set; }
- /// <summary>
- /// Log settings from configuration file
- /// </summary>
- /// <value></value>
- public static LogSettings? LogSettings { get; private set; }
- /// <summary>
- /// Email settings from configuration file
- /// </summary>
- /// <value></value>
- public static EmailSettings? EmailSettings { get; private set; }
- /// <summary>
- /// Storage settings is from configuration file
- /// </summary>
- /// <value></value>
- public static StorageSettings? StorageSettings { get; private set; }
- /// <summary>
- /// Remedical settings is from configuration file
- /// </summary>
- /// <value></value>
- public static RemedicalSettings? RemedicalSettings { get; private set; }
- /// <summary>
- /// Notification Service Settings is from configuration file
- /// </summary>
- /// <value></value>
- public static NotificationSettings? NotificationSettings { get; private set; }
- static ConfigurationManager()
- {
- var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
- _parameters = ParameterSerializer.Deserialize(configFilePath);
- LoadConfiguration();
- }
- public static void ReloadConfig()
- {
- var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
- _parameters = ParameterSerializer.Deserialize(configFilePath);
- LoadConfiguration();
- }
- /// <summary>
- /// Get parameter by specifed section name and parameter name
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public static T GetParammeter<T>(string sectionName, string paramName) where T : class
- {
- var parameter = _parameters.FirstOrDefault(x => x.Section == sectionName && x.Name == paramName) as T;
- if (parameter != null)
- {
- return parameter;
- }
- throw new System.ArgumentException($"{sectionName} - {paramName} not set correct");
- }
- /// <summary>
- /// Get value by section name and item key
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="sectionName">The parameter section name</param>
- /// <param name="key">The parameter key</param>
- /// <param name="defaultValue">The parameter default value</param>
- /// <returns></returns>
- public static T GetValue<T>(string sectionName, string key, T defaultValue)
- {
- var t = typeof(T);
- var itemValue = _parameters.FirstOrDefault(q => q.Section == sectionName && q.Name == key);
- if (itemValue == null)
- {
- return defaultValue;
- }
- object value = null;
- var type = itemValue.GetType();
- if (type == typeof(StringParameter))
- {
- value = ((StringParameter)itemValue).Value;
- }
- if (type == typeof(IntParameter))
- {
- value = ((IntParameter)itemValue).Value.ToString();
- }
- if (type == typeof(BoolParameter))
- {
- value = ((BoolParameter)itemValue).Value.ToString();
- }
- if (type == typeof(EnumStringParameter))
- {
- value = ((EnumStringParameter)itemValue).Source;
- }
- if (t == typeof(bool) && (value?.ToString() == "0" || value?.ToString() == "1"))
- {
- return (T)(object)(value?.ToString() != "0");
- }
- return (T)Convert.ChangeType(value, t);
- }
- private static void LoadConfiguration()
- {
- Host = GetParammeter<StringParameter>("Gateway", "Host").Value;
- IsDistributed = GetParammeter<BoolParameter>("General", "IsDistributed").Value;
- IsMaster = GetParammeter<BoolParameter>("General", "IsMaster").Value;
- MasterUrl = GetParammeter<StringParameter>("General", "MasterUrl").Value;
- EmailSettings = new EmailSettings(); //TODO load from settings
- //TODO load log settings
- var debugMode = ConfigurationManager.GetParammeter<BoolParameter>("Log", "Debug").Value;
- var traceLevelString = ConfigurationManager.GetParammeter<StringParameter>("Log", "Level").Value;
- if (!Enum.TryParse(traceLevelString, true, out TraceLevel traceLevel))
- {
- traceLevel = TraceLevel.Info;
- }
- LogSettings = new LogSettings(debugMode, traceLevel);
- LoadRemedicalConfig();
- LoadStorageConfig();
- //TODO others
- }
- /// <summary>
- /// load remedical config
- /// </summary>
- private static void LoadRemedicalConfig()
- {
- RemedicalSettings = new RemedicalSettings()
- {
- IsUploadThumbnail = GetParammeter<BoolParameter>("Remedical", "IsUploadThumbnail").Value,
- };
- }
- /// <summary>
- /// 加载 存储服务 配置
- /// </summary>
- /// <returns></returns>
- private static void LoadStorageConfig()
- {
- StorageSettings = new StorageSettings()
- {
- DefaultStorageServer = GetParammeter<StringParameter>("Storage", "StorageServer").Value == "Vinno" ?
- StorageServerEnum.Vinno : StorageServerEnum.Tencent,
- CFSServerUrl = GetParammeter<StringParameter>("Storage", "CFSServerUrl").Value,
- CDNServerUrl = GetParammeter<StringParameter>("Storage", "CDNServerUrl").Value,
- Sercret = GetParammeter<StringParameter>("Storage", "Sercret").Value,
- AppId = GetParammeter<StringParameter>("Storage", "AppId").Value,
- };
- }
- /// <summary>
- /// 重新设置cfs地址
- /// </summary>
- /// <returns></returns>
- public static void SetStorageCFSServerUrl(string cfsUrl)
- {
- StorageSettings.CFSServerUrl = cfsUrl;
- }
- }
- }
|