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, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHealthChecks("/health", new HealthCheckOptions()
            {
                ResponseWriter = async(context, healthReport) => {
                    context.Response.ContentType = MediaTypeNames.Application.Json;

                    var healthCheckData = new HealthCheckDataResponse()
                    {
                        Status   = healthReport.Status.ToString(),
                        Duration = healthReport.TotalDuration,
                        Data     = healthReport.Entries.Select(it => new HealthCheckData()
                        {
                            Component = it.Key, Description = it.Value.Description, Status = it.Value.Status.ToString()
                        })
                    };

                    await context.Response.WriteAsync(JsonConvert.SerializeObject(healthCheckData));
                }
            });

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseAuthentication();

            var swaggerConfigOptions = new SwaggerConfigOptions();
            var apiConfigParameters  = new ApiConfigParameters();

            Configuration.Bind(nameof(SwaggerConfigOptions), swaggerConfigOptions);
            Configuration.Bind(nameof(ApiConfigParameters), apiConfigParameters);
            app.UseSwagger(options =>
            {
                options.RouteTemplate = swaggerConfigOptions.JsonRoute;
            });
            app.UseSwaggerUI(options => {
                options.SwaggerEndpoint(swaggerConfigOptions.UIEndpoint.Replace("{CurrentVersion}", apiConfigParameters.CurrentVersion), swaggerConfigOptions.Description);

                //options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
            });

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(options =>
            {
                options.MapDefaultControllerRoute();
            });
        }
Esempio n. 2
0
        public void InstallServices(IServiceCollection services, IConfiguration configuration)
        {
            //MAKE JWT PARAMETERS AVAILABLE THROUGHOUT THE APP
            var apiConfigParameters = new ApiConfigParameters();

            configuration.Bind(nameof(ApiConfigParameters), apiConfigParameters);
            services.AddSingleton(apiConfigParameters);

            //SWAGGER
            services.AddSwaggerGen(options => {
                options.SwaggerDoc(apiConfigParameters.CurrentVersion, new OpenApiInfo()
                {
                    Title = "Tweetbook API", Version = apiConfigParameters.CurrentVersion
                });
                options.ExampleFilters(); //For incremented documentation purposes (code continues down below in AddSwaggerExamplesFromAssemblyOf part)
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey
                });

                //Instructs Swagger to require a new input parameter as per ClientApiKeyValidationOperationFilter class
                //https://stackoverflow.com/questions/41493130/web-api-how-to-add-a-header-parameter-for-all-api-in-swagger
                options.OperationFilter <ClientApiKeyValidationOperationFilter>();

                options.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme()
                        {
                            Reference = new OpenApiReference()
                            {
                                Id   = "Bearer",
                                Type = ReferenceType.SecurityScheme
                            }
                        }, new List <string>()
                    }
                });

                //The lines below allow to take controller classes xml comments to Swagger documentation. The more documented, the better!
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
            });

            //For Swagger documentation increment purposes
            services.AddSwaggerExamplesFromAssemblyOf <Startup>();
        }