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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 string _Title;
  15. private string _Description;
  16. private string _Author;
  17. private string _Host;
  18. private UserConfig _UserConfig;
  19. private ContactConfig _ContactConfig;
  20. private EmailConfig _EmailConfig;
  21. private GitConfig _GitConfig;
  22. private UploadConfig _UploadConfig;
  23. private PasteConfig _PasteConfig;
  24. private BlogConfig _BlogConfig;
  25. private ApiConfig _ApiConfig;
  26. private PodcastConfig _PodcastConfig;
  27. private string _SupportEmail;
  28. private string _BitcoinAddress;
  29. public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
  30. // Site Information
  31. public string Title { get { return _Title; } set { _Title = value; } }
  32. public string Description { get { return _Description; } set { _Description = value; } }
  33. public string Author { get { return _Author; } set { _Author = value; } }
  34. public string Host { get { return _Host; } set { _Host = value; } }
  35. public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } }
  36. public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } }
  37. // User Configuration
  38. public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } }
  39. // Contact Configuration
  40. public ContactConfig ContactConfig { get { return _ContactConfig; } set { _ContactConfig = value; } }
  41. // Mail Server Configuration
  42. public EmailConfig EmailConfig { get { return _EmailConfig; } set { _EmailConfig = value; } }
  43. // Git Service Configuration
  44. public GitConfig GitConfig { get { return _GitConfig; } set { _GitConfig = value; } }
  45. // Blog Configuration
  46. public BlogConfig BlogConfig { get { return _BlogConfig; } set { _BlogConfig = value; } }
  47. // Upload Configuration
  48. public UploadConfig UploadConfig { get { return _UploadConfig; } set { _UploadConfig = value; } }
  49. // Paste Configuration
  50. public PasteConfig PasteConfig { get { return _PasteConfig; } set { _PasteConfig = value; } }
  51. // API Configuration
  52. public ApiConfig ApiConfig { get { return _ApiConfig; } set { _ApiConfig = value; } }
  53. // Podcast Configuration
  54. public PodcastConfig PodcastConfig { get { return _PodcastConfig; } set { _PodcastConfig = value; } }
  55. public Config()
  56. {
  57. _ConfigRWLock = new ReaderWriterLockSlim();
  58. _ConfigFileRWLock = new ReaderWriterLockSlim();
  59. _JsonSettings = new JsonSerializerSettings();
  60. _JsonSettings.Formatting = Formatting.Indented;
  61. SetDefaults();
  62. }
  63. public void SetDefaults()
  64. {
  65. DevEnvironment = false;
  66. Title = string.Empty;
  67. Description = string.Empty;
  68. Author = string.Empty;
  69. Host = string.Empty;
  70. UserConfig = new UserConfig();
  71. EmailConfig = new EmailConfig();
  72. ContactConfig = new ContactConfig();
  73. GitConfig = new GitConfig();
  74. BlogConfig = new BlogConfig();
  75. UploadConfig = new UploadConfig();
  76. PasteConfig = new PasteConfig();
  77. ApiConfig = new ApiConfig();
  78. PodcastConfig = new PodcastConfig();
  79. SupportEmail = string.Empty;
  80. BitcoinAddress = string.Empty;
  81. }
  82. public static Config Deserialize(string text)
  83. {
  84. return JsonConvert.DeserializeObject<Config>(text);
  85. }
  86. public static string Serialize(Config config)
  87. {
  88. return JsonConvert.SerializeObject(config, Formatting.Indented);
  89. }
  90. public static Config Load()
  91. {
  92. Config config = new Config();
  93. string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
  94. if (!File.Exists(Path.Combine(path, "Config.json")))
  95. {
  96. Config.Save(Path.Combine(path, "Config.json"), config);
  97. }
  98. else
  99. {
  100. string configContents = File.ReadAllText(Path.Combine(path, "Config.json"));
  101. config = Config.Deserialize(configContents);
  102. }
  103. return config;
  104. }
  105. public static void Save(string path, Config config)
  106. {
  107. if (!Directory.Exists(Path.GetDirectoryName(path)))
  108. {
  109. Directory.CreateDirectory(Path.GetDirectoryName(path));
  110. }
  111. string configContents = Config.Serialize(config);
  112. File.WriteAllText(path, configContents);
  113. }
  114. }
  115. }