using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Mirea.Api.DataAccess.Application; using Mirea.Api.DataAccess.Persistence; using Mirea.Api.DataAccess.Persistence.Common; using Mirea.Api.Endpoint.Common.Interfaces; using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Configuration.AppConfig; using Mirea.Api.Endpoint.Configuration.General; using Mirea.Api.Endpoint.Configuration.General.Settings; using Mirea.Api.Endpoint.Configuration.General.Validators; using Mirea.Api.Endpoint.Middleware; using System; using System.IO; namespace Mirea.Api.Endpoint; public class Program { public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration) { var dbSettings = configuration.Get(); services.AddApplication(); services.AddPersistence( dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite, dbSettings?.ConnectionStringSql ?? string.Empty); return services; } public static void Main(string[] args) { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment()); builder.Configuration.AddJsonFile(PathBuilder.Combine(GeneralConfig.FilePath), optional: true, reloadOnChange: true); builder.Services.Configure(builder.Configuration); builder.Host.AddCustomSerilog(); AddDatabase(builder.Services, builder.Configuration); builder.Services.AddControllers(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddMemoryCache(); builder.Services.AddCustomRedis(builder.Configuration); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy => { policy.AllowAnyHeader(); policy.AllowAnyMethod(); policy.WithOrigins("http://localhost:4200"); policy.AllowCredentials(); }); }); builder.Services.AddCustomApiVersioning(); builder.Services.AddCustomSwagger(); builder.Services.AddJwtToken(builder.Configuration); builder.Services.AddSecurity(builder.Configuration); var app = builder.Build(); app.UseCors("AllowAll"); app.UseCustomSerilog(); using (var scope = app.Services.CreateScope()) { var serviceProvider = scope.ServiceProvider; var optionsSnapshot = serviceProvider.GetRequiredService>(); var settingsValidator = new SettingsRequiredValidator(optionsSnapshot); var isDoneConfig = settingsValidator.AreSettingsValid(); if (isDoneConfig) { var uberDbContext = serviceProvider.GetRequiredService(); var maintenanceModeService = serviceProvider.GetRequiredService(); maintenanceModeService.DisableMaintenanceMode(); DbInitializer.Initialize(uberDbContext); } } app.UseCustomSwagger(app.Services); app.UseMiddleware(); app.UseMiddleware(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); } }