mirror of
https://github.com/lidarr/Lidarr
synced 2024-12-26 17:47:08 +00:00
4da6654440
Added caching to ConfigFileProvider,
73 lines
No EOL
2 KiB
C#
73 lines
No EOL
2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Owin.Hosting;
|
|
using NLog;
|
|
using NzbDrone.Common;
|
|
using NzbDrone.Core.Configuration;
|
|
using NzbDrone.Owin.MiddleWare;
|
|
using Owin;
|
|
using System.Linq;
|
|
|
|
namespace NzbDrone.Owin
|
|
{
|
|
public class OwinHostController : IHostController
|
|
{
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
private readonly IEnumerable<IOwinMiddleWare> _owinMiddleWares;
|
|
private readonly Logger _logger;
|
|
private IDisposable _host;
|
|
|
|
public OwinHostController(IConfigFileProvider configFileProvider, IEnumerable<IOwinMiddleWare> owinMiddleWares, Logger logger)
|
|
{
|
|
_configFileProvider = configFileProvider;
|
|
_owinMiddleWares = owinMiddleWares;
|
|
_logger = logger;
|
|
}
|
|
|
|
public void StartServer()
|
|
{
|
|
var options = new StartOptions
|
|
{
|
|
Url = "http://*:" + _configFileProvider.Port,
|
|
App = GetType().AssemblyQualifiedName
|
|
};
|
|
|
|
_logger.Info("starting server on {0}", options.Url);
|
|
|
|
_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);
|
|
}
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
}
|
|
} |