MireaBackend/Endpoint/Program.cs

125 lines
4.1 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Builder;
2024-01-26 19:34:42 +03:00
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;
2024-01-26 19:34:42 +03:00
using Microsoft.Extensions.Options;
using Mirea.Api.DataAccess.Application;
using Mirea.Api.DataAccess.Persistence;
2024-01-28 06:29:47 +03:00
using Mirea.Api.Endpoint.Configuration;
2024-01-26 19:34:42 +03:00
using Mirea.Api.Endpoint.Properties;
2024-05-28 06:30:42 +03:00
using Mirea.Api.Endpoint.Configuration.Swagger;
2024-01-26 19:34:42 +03:00
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<DictionaryEntry>()
.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.Get<GeneralConfig>()?.DbSettings?.ConnectionStringSql ?? string.Empty);
builder.Services.AddControllers();
2024-01-26 19:32:10 +03:00
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyHeader();
policy.AllowAnyMethod();
policy.AllowAnyOrigin();
});
});
2024-01-26 19:33:25 +03:00
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();
2024-01-26 19:33:25 +03:00
builder.Services.AddSwaggerGen(options =>
{
options.OperationFilter<SwaggerDefaultValues>();
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
var xmlPath = Path.Combine(basePath, "docs.xml");
options.IncludeXmlComments(xmlPath);
});
builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, 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<UberDbContext>();
DbInitializer.Initialize(uber!);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
2024-01-26 19:33:25 +03:00
app.UseSwaggerUI(options =>
{
var provider = app.Services.GetService<IApiVersionDescriptionProvider>();
2024-01-26 20:38:49 +03:00
foreach (var description in provider!.ApiVersionDescriptions)
2024-01-26 19:33:25 +03:00
{
var url = $"/swagger/{description.GroupName}/swagger.json";
var name = description.GroupName.ToUpperInvariant();
options.SwaggerEndpoint(url, name);
}
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}