sonarr-repo-only/NzbDrone.Web/Global.asax.cs

135 lines
4.5 KiB
C#
Raw Permalink Normal View History

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