ConfigManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Common.Global;
  2. using Database.Database;
  3. using Microsoft.Identity.Client;
  4. namespace Esim.Apis.Singleton
  5. {
  6. public class ConfigManager
  7. {
  8. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ConfigManager));
  9. private ModelContext dbContext;
  10. private List<Config> appConfigs = new List<Config>();
  11. private ConfigManager()
  12. {
  13. dbContext = new DbHelper().GetDbContext(GlobalConfig.Configuration!);
  14. log.Debug("ConfigManager initialized");
  15. }
  16. private static readonly object lockObj = new object();
  17. private static ConfigManager? instance;
  18. public static ConfigManager Instance => instance ??= new ConfigManager();
  19. public void Initialize()
  20. {
  21. appConfigs = dbContext.Configs.ToList();
  22. log.Debug("ConfigManager initialized");
  23. }
  24. public string GetConfigWebValue(string configName, string lang = "lo")
  25. {
  26. var config = appConfigs.FirstOrDefault(c => c.Name == configName && c.Type == "WEB");
  27. if (config != null)
  28. {
  29. log.Debug($"Config found: {configName} = {config.ValueLocal}");
  30. return GetValueByLang(config, lang);
  31. }
  32. log.Warn($"Config not found: {configName}");
  33. return string.Empty;
  34. }
  35. public string GetConfigSmsValue(string configName, string lang = "lo")
  36. {
  37. var config = appConfigs.FirstOrDefault(c => c.Name == configName && c.Type == "SMS");
  38. if (config != null)
  39. {
  40. log.Debug($"Config found: {configName} = {config.ValueLocal}");
  41. return GetValueByLang(config, lang);
  42. }
  43. log.Warn($"Config not found: {configName}");
  44. return string.Empty;
  45. }
  46. public string GetConfigAppValue(string configName, string lang = "lo")
  47. {
  48. var config = appConfigs.FirstOrDefault(c => c.Name == configName && c.Type == "APP");
  49. if (config != null)
  50. {
  51. log.Debug($"Config found: {configName} = {config.ValueLocal}");
  52. return GetValueByLang(config, lang);
  53. }
  54. log.Warn($"Config not found: {configName}");
  55. return string.Empty;
  56. }
  57. /// <summary>
  58. /// Get config value by language: vi=Vietnamese, en=English, lo=Lao (default)
  59. /// </summary>
  60. private string GetValueByLang(Config config, string lang)
  61. {
  62. return lang switch
  63. {
  64. "vi" => config.Value ?? config.ValueLocal ?? "",
  65. "en" => config.ValueGlobal ?? config.ValueLocal ?? "",
  66. _ => config.ValueLocal ?? "" // Default: Lao
  67. };
  68. }
  69. // loop 5p to refresh
  70. public void RefreshConfigs()
  71. {
  72. while (true)
  73. {
  74. try
  75. {
  76. log.Debug($"ConfigManager refreshing...");
  77. appConfigs = dbContext.Configs.ToList();
  78. log.Debug("ConfigManager refreshed");
  79. }
  80. catch (Exception ex)
  81. {
  82. log.Error("Error refreshing configs", ex);
  83. }
  84. Thread.Sleep(5 * 60 * 1000); // 5 minutes
  85. }
  86. }
  87. }
  88. }