Esempio n. 1
0
 public AppConfig()
 {
     JwtAuth           = new JwtAuthSettings();
     Api               = new ApiSettings();
     Swagger           = new SwaggerSettings();
     ExceptionHandling = new ExceptionHandlingSettings();
 }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            AppConfig appConfig = AppConfig.ApplicationInstance;

            if (AppConfig.ApplicationInstance.JwtAuth.IsMiddlewareEnabled)
            {
                app.UseAuthentication();
            }

            ExceptionHandlingSettings exceptionConfig = appConfig?.ExceptionHandling;

            if (exceptionConfig != null && exceptionConfig.IsMiddlewareEnabled)
            {
                app.UseMiddleware(typeof(ExceptionHandlingMiddleware));
            }

            // If configured to do so, setup Swagger based on our configuration
            SwaggerSettings swaggerConfig = appConfig?.Swagger;

            if (swaggerConfig != null && swaggerConfig.IsEnabled)
            {
                app.UseStaticFiles();
                app.UseSwagger(c =>
                {
                    c.RouteTemplate = swaggerConfig.SwaggerRoute + "/{documentName}/swagger.json";
                });
                app.UseSwaggerUI(c =>
                {
                    c.RoutePrefix  = swaggerConfig.SwaggerUiRoute;
                    var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute));
                    AssemblyTitleAttribute assemblyTitleAttribute = attributes.SingleOrDefault() as AssemblyTitleAttribute;
                    string title = assemblyTitleAttribute?.Title + " v" + appConfig.Api.Version;
                    c.SwaggerEndpoint((string.IsNullOrEmpty(swaggerConfig.OverrideRootUrl) ? string.Empty : swaggerConfig.OverrideRootUrl) + "/" + swaggerConfig.SwaggerRoute + "/v" + appConfig.Api.Version + "/swagger.json", title);
                });
            }

            // Add CORS settings
            if (appConfig.Api == null || appConfig.Api.IsCorsAllowingAll)
            {
                app.UseCors(
                    o =>
                {
                    o.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
                });
            }

            app.UseMvc();
        }