134 lines
6.3 KiB
C#
134 lines
6.3 KiB
C#
using System.Collections.Specialized;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Web.Mvc;
|
|
using System.Web.Routing;
|
|
using Lucene.Net.Analysis;
|
|
using Lucene.Net.Analysis.Standard;
|
|
using Lucene.Net.Index;
|
|
using Lucene.Net.QueryParsers;
|
|
using Lucene.Net.Search;
|
|
using Lucene.Net.Store;
|
|
using Lucene.Net.Util;
|
|
using Microsoft.Practices.Unity;
|
|
using Microsoft.Practices.Unity.InterceptionExtension;
|
|
using Portoa.Logging;
|
|
using Portoa.Lucene;
|
|
using Portoa.Persistence;
|
|
using Portoa.Search;
|
|
using Portoa.Web;
|
|
using Portoa.Web.Models;
|
|
using Portoa.Web.Security;
|
|
using Portoa.Web.Unity;
|
|
using UnityGenerics;
|
|
using VideoGameQuotes.Api;
|
|
using VideoGameQuotes.Api.Persistence;
|
|
using VideoGameQuotes.Web.Configuration;
|
|
using VideoGameQuotes.Web.Controllers;
|
|
using VideoGameQuotes.Web.Models;
|
|
using VideoGameQuotes.Web.Search;
|
|
using VideoGameQuotes.Web.Security;
|
|
using VideoGameQuotes.Web.Services;
|
|
using Directory = Lucene.Net.Store.Directory;
|
|
|
|
namespace VideoGameQuotes.Web {
|
|
public class MvcApplication : MvcApplicationBase<User> {
|
|
|
|
protected override void ConfigureModelBinders(ModelBinderDictionary binders) {
|
|
binders
|
|
.Add<Region, FlagEnumModelBinder<Region>>()
|
|
.Add<BrowseModel, BrowseModelBinder>()
|
|
.Add<ApiModel, ApiModelBinder>();
|
|
}
|
|
|
|
protected override void ConfigureUnityExtensions() {
|
|
Container
|
|
.AddNewExtension<ConfigureLog4Net>()
|
|
.Configure<ILog4NetConfigurator>()
|
|
.SetName("VideoGameQuotes.Web")
|
|
.UseXml();
|
|
|
|
Container.AddNewExtension<LogAllMethodCalls>();
|
|
Container.AddNewExtension<UpdateSearchIndex>();
|
|
}
|
|
|
|
protected override void ConfigureUnity() {
|
|
Container
|
|
.RegisterType<VerifyUserAttribute>(new InjectionProperty<VerifyUserAttribute>(attr => attr.UserProvider))
|
|
.RegisterAndIntercept<ICurrentUserProvider<User>, SessionBasedUserProvider>()
|
|
.RegisterAndIntercept<IUserService, UserService>()
|
|
.RegisterAndIntercept<IAdministrationService, AdministrationService>()
|
|
.RegisterAndIntercept<IQuoteService, QuoteService>()
|
|
.RegisterAndIntercept<ISystemService, SystemService>()
|
|
.RegisterAndIntercept<ICategoryService, CategoryService>()
|
|
.RegisterAndIntercept<IPublisherService, PublisherService>()
|
|
.RegisterAndIntercept<IGameService, GameService>()
|
|
.RegisterAndIntercept<IApiService, ApiService>()
|
|
.RegisterAndIntercept<IAuthenticationService, FormsAuthenticationService>()
|
|
.RegisterAndIntercept<IUserRepository, UserRepository>();
|
|
|
|
//search stuff
|
|
Container
|
|
.RegisterType<Directory>(new ContainerControlledLifetimeManager(), new InjectionFactory(CreateIndexDirectory))
|
|
.RegisterType<IndexWriter>(new ContainerControlledLifetimeManager(), new InjectionFactory(CreateIndexWriter))
|
|
.RegisterInstance(Version.LUCENE_29)
|
|
.RegisterType<Analyzer, StandardAnalyzer>(new InjectionConstructor(typeof(Version)))
|
|
.RegisterType<QueryParser>(new InjectionFactory(CreateQueryParser))
|
|
.RegisterAndIntercept(typeof(ISearcher<>), typeof(LuceneEntitySearcher<>))
|
|
.RegisterAndIntercept(typeof(ISearchService<>), typeof(SearchService<>))
|
|
.RegisterAndIntercept<ILuceneDocumentHandler<Quote>, QuoteDocumentHandler>()
|
|
.RegisterAndIntercept(typeof(ISearchIndexBuilder<>), typeof(LuceneEntityIndexBuilder<>));
|
|
}
|
|
|
|
#region lucene-related factories
|
|
private static QueryParser CreateQueryParser(IUnityContainer container) {
|
|
return new QueryParser(container.Resolve<Version>(), "text", container.Resolve<Analyzer>());
|
|
}
|
|
|
|
private static Directory CreateIndexDirectory(IUnityContainer container) {
|
|
var indexDirectory = ((NameValueCollection)ConfigurationManager.GetSection("vgquotes"))["luceneIndexDirectory"];
|
|
return new SimpleFSDirectory(new DirectoryInfo(indexDirectory));
|
|
}
|
|
|
|
private static IndexWriter CreateIndexWriter(IUnityContainer container) {
|
|
return new IndexWriter(
|
|
container.Resolve<Directory>(),
|
|
new StandardAnalyzer(Version.LUCENE_29),
|
|
true,
|
|
IndexWriter.MaxFieldLength.UNLIMITED
|
|
);
|
|
}
|
|
#endregion
|
|
|
|
protected override void AfterStartUp() {
|
|
Container.Resolve<ISearchIndexBuilder<Quote>>().BuildIndex();
|
|
}
|
|
|
|
protected override void RegisterRoutes(RouteCollection routes) {
|
|
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
|
|
routes.IgnoreRoute("media/{*anything}");
|
|
|
|
//bullshit route so that RenderAction works
|
|
routes.MapRoute("mainmenu", "home/mainmenu", new { controller = "Home", action = "MainMenu" });
|
|
|
|
routes.MapRoute("crud-default", "{controller}/{action}", null, new { controller = "system|publisher|game|category", action = "create|edit|delete" });
|
|
|
|
routes.MapRoute("users-paged", "admin/users/{start}-{end}", new { controller = "Admin", action = "Users" }, new { start = @"\d+", end = @"\d+" });
|
|
routes.MapRoute("admin", "admin/{action}", new { controller = "Admin", action = "Index" }, new { action = "users|create|flags|password" });
|
|
|
|
routes.MapRoute("user-edit", "user/edit/{usernameOrIp}", new { controller = "User", action = "Edit", usernameOrIp = @"\w+" });
|
|
routes.MapRoute("user-default", "user/{action}/{id}", new { controller = "User", action = "delete|ban", id = UrlParameter.Optional });
|
|
|
|
routes.MapRoute("api", "api/{action}/{id}/{*criteria}", new { controller = "Api" }, new { action = "game|system|category|publisher|quote", id = @"\d+|all" });
|
|
routes.MapRoute("home", "{action}", new { controller = "Home", action = "Index" }, new { action = "about|contact|login|logout" });
|
|
routes.MapRoute("paged", "{action}/{page}", new { controller = "Quote", page = 1 }, new { action = "best|recent", page = @"\d+" });
|
|
routes.MapRoute("browse", "browse/{*qualifiers}", new { controller = "Quote", action = "Browse" });
|
|
routes.MapRoute("search", "search/{*searchQuery}", new { controller = "Quote", action = "Search" });
|
|
routes.MapRoute("quote-task", "{action}/{id}", new { controller = "Quote" }, new { action = "edit|flags", id = @"\d+" });
|
|
routes.MapRoute("quote", "{action}", new { controller = "Quote" }, new { action = "submit|recent|random|vote|report" });
|
|
routes.MapRoute("dismiss-flag", "dismiss-flag", new { controller = "Quote", action = "DismissFlag" });
|
|
routes.MapRoute("individual-quote", "quote/{id}/{*text}", new { controller = "Quote", action = "Quote" }, new { id = @"\d+" });
|
|
routes.MapRoute("default", "{controller}", new { controller = "home", action = "index" });
|
|
}
|
|
}
|
|
} |