2010-10-15 00:10:44 -07:00
|
|
|
using System;
|
2011-05-30 00:05:45 -07:00
|
|
|
using System.Data.Common;
|
2011-09-03 20:05:44 -07:00
|
|
|
using System.Linq;
|
2010-10-17 10:22:48 -07:00
|
|
|
using System.Threading;
|
2010-10-15 00:10:44 -07:00
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
2010-09-22 20:19:47 -07:00
|
|
|
using System.Web.Routing;
|
2013-01-02 17:09:13 -08:00
|
|
|
using Autofac;
|
|
|
|
using Autofac.Integration.Mvc;
|
2012-10-14 17:53:34 -07:00
|
|
|
using LowercaseRoutesMVC;
|
2010-10-15 00:10:44 -07:00
|
|
|
using NLog;
|
2010-09-22 20:19:47 -07:00
|
|
|
using NzbDrone.Core;
|
2012-11-13 08:27:48 -08:00
|
|
|
using NzbDrone.Core.Repository.Quality;
|
|
|
|
using NzbDrone.Web.Helpers.Binders;
|
2012-12-18 17:40:47 -08:00
|
|
|
using SignalR;
|
2010-09-22 20:19:47 -07:00
|
|
|
|
|
|
|
namespace NzbDrone.Web
|
|
|
|
{
|
2013-01-02 17:09:13 -08:00
|
|
|
public class MvcApplication : HttpApplication
|
2010-09-22 20:19:47 -07:00
|
|
|
{
|
2010-10-17 10:22:48 -07:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2010-09-22 20:19:47 -07:00
|
|
|
|
|
|
|
public static void RegisterRoutes(RouteCollection routes)
|
|
|
|
{
|
2012-11-02 00:35:49 -07:00
|
|
|
routes.IgnoreRoute("api/{*pathInfo}");
|
2010-09-22 20:19:47 -07:00
|
|
|
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
|
2013-01-20 18:47:08 -08:00
|
|
|
routes.IgnoreRoute("{resource}.html");
|
2011-04-18 17:12:06 -07:00
|
|
|
routes.IgnoreRoute("{*robotstxt}", new { robotstxt = @"(.*/)?robots.txt(/.*)?" });
|
|
|
|
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
|
2012-11-02 00:35:49 -07:00
|
|
|
|
2012-10-22 00:05:27 -07:00
|
|
|
routes.MapRouteLowercase(
|
|
|
|
name: "WithSeasonNumber",
|
2012-10-25 21:20:50 -07:00
|
|
|
url: "{controller}/{action}/{seriesId}/{seasonNumber}"
|
2012-10-22 00:05:27 -07:00
|
|
|
);
|
2011-03-30 18:42:27 -07:00
|
|
|
|
2012-10-14 17:53:34 -07:00
|
|
|
routes.MapRouteLowercase(
|
2012-10-22 00:05:27 -07:00
|
|
|
name: "SeriesId",
|
|
|
|
url: "{controller}/{action}/{seriesId}",
|
|
|
|
defaults: new { controller = "Series", action = "Index", seriesId = UrlParameter.Optional }
|
|
|
|
);
|
2010-09-22 20:19:47 -07:00
|
|
|
}
|
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
protected void Application_Start()
|
2012-11-03 11:23:47 -07:00
|
|
|
{
|
2013-01-02 17:09:13 -08:00
|
|
|
InitContainer();
|
2012-11-03 11:23:47 -07:00
|
|
|
|
2010-09-22 20:19:47 -07:00
|
|
|
RegisterRoutes(RouteTable.Routes);
|
2011-03-29 23:18:35 -07:00
|
|
|
AreaRegistration.RegisterAllAreas();
|
2011-07-03 17:19:05 -07:00
|
|
|
|
2011-11-21 22:55:09 -08:00
|
|
|
var razor = ViewEngines.Engines.Single(e => e is RazorViewEngine);
|
2011-07-03 17:19:05 -07:00
|
|
|
ViewEngines.Engines.Clear();
|
|
|
|
ViewEngines.Engines.Add(razor);
|
|
|
|
|
2012-11-13 08:27:48 -08:00
|
|
|
ModelBinders.Binders.Add(typeof(QualityTypes), new QualityTypesBinder());
|
|
|
|
|
2011-03-29 23:18:35 -07:00
|
|
|
RegisterGlobalFilters(GlobalFilters.Filters);
|
2011-11-02 22:04:14 -07:00
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
Logger.Info("Fully initialized and ready.");
|
2010-09-29 10:19:18 -07:00
|
|
|
}
|
2010-09-22 20:19:47 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
private void InitContainer()
|
2010-09-22 20:19:47 -07:00
|
|
|
{
|
2011-11-08 09:48:34 -08:00
|
|
|
Logger.Info("NzbDrone Starting up.");
|
2012-01-22 18:24:16 -08:00
|
|
|
var dispatch = new CentralDispatch();
|
2011-11-08 12:12:54 -08:00
|
|
|
dispatch.DedicateToHost();
|
2010-10-24 00:46:58 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
dispatch.ContainerBuilder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).SingleInstance();
|
|
|
|
dispatch.ContainerBuilder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).AsImplementedInterfaces().SingleInstance();
|
|
|
|
|
|
|
|
MVCRegistration(dispatch.ContainerBuilder);
|
|
|
|
|
2013-01-02 19:04:42 -08:00
|
|
|
var container = dispatch.BuildContainer();
|
2013-01-02 17:09:13 -08:00
|
|
|
|
|
|
|
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
|
2012-11-02 00:35:49 -07:00
|
|
|
|
2012-12-18 17:40:47 -08:00
|
|
|
//SignalR
|
|
|
|
RouteTable.Routes.MapHubs();
|
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void MVCRegistration(ContainerBuilder builder)
|
|
|
|
{
|
|
|
|
builder.RegisterModule(new AutofacWebTypesModule());
|
|
|
|
|
|
|
|
builder.RegisterControllers(typeof(MvcApplication).Assembly).InjectActionInvoker();
|
|
|
|
builder.RegisterModelBinders(typeof(MvcApplication).Assembly).SingleInstance();
|
2012-11-02 00:35:49 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
builder.RegisterType<ControllerActionInvoker>().As<IActionInvoker>();
|
2010-10-15 00:10:44 -07:00
|
|
|
}
|
2010-10-10 12:00:07 -07:00
|
|
|
|
2011-03-29 23:18:35 -07:00
|
|
|
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
|
|
|
|
{
|
|
|
|
filters.Add(new HandleErrorAttribute());
|
|
|
|
}
|
|
|
|
|
2010-10-15 00:10:44 -07:00
|
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
protected void Application_Error(object sender, EventArgs e)
|
|
|
|
{
|
2010-10-17 10:22:48 -07:00
|
|
|
var lastError = Server.GetLastError();
|
2011-04-21 19:23:31 -07:00
|
|
|
|
|
|
|
if (lastError is HttpException && lastError.InnerException == null)
|
2010-10-17 10:22:48 -07:00
|
|
|
{
|
2011-03-28 22:50:18 -07:00
|
|
|
Logger.WarnException(String.Format("{0}. URL[{1}]", lastError.Message, Request.Path), lastError);
|
2011-04-09 19:28:54 -07:00
|
|
|
return;
|
2010-10-17 10:22:48 -07:00
|
|
|
}
|
2011-04-09 19:28:54 -07:00
|
|
|
|
2011-04-18 17:12:06 -07:00
|
|
|
Logger.FatalException(lastError.Message + Environment.NewLine + Request.Url.PathAndQuery, lastError);
|
2011-04-09 19:28:54 -07:00
|
|
|
|
2011-05-30 00:05:45 -07:00
|
|
|
if (lastError is DbException)
|
2010-10-17 10:22:48 -07:00
|
|
|
{
|
2011-04-09 19:28:54 -07:00
|
|
|
Logger.Warn("Restarting application");
|
|
|
|
HttpRuntime.UnloadAppDomain();
|
2010-10-17 10:22:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Application_BeginRequest()
|
|
|
|
{
|
2011-11-23 21:50:41 -08:00
|
|
|
Thread.CurrentThread.Name = "WEB_THREAD";
|
2011-06-13 18:35:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void Application_EndRequest()
|
|
|
|
{
|
2010-09-22 20:19:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|