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