The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Config.cs 7.2KB

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