1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone/Owin/OwinHostController.cs

71 lines
2.1 KiB
C#
Raw Normal View History

2013-02-19 02:13:42 +01:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
2013-02-19 02:13:42 +01:00
using NLog;
using Nancy.Bootstrapper;
using Nancy.Owin;
using NzbDrone.Common;
using NzbDrone.Owin.MiddleWare;
using Owin;
2013-02-19 02:13:42 +01:00
namespace NzbDrone.Owin
2013-02-19 02:13:42 +01:00
{
public class OwinHostController : IHostController
2013-02-19 02:13:42 +01:00
{
private readonly ConfigFileProvider _configFileProvider;
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
2013-02-19 02:13:42 +01:00
private readonly Logger _logger;
private IDisposable _host;
2013-02-19 02:13:42 +01:00
public OwinHostController(ConfigFileProvider configFileProvider, IEnumerable<IOwinMiddleWare> owinMiddleWares, Logger logger)
2013-02-19 02:13:42 +01:00
{
_configFileProvider = configFileProvider;
_owinMiddleWares = owinMiddleWares;
2013-02-19 02:13:42 +01:00
_logger = logger;
}
public void StartServer()
{
_host = WebApplication.Start(AppUrl, BuildApp);
}
private void BuildApp(IAppBuilder appBuilder)
{
foreach (var middleWare in _owinMiddleWares)
{
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]);
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.");
StopServer();
2013-02-19 02:13:42 +01:00
StartServer();
}
public void StopServer()
{
if (_host == null) return;
_logger.Info("Attempting to stop Nancy host");
_host.Dispose();
2013-02-19 02:13:42 +01:00
_host = null;
_logger.Info("Host has stopped");
}
2013-02-19 02:13:42 +01:00
}
}