using System.Reflection; using DbContextLib; using DbContextLib.Identity; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using Model.Manager; using Model2Entities; using StubAPI; using StubbedContextLib; using Swashbuckle.AspNetCore.SwaggerGen; namespace HeartTrackAPI.Utils; public class AppBootstrap(IConfiguration configuration) { private IConfiguration Configuration { get; } = configuration; public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddEndpointsApiExplorer(); AddSwagger(services); AddHeartTrackContextServices(services); AddModelService(services); AddIdentityServices(services); AddApiVersioning(services); services.AddHealthChecks(); } private void AddHeartTrackContextServices(IServiceCollection services) { string? connectionString; switch (Environment.GetEnvironmentVariable("TYPE")) { case "BDD": var host = Environment.GetEnvironmentVariable("HOST"); var port = Environment.GetEnvironmentVariable("PORTDB"); var database = Environment.GetEnvironmentVariable("DATABASE"); var username = Environment.GetEnvironmentVariable("USERNAME"); var password = Environment.GetEnvironmentVariable("PASSWORD"); connectionString = $"Server={host};port={port};database={database};user={username};password={password}"; Console.WriteLine("========RUNNING USING THE MYSQL SERVER=============="); Console.WriteLine(connectionString); Console.WriteLine("===================================================="); services.AddDbContext(options => options.UseInMemoryDatabase("AuthDb")); services.AddDbContext(options => options.UseMySql($"{connectionString}", new MySqlServerVersion(new Version(10, 11, 1))) , ServiceLifetime.Singleton); break; default: Console.WriteLine("====== RUNNING USING THE IN SQLITE DATABASE ======"); connectionString = Configuration.GetConnectionString("HeartTrackAuthConnection"); Console.WriteLine(connectionString); if (!string.IsNullOrWhiteSpace(connectionString)) { services.AddDbContext(options => options.UseInMemoryDatabase("AuthDb")); services.AddDbContext(options => options.UseSqlite(connectionString), ServiceLifetime.Singleton); } else { services.AddDbContext(options => options.UseInMemoryDatabase("AuthDb")); services.AddDbContext(); } break; } } private void AddModelService(IServiceCollection services) { switch (Environment.GetEnvironmentVariable("TYPE")) { case "BDD": services.AddSingleton(provider => new DbDataManager(provider.GetRequiredService())); break; case "STUB-MODEL": services.AddSingleton(); break; default: services.AddSingleton(provider => { provider.GetRequiredService().Database.EnsureCreated(); return new DbDataManager(provider.GetRequiredService()); }); // services.AddSingleton(provider => new DbDataManager(provider.GetRequiredService())); break; } } private void AddIdentityServices(IServiceCollection services) { // services.AddTransient, EmailSender>(); services.AddAuthorization(); services.AddIdentityApiEndpoints() .AddEntityFrameworkStores(); // .AddEntityFrameworkStores().AddDefaultTokenProviders(); } private void AddApiVersioning(IServiceCollection services) { services.AddApiVersioning(opt => { opt.ReportApiVersions = true; opt.AssumeDefaultVersionWhenUnspecified = true; opt.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0); opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(), new HeaderApiVersionReader("x-api-version"), new MediaTypeApiVersionReader("x-api-version")); }); } private void AddSwagger(IServiceCollection services) { services.AddSwaggerGen(options => { options.OperationFilter(); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); options.IncludeXmlComments(xmlPath); 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 }); var scheme = new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header, }, new List() } }; options.AddSecurityRequirement(scheme); }); services.AddTransient, SwaggerOptions>(); services.AddSwaggerGen(options => { options.OperationFilter(); }); services.AddVersionedApiExplorer(setup => { setup.GroupNameFormat = "'v'VVV"; setup.SubstituteApiVersionInUrl = true; }); } public void Configure(WebApplication app, IWebHostEnvironment env) { app.UseHttpsRedirection(); app.MapIdentityApi(); app.MapControllers(); app.UseAuthorization(); app.MapHealthChecks("/health"); // Configure the HTTP request pipeline. if (true) { var apiVersionDescriptionProvider = app.Services.GetRequiredService(); app.UseSwagger(); app.UseSwaggerUI(); app.MapSwagger(); app.UseSwaggerUI(options => { foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions) { options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant()); } }); } } }