The next generation of the Teknik Services. Written in ASP.NET.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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