using System.Diagnostics; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Serilog; using Serilog.Events; using WyvernInventory.Core.Interfaces.Repos; using WyvernInventory.Core.Interfaces.Services; using WyvernInventory.Core.Models; using WyvernInventory.Infrastructure.Data; using WyvernInventory.Infrastructure.Repos; using WyvernInventory.Infrastructure.Services; using ILogger = Serilog.ILogger; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddScoped, InventoryTypeService>(); builder.Services.AddScoped, InventoryTypeRepo>(); builder.Services.AddScoped(); builder.Services.AddScoped(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); builder.Services.AddSwaggerGen(); builder.Host.UseSerilog((context, services, configuration) => configuration .ReadFrom.Configuration(context.Configuration) .ReadFrom.Services(services) ); if (builder.Configuration["Database:ConnectionString"].IsNullOrEmpty()) { ILogger logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger(); logger.Fatal("Database environment variables not found"); Environment.Exit(1); } else { builder.Services.AddDbContext(options => { options .UseSqlServer(builder.Configuration["Database:ConnectionString"]).EnableSensitiveDataLogging(); if (builder.Environment.IsDevelopment()) { options .EnableSensitiveDataLogging() .EnableDetailedErrors() .LogTo(msg => { Debug.WriteLine(msg); }, [DbLoggerCategory.Database.Command.Name], LogLevel.Information); } }); } var app = builder.Build(); using (var scope = app.Services.CreateScope()) { DBContext db = scope.ServiceProvider.GetRequiredService(); var logger = scope.ServiceProvider.GetRequiredService(); if (db.Database.GetPendingMigrations().Any()) { await db.Database.MigrateAsync().ConfigureAwait(false); logger.Write(LogEventLevel.Information, "Migrated database"); } } // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } app.UseSwagger(); app.UseSwaggerUI(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();