using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Mirea.Api.DataAccess.Application; using Mirea.Api.DataAccess.Persistence; using Mirea.Api.Endpoint.Configuration; using Mirea.Api.Endpoint.Middleware; using Mirea.Api.Endpoint.Properties; using Swashbuckle.AspNetCore.SwaggerGen; using System; using System.Collections; using System.IO; using System.Linq; namespace Mirea.Api.Endpoint; public class Program { private static IConfigurationRoot ConfigureEnvironment() { EnvironmentManager.LoadEnvironment(".env"); var environmentVariables = Environment.GetEnvironmentVariables() .OfType() .ToDictionary( entry => entry.Key.ToString() ?? string.Empty, entry => entry.Value?.ToString() ?? string.Empty ); var result = new ConfigurationBuilder().AddInMemoryCollection(environmentVariables!); return result.Build(); } public static void Main(string[] args) { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddConfiguration(ConfigureEnvironment()); builder.Configuration.AddJsonFile(Settings.FilePath, optional: true, reloadOnChange: true); builder.Services.AddApplication(); builder.Services.AddPersistence(builder.Configuration); builder.Services.AddControllers(); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy => { policy.AllowAnyHeader(); policy.AllowAnyMethod(); policy.AllowAnyOrigin(); }); }); builder.Services.AddApiVersioning(options => { options.DefaultApiVersion = new ApiVersion(1, 0); options.AssumeDefaultVersionWhenUnspecified = true; options.ReportApiVersions = true; options.ApiVersionReader = new UrlSegmentApiVersionReader(); }); builder.Services.AddVersionedApiExplorer(options => { options.GroupNameFormat = "'v'VVV"; options.SubstituteApiVersionInUrl = true; }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => { options.OperationFilter(); var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); var xmlPath = Path.Combine(basePath, "docs.xml"); options.IncludeXmlComments(xmlPath); }); builder.Services.AddTransient, ConfigureSwaggerOptions>(); var app = builder.Build(); #if DEBUG // Write configurations foreach (var item in app.Configuration.AsEnumerable()) Console.WriteLine($"{item.Key}:{item.Value}"); #endif var uber = app.Services.CreateScope().ServiceProvider.GetService(); DbInitializer.Initialize(uber!); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(options => { var provider = app.Services.GetService(); foreach (var description in provider!.ApiVersionDescriptions) { var url = $"/swagger/{description.GroupName}/swagger.json"; var name = description.GroupName.ToUpperInvariant(); options.SwaggerEndpoint(url, name); } }); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); } }