You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/NzbDrone/Owin/OwinHostController.cs

79 lines
2.4 KiB

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using NLog;
using Nancy.Bootstrapper;
using Nancy.Owin;
using NzbDrone.Common;
using NzbDrone.Owin.MiddleWare;
using Owin;
using System.Linq;
namespace NzbDrone.Owin
{
public class OwinHostController : IHostController
{
private readonly ConfigFileProvider _configFileProvider;
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
private readonly Logger _logger;
private IDisposable _host;
public OwinHostController(ConfigFileProvider configFileProvider, IEnumerable<IOwinMiddleWare> owinMiddleWares, Logger logger)
{
_configFileProvider = configFileProvider;
_owinMiddleWares = owinMiddleWares;
_logger = logger;
}
public void StartServer()
{
var options = new StartOptions
{
App = GetType().AssemblyQualifiedName,
Port = _configFileProvider.Port
};
_host = WebApplication.Start(options, BuildApp);
}
private void BuildApp(IAppBuilder appBuilder)
{
foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order))
{
_logger.Debug("Attaching {0} to host", middleWare.GetType().Name);
middleWare.Attach(appBuilder);
}
}
private static IAppBuilder RunNancy(IAppBuilder builder, INancyBootstrapper bootstrapper)
{
var nancyOwinHost = new NancyOwinHost(null, bootstrapper);
return builder.Use((Func<Func<IDictionary<string, object>, Task>, Func<IDictionary<string, object>, Task>>)(next => (Func<IDictionary<string, object>, Task>)nancyOwinHost.Invoke), new object[0]);
}
public string AppUrl
{
get { return string.Format("http://localhost:{0}", _configFileProvider.Port); }
}
public void RestartServer()
{
_logger.Warn("Attempting to restart server.");
StopServer();
StartServer();
}
public void StopServer()
{
if (_host == null) return;
_logger.Info("Attempting to stop Nancy host");
_host.Dispose();
_host = null;
_logger.Info("Host has stopped");
}
}
}