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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
  36. public bool Migrate { get { return _Migrate; } set { _Migrate = value; } }
  37. // Site Information
  38. public string Title { get { return _Title; } set { _Title = value; } }
  39. public string Description { get { return _Description; } set { _Description = value; } }
  40. public string Author { get { return _Author; } set { _Author = value; } }
  41. public string Host { get { return _Host; } set { _Host = value; } }
  42. public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } }
  43. public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } }
  44. public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } }
  45. public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } }
  46. // User Configuration
  47. public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } }
  48. // Contact Configuration
  49. public ContactConfig ContactConfig { get { return _ContactConfig; } set { _ContactConfig = value; } }
  50. // Mail Server Configuration
  51. public EmailConfig EmailConfig { get { return _EmailConfig; } set { _EmailConfig = value; } }
  52. // Git Service Configuration
  53. public GitConfig GitConfig { get { return _GitConfig; } set { _GitConfig = value; } }
  54. // Blog Configuration
  55. public BlogConfig BlogConfig { get { return _BlogConfig; } set { _BlogConfig = value; } }
  56. // Upload Configuration
  57. public UploadConfig UploadConfig { get { return _UploadConfig; } set { _UploadConfig = value; } }
  58. // Paste Configuration
  59. public PasteConfig PasteConfig { get { return _PasteConfig; } set { _PasteConfig = value; } }
  60. // API Configuration
  61. public ApiConfig ApiConfig { get { return _ApiConfig; } set { _ApiConfig = value; } }
  62. // Podcast Configuration
  63. public PodcastConfig PodcastConfig { get { return _PodcastConfig; } set { _PodcastConfig = value; } }
  64. // Stream Configuration
  65. public StreamConfig StreamConfig { get { return _StreamConfig; } set { _StreamConfig = value; } }
  66. // Shortener Configuration
  67. public ShortenerConfig ShortenerConfig { get { return _ShortenerConfig; } set { _ShortenerConfig = value; } }
  68. // Database Configuration
  69. public DatabaseConfig DatabaseConfig { get { return _DatabaseConfig; } set { _DatabaseConfig = value; } }
  70. public Config()
  71. {
  72. _ConfigRWLock = new ReaderWriterLockSlim();
  73. _ConfigFileRWLock = new ReaderWriterLockSlim();
  74. _JsonSettings = new JsonSerializerSettings();
  75. _JsonSettings.Formatting = Formatting.Indented;
  76. SetDefaults();
  77. }
  78. public void SetDefaults()
  79. {
  80. DevEnvironment = false;
  81. Migrate = false;
  82. Title = string.Empty;
  83. Description = string.Empty;
  84. Author = string.Empty;
  85. Host = string.Empty;
  86. SupportEmail = string.Empty;
  87. BitcoinAddress = string.Empty;
  88. Salt1 = string.Empty;
  89. Salt2 = string.Empty;
  90. UserConfig = new UserConfig();
  91. EmailConfig = new EmailConfig();
  92. ContactConfig = new ContactConfig();
  93. GitConfig = new GitConfig();
  94. BlogConfig = new BlogConfig();
  95. UploadConfig = new UploadConfig();
  96. PasteConfig = new PasteConfig();
  97. ApiConfig = new ApiConfig();
  98. PodcastConfig = new PodcastConfig();
  99. StreamConfig = new StreamConfig();
  100. ShortenerConfig = new ShortenerConfig();
  101. DatabaseConfig = new DatabaseConfig();
  102. }
  103. public static Config Deserialize(string text)
  104. {
  105. return JsonConvert.DeserializeObject<Config>(text);
  106. }
  107. public static string Serialize(Config config)
  108. {
  109. return JsonConvert.SerializeObject(config, Formatting.Indented);
  110. }
  111. public static Config Load()
  112. {
  113. Config config = new Config();
  114. string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
  115. if (!File.Exists(Path.Combine(path, "Config.json")))
  116. {
  117. Save(Path.Combine(path, "Config.json"), config);
  118. }
  119. else
  120. {
  121. string configContents = File.ReadAllText(Path.Combine(path, "Config.json"));
  122. config = Deserialize(configContents);
  123. }
  124. return config;
  125. }
  126. public static void Save(Config config)
  127. {
  128. string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
  129. Save(Path.Combine(path, "Config.json"), config);
  130. }
  131. public static void Save(string path, Config config)
  132. {
  133. if (!Directory.Exists(Path.GetDirectoryName(path)))
  134. {
  135. Directory.CreateDirectory(Path.GetDirectoryName(path));
  136. }
  137. string configContents = Config.Serialize(config);
  138. File.WriteAllText(path, configContents);
  139. }
  140. }
  141. }