2013-02-19 01:13:42 +00:00
|
|
|
|
using System;
|
2013-05-04 20:29:24 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Owin.Hosting;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using Nancy.Bootstrapper;
|
2013-05-04 20:29:24 +00:00
|
|
|
|
using Nancy.Owin;
|
|
|
|
|
using Owin;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
|
{
|
|
|
|
|
public interface IHostController
|
|
|
|
|
{
|
|
|
|
|
string AppUrl { get; }
|
|
|
|
|
void StartServer();
|
|
|
|
|
void RestartServer();
|
|
|
|
|
void StopServer();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-04 20:29:24 +00:00
|
|
|
|
|
|
|
|
|
public class OwinHostController : IHostController
|
2013-02-19 01:13:42 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly ConfigFileProvider _configFileProvider;
|
|
|
|
|
private readonly INancyBootstrapper _bootstrapper;
|
|
|
|
|
private readonly Logger _logger;
|
2013-05-04 20:29:24 +00:00
|
|
|
|
private IDisposable _host;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
|
2013-05-04 20:29:24 +00:00
|
|
|
|
public OwinHostController(ConfigFileProvider configFileProvider, INancyBootstrapper bootstrapper, Logger logger)
|
2013-02-19 01:13:42 +00:00
|
|
|
|
{
|
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
|
_bootstrapper = bootstrapper;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StartServer()
|
|
|
|
|
{
|
2013-05-04 20:29:24 +00:00
|
|
|
|
_host = WebApplication.Start(AppUrl, builder => RunNancy(builder, _bootstrapper));
|
|
|
|
|
}
|
2013-03-26 04:03:16 +00:00
|
|
|
|
|
2013-05-04 20:29:24 +00:00
|
|
|
|
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]);
|
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.");
|
|
|
|
|
|
2013-05-04 20:29:24 +00:00
|
|
|
|
StopServer();
|
2013-02-19 01:13:42 +00:00
|
|
|
|
StartServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopServer()
|
|
|
|
|
{
|
|
|
|
|
if (_host == null) return;
|
|
|
|
|
|
|
|
|
|
_logger.Info("Attempting to stop Nancy host");
|
2013-05-04 20:29:24 +00:00
|
|
|
|
_host.Dispose();
|
2013-02-19 01:13:42 +00:00
|
|
|
|
_host = null;
|
|
|
|
|
_logger.Info("Host has stopped");
|
|
|
|
|
}
|
2013-05-04 20:29:24 +00:00
|
|
|
|
|
2013-02-19 01:13:42 +00:00
|
|
|
|
}
|
2013-05-04 20:29:24 +00:00
|
|
|
|
|
2013-02-19 01:13:42 +00:00
|
|
|
|
}
|