2015-07-16 23:54:53 +02:00
|
|
|
|
using System;
|
2015-11-23 07:55:24 +01:00
|
|
|
|
using System.IO;
|
2015-07-16 23:54:53 +02:00
|
|
|
|
using System.Threading;
|
2015-11-23 07:55:24 +01:00
|
|
|
|
using System.Web;
|
2015-07-16 23:54:53 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
2015-12-21 09:58:09 +01:00
|
|
|
|
namespace Teknik.Configuration
|
2015-07-16 23:54:53 +02:00
|
|
|
|
{
|
|
|
|
|
public class Config
|
|
|
|
|
{
|
|
|
|
|
private ReaderWriterLockSlim _ConfigRWLock;
|
|
|
|
|
private ReaderWriterLockSlim _ConfigFileRWLock;
|
|
|
|
|
private JsonSerializerSettings _JsonSettings;
|
|
|
|
|
|
2015-12-21 09:58:09 +01:00
|
|
|
|
private bool _DevEnvironment;
|
2016-02-06 01:48:00 +01:00
|
|
|
|
private bool _Migrate;
|
2015-12-21 09:58:09 +01:00
|
|
|
|
private string _Title;
|
|
|
|
|
private string _Description;
|
|
|
|
|
private string _Author;
|
|
|
|
|
private string _Host;
|
2016-01-29 08:08:02 +01:00
|
|
|
|
private string _SupportEmail;
|
|
|
|
|
private string _BitcoinAddress;
|
|
|
|
|
private string _Salt1;
|
|
|
|
|
private string _Salt2;
|
2016-01-29 00:24:12 +01:00
|
|
|
|
private UserConfig _UserConfig;
|
|
|
|
|
private ContactConfig _ContactConfig;
|
2016-01-28 08:43:41 +01:00
|
|
|
|
private EmailConfig _EmailConfig;
|
2016-01-29 00:24:12 +01:00
|
|
|
|
private GitConfig _GitConfig;
|
2015-12-21 09:58:09 +01:00
|
|
|
|
private UploadConfig _UploadConfig;
|
2016-01-23 01:44:07 +01:00
|
|
|
|
private PasteConfig _PasteConfig;
|
2015-12-21 09:58:09 +01:00
|
|
|
|
private BlogConfig _BlogConfig;
|
2016-01-21 23:47:07 +01:00
|
|
|
|
private ApiConfig _ApiConfig;
|
2016-01-25 07:31:01 +01:00
|
|
|
|
private PodcastConfig _PodcastConfig;
|
2016-02-03 00:02:45 +01:00
|
|
|
|
private StreamConfig _StreamConfig;
|
2016-01-29 22:40:27 +01:00
|
|
|
|
private DatabaseConfig _DatabaseConfig;
|
2015-07-16 23:54:53 +02:00
|
|
|
|
|
2015-12-16 21:46:54 +01:00
|
|
|
|
public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
|
2016-02-06 01:48:00 +01:00
|
|
|
|
public bool Migrate { get { return _Migrate; } set { _Migrate = value; } }
|
2015-12-16 21:46:54 +01:00
|
|
|
|
|
2015-12-21 05:56:06 +01:00
|
|
|
|
// Site Information
|
2015-12-16 21:46:54 +01:00
|
|
|
|
public string Title { get { return _Title; } set { _Title = value; } }
|
|
|
|
|
public string Description { get { return _Description; } set { _Description = value; } }
|
|
|
|
|
public string Author { get { return _Author; } set { _Author = value; } }
|
|
|
|
|
public string Host { get { return _Host; } set { _Host = value; } }
|
2016-01-29 00:24:12 +01:00
|
|
|
|
public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } }
|
|
|
|
|
public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } }
|
2016-01-29 08:08:02 +01:00
|
|
|
|
public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } }
|
|
|
|
|
public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } }
|
2016-01-29 00:24:12 +01:00
|
|
|
|
|
|
|
|
|
// User Configuration
|
|
|
|
|
public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } }
|
2015-12-16 21:46:54 +01:00
|
|
|
|
|
2016-01-28 08:43:41 +01:00
|
|
|
|
// Contact Configuration
|
|
|
|
|
public ContactConfig ContactConfig { get { return _ContactConfig; } set { _ContactConfig = value; } }
|
|
|
|
|
|
2016-01-25 07:31:01 +01:00
|
|
|
|
// Mail Server Configuration
|
2016-01-28 08:43:41 +01:00
|
|
|
|
public EmailConfig EmailConfig { get { return _EmailConfig; } set { _EmailConfig = value; } }
|
2015-12-16 21:46:54 +01:00
|
|
|
|
|
2016-01-29 00:24:12 +01:00
|
|
|
|
// Git Service Configuration
|
|
|
|
|
public GitConfig GitConfig { get { return _GitConfig; } set { _GitConfig = value; } }
|
2015-07-16 23:54:53 +02:00
|
|
|
|
|
2016-01-25 07:31:01 +01:00
|
|
|
|
// Blog Configuration
|
|
|
|
|
public BlogConfig BlogConfig { get { return _BlogConfig; } set { _BlogConfig = value; } }
|
2015-12-21 09:58:09 +01:00
|
|
|
|
|
|
|
|
|
// Upload Configuration
|
2016-01-25 07:31:01 +01:00
|
|
|
|
public UploadConfig UploadConfig { get { return _UploadConfig; } set { _UploadConfig = value; } }
|
2015-12-17 23:14:11 +01:00
|
|
|
|
|
2016-01-23 01:44:07 +01:00
|
|
|
|
// Paste Configuration
|
2016-01-25 07:31:01 +01:00
|
|
|
|
public PasteConfig PasteConfig { get { return _PasteConfig; } set { _PasteConfig = value; } }
|
2016-01-23 01:44:07 +01:00
|
|
|
|
|
2016-01-25 07:31:01 +01:00
|
|
|
|
// API Configuration
|
|
|
|
|
public ApiConfig ApiConfig { get { return _ApiConfig; } set { _ApiConfig = value; } }
|
|
|
|
|
|
|
|
|
|
// Podcast Configuration
|
|
|
|
|
public PodcastConfig PodcastConfig { get { return _PodcastConfig; } set { _PodcastConfig = value; } }
|
2016-01-21 23:47:07 +01:00
|
|
|
|
|
2016-02-03 00:02:45 +01:00
|
|
|
|
// Stream Configuration
|
|
|
|
|
public StreamConfig StreamConfig { get { return _StreamConfig; } set { _StreamConfig = value; } }
|
|
|
|
|
|
2016-01-29 22:40:27 +01:00
|
|
|
|
// Database Configuration
|
|
|
|
|
public DatabaseConfig DatabaseConfig { get { return _DatabaseConfig; } set { _DatabaseConfig = value; } }
|
|
|
|
|
|
2015-07-16 23:54:53 +02:00
|
|
|
|
public Config()
|
|
|
|
|
{
|
|
|
|
|
_ConfigRWLock = new ReaderWriterLockSlim();
|
|
|
|
|
_ConfigFileRWLock = new ReaderWriterLockSlim();
|
|
|
|
|
_JsonSettings = new JsonSerializerSettings();
|
|
|
|
|
_JsonSettings.Formatting = Formatting.Indented;
|
|
|
|
|
|
|
|
|
|
SetDefaults();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetDefaults()
|
|
|
|
|
{
|
2015-12-17 23:14:11 +01:00
|
|
|
|
DevEnvironment = false;
|
2016-02-06 01:48:00 +01:00
|
|
|
|
Migrate = false;
|
2015-12-21 09:58:09 +01:00
|
|
|
|
Title = string.Empty;
|
|
|
|
|
Description = string.Empty;
|
|
|
|
|
Author = string.Empty;
|
|
|
|
|
Host = string.Empty;
|
2016-01-29 08:08:02 +01:00
|
|
|
|
SupportEmail = string.Empty;
|
|
|
|
|
BitcoinAddress = string.Empty;
|
|
|
|
|
Salt1 = string.Empty;
|
|
|
|
|
Salt2 = string.Empty;
|
2016-01-29 00:24:12 +01:00
|
|
|
|
UserConfig = new UserConfig();
|
2016-01-28 08:43:41 +01:00
|
|
|
|
EmailConfig = new EmailConfig();
|
|
|
|
|
ContactConfig = new ContactConfig();
|
2016-01-29 00:24:12 +01:00
|
|
|
|
GitConfig = new GitConfig();
|
2015-12-21 09:58:09 +01:00
|
|
|
|
BlogConfig = new BlogConfig();
|
|
|
|
|
UploadConfig = new UploadConfig();
|
2016-01-23 01:44:07 +01:00
|
|
|
|
PasteConfig = new PasteConfig();
|
2016-01-21 23:47:07 +01:00
|
|
|
|
ApiConfig = new ApiConfig();
|
2016-01-25 07:31:01 +01:00
|
|
|
|
PodcastConfig = new PodcastConfig();
|
2016-02-03 00:02:45 +01:00
|
|
|
|
StreamConfig = new StreamConfig();
|
2016-01-29 22:40:27 +01:00
|
|
|
|
DatabaseConfig = new DatabaseConfig();
|
2015-07-16 23:54:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Config Deserialize(string text)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<Config>(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Serialize(Config config)
|
|
|
|
|
{
|
2015-12-24 07:36:33 +01:00
|
|
|
|
return JsonConvert.SerializeObject(config, Formatting.Indented);
|
2015-07-16 23:54:53 +02:00
|
|
|
|
}
|
2015-11-23 07:55:24 +01:00
|
|
|
|
|
|
|
|
|
public static Config Load()
|
|
|
|
|
{
|
2015-11-23 20:17:57 +01:00
|
|
|
|
Config config = new Config();
|
2015-12-16 23:52:59 +01:00
|
|
|
|
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
|
2015-12-24 07:36:33 +01:00
|
|
|
|
if (!File.Exists(Path.Combine(path, "Config.json")))
|
|
|
|
|
{
|
2016-01-30 06:26:44 +01:00
|
|
|
|
Save(Path.Combine(path, "Config.json"), config);
|
2015-12-24 07:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2015-11-23 20:17:57 +01:00
|
|
|
|
{
|
2015-12-16 23:52:59 +01:00
|
|
|
|
string configContents = File.ReadAllText(Path.Combine(path, "Config.json"));
|
2016-01-30 06:26:44 +01:00
|
|
|
|
config = Deserialize(configContents);
|
2015-11-23 20:17:57 +01:00
|
|
|
|
}
|
2015-11-23 07:55:24 +01:00
|
|
|
|
return config;
|
|
|
|
|
}
|
2015-12-24 07:36:33 +01:00
|
|
|
|
|
2016-01-30 06:26:44 +01:00
|
|
|
|
public static void Save(Config config)
|
|
|
|
|
{
|
|
|
|
|
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
|
|
|
|
|
Save(Path.Combine(path, "Config.json"), config);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-24 07:36:33 +01:00
|
|
|
|
public static void Save(string path, Config config)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(path)))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
}
|
|
|
|
|
string configContents = Config.Serialize(config);
|
|
|
|
|
File.WriteAllText(path, configContents);
|
|
|
|
|
}
|
2015-07-16 23:54:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|