Esempio n. 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, StartupService startupService)
        {
            loggerFactory.AddSerilog();

            if (env.IsDevelopment())
            {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
                app.UseDeveloperExceptionPage();
            }

            if (!string.IsNullOrEmpty(SiteConfig.BaseUrlPrefix))
            {
                app.UsePathBase();
            }

            app.UseSession();

            app.UseSecretValidator();
            app.UseRequestSizeLimit();

            if (!string.IsNullOrEmpty(Configuration.GetValue("SlambyApi:Elm:Key", string.Empty)))
            {
                app.UseElmSecurity();
                app.UseElmStyleUrlFix();
                app.UseElmPage();    // Shows the logs at the specified path
                app.UseElmCapture(); // Adds the ElmLoggerProvider
            }

            app.UseCors(builder => builder
                        .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod());

            var logger = loggerFactory.CreateLogger <Startup>();

            try
            {
                startupService.Startup();

                if (env.IsDevelopment())
                {
                    app.UseSwagger();
                    app.UseSwaggerUi("swagger/ui", $"/swagger/{ApiVersion}/swagger.json");
                }

                app.UseGzip();

                app.UseRequestLogger();

                // Set up custom content types - associating file extension to MIME type
                var provider = new FileExtensionContentTypeProvider();
                app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider        = new PhysicalFileProvider(SiteConfig.Directory.User),
                    RequestPath         = new PathString(Common.Constants.FilesPath),
                    ContentTypeProvider = provider
                });
                app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider        = new PhysicalFileProvider(Path.Combine(env.WebRootPath, "assets")),
                    RequestPath         = new PathString("/assets"),
                    ContentTypeProvider = provider
                });

                //LimitsMiddleware.AspNetCore
                int maxConcurrentRequests = int.TryParse(
                    Configuration.GetValue("SlambyApi:RequestsLimiting:MaxConcurrentRequests", string.Empty),
                    out maxConcurrentRequests) ? maxConcurrentRequests : 0;
                if (maxConcurrentRequests > 0)
                {
                    app.UseConcurrentRequestsLimit(maxConcurrentRequests);
                }

                app.UseApiHeaderVersion();
                app.UseApiHeaderAuthentication();
                app.UseElapsedTime();
                app.UseMvc();

                app.UseNotFound();
                app.UseTerminal();
            }
            catch (Exception ex)
            {
                app.WriteExceptionResponse(logger, ex, "Startup Runtime Error");
            }
        }