public ResultController(UnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; questionSeedService = new QuestionSeedService(unitOfWork); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, UnitOfWork unitOfWork, IAntiforgery antiforgery) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); if (Program.UseWebpack) { app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { HotModuleReplacement = true, ProjectPath = Environment.ContentRootPath + "/HistoryContest.Client" }); } } else { app.UseExceptionHandler("/Home/Error"); // Redirect all http requests to https // app.UseRewriter(new RewriteOptions().AddRedirectToHttps()); } // Use Cookie Authentication app.UseAuthentication(); //app.UseCors("OpenPolicy"); #region Static file routes // use wwwroot static files DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("login.html"); options.DefaultFileNames.Add("index.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); // enable default url rewrite for wiki app.UseDefaultFiles(new DefaultFilesOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Environment.ContentRootPath, @"HistoryContest.Docs", "wiki")), RequestPath = new PathString("/wiki") }); // use wiki static files app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Environment.ContentRootPath, @"HistoryContest.Docs", "wiki")), RequestPath = new PathString("/wiki") }); // defense default app.UseDefaultFiles(new DefaultFilesOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Environment.ContentRootPath, @"HistoryContest.Docs", "defense")), RequestPath = new PathString("/defense") }); // use defense static files app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Environment.ContentRootPath, @"HistoryContest.Docs", "defense")), RequestPath = new PathString("/defense") }); #endregion #region Api document routes // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(c => { c.RouteTemplate = "api/{documentname}/swagger.json"; }); // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.RoutePrefix = "api"; c.SwaggerEndpoint("/api/seu-history-contest/swagger.json", "SEU History Contest API v1"); }); #endregion // use sessions app.UseSession(); app.Use(async(context, next) => { await context.Session.LoadAsync(); string path = context.Request.Path.Value; if (!path.ToLower().Contains("/api/account/logout")) { // This is needed to provide the token generator with the logged in context (if any) var authInfo = await context.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme); context.User = authInfo.Principal; var tokens = antiforgery.GetAndStoreTokens(context); context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions { HttpOnly = false }); } await next.Invoke(); await context.Session.CommitAsync(); }); #region Javascript spa routes app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); routes.MapSpaFallbackRoute( name: "spa-fallback", defaults: new { controller = "Home", action = "Index" }); }); #endregion // Seed database if (!unitOfWork.DbContext.AllMigrationsApplied()) { unitOfWork.DbContext.Database.Migrate(); unitOfWork.DbContext.EnsureAllSeeded(); } if (Program.RefreshCache || Environment.IsDevelopment()) { // flush cache var endpoints = RedisService.Connection.GetEndPoints(); RedisService.Connection.GetServer(endpoints.First()).FlushDatabase(1); unitOfWork.QuestionRepository.LoadQuestionsToCache(); unitOfWork.StudentRepository.LoadStudentsToCache(); } if (Program.RegenerateSeed || Environment.IsDevelopment()) { // Load cache var questionSeedService = new QuestionSeedService(unitOfWork); int scale = unitOfWork.Configuration.QuestionSeedScale; var questionSeeds = questionSeedService.CreateNewSeeds(scale); unitOfWork.Cache.QuestionSeeds().SetRange(questionSeeds, s => s.ID.ToString()); } if (Environment.IsDevelopment()) { new TestDataService(unitOfWork).SeedTestedStudents(); } syncWithDatabaseTimer = new Timer(async o => { try { var _services = new ServiceCollection(); ConfigureServices(_services); using (var _serviceProvider = _services.BuildServiceProvider()) using (UnitOfWork _unitOfWork = _serviceProvider.GetService <UnitOfWork>()) { await _unitOfWork.SaveCacheToDataBase(); new ExcelExportService(_unitOfWork).UpdateExcelOfSchool(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } try { GC.Collect(); syncWithDatabaseTimer.Change((int)TimeSpan.FromMinutes(10).TotalMilliseconds, Timeout.Infinite); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }, null, (int)TimeSpan.FromMinutes(10).TotalMilliseconds, Timeout.Infinite); }