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

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