using System.Collections.Generic; using System.Diagnostics; using System.Linq; using WingServerCommon.Config.Parameters; namespace WingServerCommon.Config { /// /// This class will load a glabal server configuration /// public class ConfigurationManager { private static IEnumerable _parameters; /// /// The listening server gateway port /// /// public static string Host { get; private set; } /// /// Indicates the server will be deployed as distributed system /// /// public static bool IsDistributed { get; set; } /// /// Indicates the server is master /// /// public static bool IsMaster { get; set; } /// /// The master server url /// /// public static string MasterUrl { get; set; } /// /// Log settings from configuration file /// /// public static LogSettings? LogSettings { get; private set; } /// /// Email settings from configuration file /// /// public static EmailSettings? EmailSettings { get; private set; } /// /// Storage settings is from configuration file /// /// public static StorageSettings? StorageSettings { get; private set; } /// /// Remedical settings is from configuration file /// /// public static RemedicalSettings? RemedicalSettings { get; private set; } /// /// Notification Service Settings is from configuration file /// /// 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(); } /// /// Get parameter by specifed section name and parameter name /// /// public static T GetParammeter(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"); } /// /// Get value by section name and item key /// /// /// The parameter section name /// The parameter key /// The parameter default value /// public static T GetValue(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("Gateway", "Host").Value; IsDistributed = GetParammeter("General", "IsDistributed").Value; IsMaster = GetParammeter("General", "IsMaster").Value; MasterUrl = GetParammeter("General", "MasterUrl").Value; EmailSettings = new EmailSettings(); //TODO load from settings //TODO load log settings var debugMode = ConfigurationManager.GetParammeter("Log", "Debug").Value; var traceLevelString = ConfigurationManager.GetParammeter("Log", "Level").Value; if (!Enum.TryParse(traceLevelString, true, out TraceLevel traceLevel)) { traceLevel = TraceLevel.Info; } LogSettings = new LogSettings(debugMode, traceLevel); LoadRemedicalConfig(); LoadStorageConfig(); //TODO others } /// /// load remedical config /// private static void LoadRemedicalConfig() { RemedicalSettings = new RemedicalSettings() { IsUploadThumbnail = GetParammeter("Remedical", "IsUploadThumbnail").Value, }; } /// /// 加载 存储服务 配置 /// /// private static void LoadStorageConfig() { StorageSettings = new StorageSettings() { DefaultStorageServer = GetParammeter("Storage", "StorageServer").Value == "Vinno" ? StorageServerEnum.Vinno : StorageServerEnum.Tencent, CFSServerUrl = GetParammeter("Storage", "CFSServerUrl").Value, CDNServerUrl = GetParammeter("Storage", "CDNServerUrl").Value, Sercret = GetParammeter("Storage", "Sercret").Value, AppId = GetParammeter("Storage", "AppId").Value, }; } /// /// 重新设置cfs地址 /// /// public static void SetStorageCFSServerUrl(string cfsUrl) { StorageSettings.CFSServerUrl = cfsUrl; } } }