using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using MongoDB.Driver; using Prometheus; using Retroactiune.Core.Interfaces; using Retroactiune.Core.Services; using Retroactiune.Infrastructure; using Sentry.AspNetCore; namespace Retroactiune { [ExcludeFromCodeCoverage] public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } private IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Database Configuration services.Configure(Configuration.GetSection(nameof(DatabaseSettings))); services.AddSingleton(sp => sp.GetRequiredService>().Value); // AutoMapper services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); // Services services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(i => { var settings = i.GetService>(); return new MongoClient(settings.Value.ConnectionString); }); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = Configuration.GetSection("AuthorizationProvider:Domain").Value, ValidAudience = Configuration.GetSection("AuthorizationProvider:Audience").Value, IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(Configuration.GetSection("AuthorizationProvider:SymmetricSecurityKey") .Value)) }; }); // WebAPI services.AddControllers(); services.AddSwaggerGen(c => { var filePath = Path.Combine(AppContext.BaseDirectory, "Retroactiune.WebAPI.xml"); c.IncludeXmlComments(filePath); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Type = SecuritySchemeType.Http, BearerFormat = "JWT", In = ParameterLocation.Header, Scheme = "bearer", Description = "Please insert JWT token into field" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] { } } }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMetricServer(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Retroactiune API"); c.RoutePrefix = ""; }); app.UseHttpsRedirection(); app.UseSentryTracing(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); logger.LogInformation("Running"); } } }