Lidarr/NzbDrone/Providers/ConfigProvider.cs

149 lines
4.4 KiB
C#
Raw Normal View History

2010-10-15 07:10:44 +00:00
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
2011-10-07 06:36:04 +00:00
using System.Xml.XPath;
2010-10-15 07:10:44 +00:00
using NLog;
using NLog.Config;
using Ninject;
2010-10-15 07:10:44 +00:00
namespace NzbDrone.Providers
2010-10-15 07:10:44 +00:00
{
2011-10-07 06:36:04 +00:00
public class ConfigProvider
2010-10-15 07:10:44 +00:00
{
private readonly EnviromentProvider _enviromentProvider;
2011-10-07 06:43:35 +00:00
private static readonly Logger Logger = LogManager.GetLogger("Host.ConfigProvider");
2011-04-10 02:44:01 +00:00
[Inject]
public ConfigProvider(EnviromentProvider enviromentProvider)
2010-10-15 07:10:44 +00:00
{
_enviromentProvider = enviromentProvider;
}
2010-10-15 07:10:44 +00:00
public ConfigProvider()
{
2010-10-15 07:10:44 +00:00
}
public virtual int PortNumber
2010-10-15 07:10:44 +00:00
{
get { return GetValueInt("Port"); }
}
2011-10-07 06:36:04 +00:00
public virtual bool LaunchBrowser
{
get { return GetValueBoolean("LaunchBrowser"); }
}
public virtual string IISDirectory
{
get { return Path.Combine(_enviromentProvider.ApplicationPath, "IISExpress"); }
}
public virtual string IISExePath
{
get { return Path.Combine(IISDirectory, "iisexpress.exe"); }
}
public virtual string IISConfigPath
{
get { return Path.Combine(IISDirectory, "AppServer", "applicationhost.config"); }
2010-10-15 07:10:44 +00:00
}
public virtual string AppDataDirectory
{
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web", "App_Data"); }
}
public virtual string ConfigFile
2011-10-07 06:36:04 +00:00
{
get { return Path.Combine(AppDataDirectory, "Config.xml"); }
2011-10-07 06:36:04 +00:00
}
public virtual string NlogConfigPath
2011-10-07 06:36:04 +00:00
{
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web\\log.config"); }
2011-10-07 06:36:04 +00:00
}
2011-10-07 06:36:04 +00:00
public virtual void ConfigureNlog()
2011-04-10 02:44:01 +00:00
{
LogManager.Configuration = new XmlLoggingConfiguration(NlogConfigPath, false);
}
2011-10-07 06:36:04 +00:00
public virtual void UpdateIISConfig(string configPath)
{
Logger.Info(@"Server configuration file: {0}", configPath);
Logger.Info(@"Configuring server to: [http://localhost:{0}]", PortNumber);
2011-10-07 06:36:04 +00:00
var configXml = XDocument.Load(configPath);
var bindings =
configXml.XPathSelectElement("configuration/system.applicationHost/sites").Elements("site").Where(
d => d.Attribute("name").Value.ToLowerInvariant() == "nzbdrone").First().Element("bindings");
bindings.Descendants().Remove();
bindings.Add(
new XElement("binding",
new XAttribute("protocol", "http"),
new XAttribute("bindingInformation", String.Format("*:{0}:localhost", PortNumber))
2011-10-07 06:36:04 +00:00
));
bindings.Add(
2011-10-07 06:57:43 +00:00
new XElement("binding",
new XAttribute("protocol", "http"),
new XAttribute("bindingInformation", String.Format("*:{0}:", PortNumber))
2011-10-07 06:57:43 +00:00
));
2011-10-07 06:36:04 +00:00
configXml.Save(configPath);
}
public virtual void CreateDefaultConfigFile()
{
//Create the config file here
Directory.CreateDirectory(AppDataDirectory);
if (!File.Exists(ConfigFile))
{
WriteDefaultConfig();
}
}
private void WriteDefaultConfig()
{
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
xDoc.Add(new XElement("Config",
2011-10-07 06:57:43 +00:00
new XElement("Port", 8989),
new XElement("LaunchBrowser", true)
)
);
xDoc.Save(ConfigFile);
}
private string GetValue(string key, string parent = null)
{
var xDoc = XDocument.Load(ConfigFile);
var config = xDoc.Descendants("Config").Single();
var parentContainer = config;
if (parent != null)
parentContainer = config.Descendants(parent).Single();
2011-10-07 06:57:43 +00:00
string value = parentContainer.Descendants(key).Single().Value;
return value;
}
private int GetValueInt(string key, string parent = null)
{
return Convert.ToInt32(GetValue(key, parent));
}
private bool GetValueBoolean(string key, string parent = null)
{
return Convert.ToBoolean(GetValue(key, parent));
2011-04-10 02:44:01 +00:00
}
2010-10-15 07:10:44 +00:00
}
2011-04-10 02:44:01 +00:00
}