The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Config.cs 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using Newtonsoft.Json;
  5. using Teknik.Utilities;
  6. using Teknik.Utilities.Cryptography;
  7. namespace Teknik.Configuration
  8. {
  9. public class Config
  10. {
  11. private static Config _Config { get; set; }
  12. private static string _FileHash { get; set; }
  13. private ReaderWriterLockSlim _ConfigRWLock;
  14. private ReaderWriterLockSlim _ConfigFileRWLock;
  15. private JsonSerializerSettings _JsonSettings;
  16. private bool _DevEnvironment;
  17. private bool _Migrate;
  18. private bool _UseCdn;
  19. private string _Title;
  20. private string _Description;
  21. private string _Author;
  22. private string _Host;
  23. private string _SupportEmail;
  24. private string _NoReplyEmail;
  25. private string _BitcoinAddress;
  26. private string _Salt1;
  27. private string _Salt2;
  28. private string _CdnHost;
  29. private UserConfig _UserConfig;
  30. private ContactConfig _ContactConfig;
  31. private EmailConfig _EmailConfig;
  32. private GitConfig _GitConfig;
  33. private UploadConfig _UploadConfig;
  34. private PasteConfig _PasteConfig;
  35. private BlogConfig _BlogConfig;
  36. private ApiConfig _ApiConfig;
  37. private PodcastConfig _PodcastConfig;
  38. private StreamConfig _StreamConfig;
  39. private ShortenerConfig _ShortenerConfig;
  40. private VaultConfig _VaultConfig;
  41. private StatusConfig _StatusConfig;
  42. private LoggingConfig _LoggingConfig;
  43. private PiwikConfig _PiwikConfig;
  44. private IRCConfig _IRCConfig;
  45. public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
  46. public bool Migrate { get { return _Migrate; } set { _Migrate = value; } }
  47. public bool UseCdn { get { return _UseCdn; } set { _UseCdn = value; } }
  48. // Site Information
  49. public string Title { get { return _Title; } set { _Title = value; } }
  50. public string Description { get { return _Description; } set { _Description = value; } }
  51. public string Author { get { return _Author; } set { _Author = value; } }
  52. public string Host { get { return _Host; } set { _Host = value; } }
  53. public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } }
  54. public string NoReplyEmail { get { return _NoReplyEmail; } set { _NoReplyEmail = value; } }
  55. public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } }
  56. public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } }
  57. public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } }
  58. public string CdnHost { get { return _CdnHost; } set { _CdnHost = value; } }
  59. // User Configuration
  60. public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } }
  61. // Contact Configuration
  62. public ContactConfig ContactConfig { get { return _ContactConfig; } set { _ContactConfig = value; } }
  63. // Mail Server Configuration
  64. public EmailConfig EmailConfig { get { return _EmailConfig; } set { _EmailConfig = value; } }
  65. // Git Service Configuration
  66. public GitConfig GitConfig { get { return _GitConfig; } set { _GitConfig = value; } }
  67. // Blog Configuration
  68. public BlogConfig BlogConfig { get { return _BlogConfig; } set { _BlogConfig = value; } }
  69. // Upload Configuration
  70. public UploadConfig UploadConfig { get { return _UploadConfig; } set { _UploadConfig = value; } }
  71. // Paste Configuration
  72. public PasteConfig PasteConfig { get { return _PasteConfig; } set { _PasteConfig = value; } }
  73. // API Configuration
  74. public ApiConfig ApiConfig { get { return _ApiConfig; } set { _ApiConfig = value; } }
  75. // Podcast Configuration
  76. public PodcastConfig PodcastConfig { get { return _PodcastConfig; } set { _PodcastConfig = value; } }
  77. // Stream Configuration
  78. public StreamConfig StreamConfig { get { return _StreamConfig; } set { _StreamConfig = value; } }
  79. // Shortener Configuration
  80. public ShortenerConfig ShortenerConfig { get { return _ShortenerConfig; } set { _ShortenerConfig = value; } }
  81. // Vault Configuration
  82. public VaultConfig VaultConfig { get { return _VaultConfig; } set { _VaultConfig = value; } }
  83. // Status Configuration
  84. public StatusConfig StatusConfig { get { return _StatusConfig; } set { _StatusConfig = value; } }
  85. // Logging Configuration
  86. public LoggingConfig LoggingConfig { get { return _LoggingConfig; } set { _LoggingConfig = value; } }
  87. // Piwik Configuration
  88. public PiwikConfig PiwikConfig { get { return _PiwikConfig; } set { _PiwikConfig = value; } }
  89. // Piwik Configuration
  90. public IRCConfig IRCConfig { get { return _IRCConfig; } set { _IRCConfig = value; } }
  91. public Config()
  92. {
  93. _ConfigRWLock = new ReaderWriterLockSlim();
  94. _ConfigFileRWLock = new ReaderWriterLockSlim();
  95. _JsonSettings = new JsonSerializerSettings();
  96. _JsonSettings.Formatting = Formatting.Indented;
  97. SetDefaults();
  98. }
  99. public void SetDefaults()
  100. {
  101. DevEnvironment = false;
  102. Migrate = false;
  103. UseCdn = false;
  104. Title = string.Empty;
  105. Description = string.Empty;
  106. Author = string.Empty;
  107. Host = string.Empty;
  108. SupportEmail = string.Empty;
  109. NoReplyEmail = string.Empty;
  110. BitcoinAddress = string.Empty;
  111. Salt1 = string.Empty;
  112. Salt2 = string.Empty;
  113. CdnHost = string.Empty;
  114. UserConfig = new UserConfig();
  115. EmailConfig = new EmailConfig();
  116. ContactConfig = new ContactConfig();
  117. GitConfig = new GitConfig();
  118. BlogConfig = new BlogConfig();
  119. UploadConfig = new UploadConfig();
  120. PasteConfig = new PasteConfig();
  121. ApiConfig = new ApiConfig();
  122. PodcastConfig = new PodcastConfig();
  123. StreamConfig = new StreamConfig();
  124. ShortenerConfig = new ShortenerConfig();
  125. VaultConfig = new VaultConfig();
  126. StatusConfig = new StatusConfig();
  127. LoggingConfig = new LoggingConfig();
  128. PiwikConfig = new PiwikConfig();
  129. IRCConfig = new IRCConfig();
  130. }
  131. public static Config Deserialize(string text)
  132. {
  133. return JsonConvert.DeserializeObject<Config>(text);
  134. }
  135. public static string Serialize(Config config)
  136. {
  137. return JsonConvert.SerializeObject(config, Formatting.Indented);
  138. }
  139. public static Config Load()
  140. {
  141. string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
  142. string newHash = string.Empty;
  143. if (File.Exists(Path.Combine(path, "Config.json")))
  144. {
  145. newHash = MD5.FileHash(Path.Combine(path, "Config.json"));
  146. }
  147. if (_Config == null || _FileHash == null || newHash != _FileHash)
  148. {
  149. _Config = Load(path);
  150. _FileHash = newHash;
  151. }
  152. return _Config;
  153. }
  154. public static Config Load(string path)
  155. {
  156. Config config = new Config();
  157. if (!File.Exists(Path.Combine(path, "Config.json")))
  158. {
  159. Save(Path.Combine(path, "Config.json"), config);
  160. }
  161. else
  162. {
  163. string configContents = File.ReadAllText(Path.Combine(path, "Config.json"));
  164. config = Deserialize(configContents);
  165. }
  166. return config;
  167. }
  168. public static void Save(Config config)
  169. {
  170. string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
  171. Save(Path.Combine(path, "Config.json"), config);
  172. }
  173. public static void Save(string path, Config config)
  174. {
  175. if (!Directory.Exists(Path.GetDirectoryName(path)))
  176. {
  177. Directory.CreateDirectory(Path.GetDirectoryName(path));
  178. }
  179. string configContents = Config.Serialize(config);
  180. File.WriteAllText(path, configContents);
  181. }
  182. }
  183. }