Polianin Nikita
3f30b98cf9
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m27s
133 lines
4.6 KiB
C#
133 lines
4.6 KiB
C#
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.Common.Interfaces;
|
|
using Mirea.Api.Endpoint.Common.Services;
|
|
using Mirea.Api.Endpoint.Configuration;
|
|
using Mirea.Api.Endpoint.Configuration.General;
|
|
using Mirea.Api.Endpoint.Configuration.Swagger;
|
|
using Mirea.Api.Endpoint.Middleware;
|
|
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(PathBuilder.Combine(GeneralConfig.FilePath), optional: true, reloadOnChange: true);
|
|
builder.Services.Configure<GeneralConfig>(builder.Configuration);
|
|
|
|
builder.Services.AddApplication();
|
|
builder.Services.AddPersistence(builder.Configuration.Get<GeneralConfig>()?.DbSettings?.ConnectionStringSql ?? string.Empty);
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddSingleton<IMaintenanceModeNotConfigureService, MaintenanceModeNotConfigureService>();
|
|
builder.Services.AddSingleton<IMaintenanceModeService, MaintenanceModeService>();
|
|
builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
|
|
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<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();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
var provider = app.Services.GetService<IApiVersionDescriptionProvider>();
|
|
|
|
foreach (var description in provider!.ApiVersionDescriptions)
|
|
{
|
|
var url = $"/swagger/{description.GroupName}/swagger.json";
|
|
var name = description.GroupName.ToUpperInvariant();
|
|
options.SwaggerEndpoint(url, name);
|
|
}
|
|
});
|
|
}
|
|
app.UseMiddleware<MaintenanceModeMiddleware>();
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
} |