sonarr-repo-only/NzbDrone.Host/Owin/OwinHostController.cs

78 lines
2.2 KiB
C#
Raw Normal View History

2013-02-19 01:13:42 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin.Hosting;
2013-02-19 01:13:42 +00:00
using NLog;
using NzbDrone.Common.Security;
using NzbDrone.Core.Configuration;
using NzbDrone.Host.Owin.MiddleWare;
using Owin;
2013-02-19 01:13:42 +00:00
namespace NzbDrone.Host.Owin
2013-02-19 01:13:42 +00:00
{
public class OwinHostController : IHostController
2013-02-19 01:13:42 +00:00
{
2013-05-10 23:53:50 +00:00
private readonly IConfigFileProvider _configFileProvider;
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
2013-02-19 01:13:42 +00:00
private readonly Logger _logger;
private IDisposable _host;
2013-02-19 01:13:42 +00:00
public OwinHostController(IConfigFileProvider configFileProvider, IEnumerable<IOwinMiddleWare> owinMiddleWares, Logger logger)
2013-02-19 01:13:42 +00:00
{
_configFileProvider = configFileProvider;
_owinMiddleWares = owinMiddleWares;
2013-02-19 01:13:42 +00:00
_logger = logger;
}
public void StartServer()
{
IgnoreCertErrorPolicy.Register();
2013-07-06 19:10:11 +00:00
var url = "http://*:" + _configFileProvider.Port;
var options = new StartOptions(url)
2013-05-05 21:24:33 +00:00
{
2013-07-06 19:10:11 +00:00
ServerFactory = "Microsoft.Owin.Host.HttpListener"
2013-05-05 21:24:33 +00:00
};
2013-07-06 19:10:11 +00:00
_logger.Info("starting server on {0}", url);
2013-05-21 03:28:14 +00:00
_host = WebApp.Start(OwinServiceProviderFactory.Create(), options, BuildApp);
}
private void BuildApp(IAppBuilder appBuilder)
{
2013-07-06 19:10:11 +00:00
appBuilder.Properties["host.AppName"] = "NzbDrone";
2013-05-05 21:24:33 +00:00
foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order))
{
2013-05-05 21:24:33 +00:00
_logger.Debug("Attaching {0} to host", middleWare.GetType().Name);
middleWare.Attach(appBuilder);
}
}
2013-02-19 01:13:42 +00:00
public string AppUrl
{
2013-03-01 00:50:50 +00:00
get { return string.Format("http://localhost:{0}", _configFileProvider.Port); }
2013-02-19 01:13:42 +00:00
}
public void RestartServer()
{
_logger.Warn("Attempting to restart server.");
StopServer();
2013-02-19 01:13:42 +00:00
StartServer();
}
public void StopServer()
{
if (_host == null) return;
_logger.Info("Attempting to stop Nancy host");
_host.Dispose();
2013-02-19 01:13:42 +00:00
_host = null;
_logger.Info("Host has stopped");
}
2013-02-19 01:13:42 +00:00
}
}