using System.Reflection; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Newtonsoft.Json.Converters; using TURN.Services.Chess.DBModel; namespace TURN.Services.Chess { public class Startup { public void ConfigureServices(IServiceCollection services) { ConfigureSwagger(services); ConfigureAuthorization(services); _ = services.AddControllers(); _ = services.AddControllers().AddNewtonsoftJson(jsonOptions => { jsonOptions.SerializerSettings.Converters.Add(new StringEnumConverter()); }); _ = services.AddDbContext( options => options.UseNpgsql(Environment.GetEnvironmentVariable("DB"))); } public void Configure(IApplicationBuilder app) { _ = app.UseRouting(); _ = app.UseAuthentication(); _ = app.UseAuthorization(); _ = app.UseSwagger(); _ = app.UseSwaggerUI(); _ = app.UseEndpoints(endpoints => { _ = endpoints.MapControllers(); _ = endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action}"); _ = endpoints.MapSwagger(); }); } private void ConfigureSwagger(IServiceCollection services) { _ = services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "TURN.Api.Swagger", Version = "v1" }); string xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); }); _ = services.AddSwaggerGenNewtonsoftSupport(); } private void ConfigureAuthorization(IServiceCollection services) { _ = services.AddAuthentication() .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = AuthOptions.ISSUER, ValidateAudience = true, ValidAudience = AuthOptions.AUDIENCE, ValidateLifetime = true, IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(), ValidateIssuerSigningKey = true, }; }); _ = services.AddAuthorization(); } } }