1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-10 21:12:56 +01:00
Radarr/NzbDrone/Owin/OwinHostController.cs

73 lines
2.0 KiB
C#
Raw Normal View History

2013-02-19 02:13:42 +01:00
using System;
using System.Collections.Generic;
using Microsoft.Owin.Hosting;
2013-02-19 02:13:42 +01:00
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.Configuration;
using NzbDrone.Owin.MiddleWare;
using Owin;
2013-05-05 23:24:33 +02:00
using System.Linq;
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
{
2013-05-11 01:53:50 +02:00
private readonly IConfigFileProvider _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
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;
_owinMiddleWares = owinMiddleWares;
2013-02-19 02:13:42 +01:00
_logger = logger;
}
public void StartServer()
{
2013-05-05 23:24:33 +02:00
var options = new StartOptions
{
Url = "http://*:" + _configFileProvider.Port,
App = GetType().AssemblyQualifiedName
2013-05-05 23:24:33 +02:00
};
2013-05-21 05:28:14 +02:00
_logger.Info("starting server on {0}", options.Url);
2013-05-05 23:24:33 +02:00
_host = WebApplication.Start(options, BuildApp);
}
private void BuildApp(IAppBuilder appBuilder)
{
2013-05-05 23:24:33 +02:00
foreach (var middleWare in _owinMiddleWares.OrderBy(c => c.Order))
{
2013-05-05 23:24:33 +02:00
_logger.Debug("Attaching {0} to host", middleWare.GetType().Name);
middleWare.Attach(appBuilder);
}
}
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
}
}