1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00
Radarr/NzbDrone.Common/HostController.cs

76 lines
2.0 KiB
C#
Raw Normal View History

2013-02-19 02:13:42 +01:00
using System;
using System.Linq;
using NLog;
using Nancy.Bootstrapper;
using Nancy.Hosting.Self;
namespace NzbDrone.Common
{
public interface IHostController
{
bool ServerStarted { get; }
string AppUrl { get; }
void StartServer();
void RestartServer();
void StopServer();
}
public class HostController : IHostController
{
private readonly ConfigFileProvider _configFileProvider;
private readonly SecurityProvider _securityProvider;
2013-02-19 02:13:42 +01:00
private readonly INancyBootstrapper _bootstrapper;
private readonly Logger _logger;
private NancyHost _host;
public bool ServerStarted { get; private set; }
public HostController(ConfigFileProvider configFileProvider, SecurityProvider securityProvider, INancyBootstrapper bootstrapper, Logger logger)
2013-02-19 02:13:42 +01:00
{
_configFileProvider = configFileProvider;
_securityProvider = securityProvider;
2013-02-19 02:13:42 +01:00
_bootstrapper = bootstrapper;
_logger = logger;
}
public void StartServer()
{
if (_securityProvider.IsNzbDroneUrlRegistered())
_host = new NancyHost(new Uri(AppUrl), _bootstrapper);
else
_host = new NancyHost(new Uri(AppUrl), _bootstrapper, new HostConfiguration { RewriteLocalhost = false });
2013-03-01 01:50:50 +01:00
_host.Start();
2013-02-19 02:13:42 +01:00
}
public string AppUrl
{
2013-03-01 01:50:50 +01:00
get { return string.Format("http://localhost:{0}", _configFileProvider.Port); }
2013-02-19 02:13:42 +01:00
}
public void RestartServer()
{
_logger.Warn("Attempting to restart server.");
if (_host != null)
{
StopServer();
}
StartServer();
}
public void StopServer()
{
if (_host == null) return;
_logger.Info("Attempting to stop Nancy host");
_host.Stop();
_host = null;
_logger.Info("Host has stopped");
}
}
}