2023-12-29 04:19:18 +03:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2024-01-26 19:34:42 +03:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-12-29 04:19:18 +03:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-01-26 19:34:42 +03:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Mirea.Api.DataAccess.Application;
|
|
|
|
using Mirea.Api.DataAccess.Persistence;
|
2024-05-30 23:58:24 +03:00
|
|
|
using Mirea.Api.DataAccess.Persistence.Common;
|
2024-05-28 06:56:25 +03:00
|
|
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
2024-05-28 06:49:40 +03:00
|
|
|
using Mirea.Api.Endpoint.Common.Services;
|
2024-06-01 10:57:52 +03:00
|
|
|
using Mirea.Api.Endpoint.Configuration.AppConfig;
|
2024-05-28 06:53:52 +03:00
|
|
|
using Mirea.Api.Endpoint.Configuration.General;
|
2024-05-28 07:12:58 +03:00
|
|
|
using Mirea.Api.Endpoint.Configuration.General.Validators;
|
2024-05-28 07:04:07 +03:00
|
|
|
using Mirea.Api.Endpoint.Middleware;
|
2024-01-26 19:34:42 +03:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2023-12-29 04:19:18 +03:00
|
|
|
|
|
|
|
namespace Mirea.Api.Endpoint;
|
|
|
|
|
|
|
|
public class Program
|
|
|
|
{
|
2024-06-01 10:57:52 +03:00
|
|
|
public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration)
|
2024-01-26 19:30:41 +03:00
|
|
|
{
|
2024-06-01 11:10:42 +03:00
|
|
|
var dbSettings = configuration.Get<GeneralConfig>()?.DbSettings;
|
2024-06-01 10:57:52 +03:00
|
|
|
services.AddApplication();
|
|
|
|
services.AddPersistence(
|
|
|
|
dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite,
|
|
|
|
dbSettings?.ConnectionStringSql ?? string.Empty);
|
2024-05-29 06:30:01 +03:00
|
|
|
|
|
|
|
return services;
|
|
|
|
}
|
2024-05-29 06:42:14 +03:00
|
|
|
|
2023-12-29 04:19:18 +03:00
|
|
|
public static void Main(string[] args)
|
|
|
|
{
|
2024-01-26 19:31:28 +03:00
|
|
|
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
|
|
|
|
|
2023-12-29 04:19:18 +03:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2024-06-01 10:57:52 +03:00
|
|
|
builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment());
|
2024-05-28 06:49:40 +03:00
|
|
|
builder.Configuration.AddJsonFile(PathBuilder.Combine(GeneralConfig.FilePath), optional: true, reloadOnChange: true);
|
|
|
|
builder.Services.Configure<GeneralConfig>(builder.Configuration);
|
2023-12-29 04:19:18 +03:00
|
|
|
|
2024-06-01 10:57:52 +03:00
|
|
|
builder.Host.AddCustomSerilog();
|
|
|
|
AddDatabase(builder.Services, builder.Configuration);
|
|
|
|
|
2023-12-29 04:19:18 +03:00
|
|
|
builder.Services.AddControllers();
|
2024-01-26 19:32:10 +03:00
|
|
|
|
2024-05-28 07:01:23 +03:00
|
|
|
builder.Services.AddSingleton<IMaintenanceModeNotConfigureService, MaintenanceModeNotConfigureService>();
|
|
|
|
builder.Services.AddSingleton<IMaintenanceModeService, MaintenanceModeService>();
|
2024-05-28 06:56:25 +03:00
|
|
|
builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
|
2024-06-01 10:57:52 +03:00
|
|
|
|
|
|
|
builder.Services.AddMemoryCache();
|
|
|
|
builder.Services.AddCustomRedis(builder.Configuration);
|
|
|
|
|
2024-01-26 19:32:10 +03:00
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy("AllowAll", policy =>
|
|
|
|
{
|
|
|
|
policy.AllowAnyHeader();
|
|
|
|
policy.AllowAnyMethod();
|
2024-06-01 10:57:52 +03:00
|
|
|
policy.WithOrigins("http://localhost:4200");
|
|
|
|
policy.AllowCredentials();
|
2024-01-26 19:32:10 +03:00
|
|
|
});
|
|
|
|
});
|
2024-01-26 19:33:25 +03:00
|
|
|
|
2024-06-01 10:57:52 +03:00
|
|
|
builder.Services.AddCustomApiVersioning();
|
|
|
|
builder.Services.AddCustomSwagger();
|
2024-01-26 19:33:25 +03:00
|
|
|
|
2024-06-01 10:57:52 +03:00
|
|
|
builder.Services.AddJwtToken(builder.Configuration);
|
|
|
|
builder.Services.AddSecurity(builder.Configuration);
|
2023-12-29 04:19:18 +03:00
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
2024-06-10 22:01:58 +03:00
|
|
|
app.UseStaticFiles();
|
2024-06-01 10:57:52 +03:00
|
|
|
app.UseCors("AllowAll");
|
|
|
|
app.UseCustomSerilog();
|
2024-01-26 19:36:18 +03:00
|
|
|
|
2024-05-28 07:12:58 +03:00
|
|
|
using (var scope = app.Services.CreateScope())
|
|
|
|
{
|
|
|
|
var serviceProvider = scope.ServiceProvider;
|
|
|
|
|
|
|
|
var optionsSnapshot = serviceProvider.GetRequiredService<IOptionsSnapshot<GeneralConfig>>();
|
|
|
|
var settingsValidator = new SettingsRequiredValidator(optionsSnapshot);
|
|
|
|
var isDoneConfig = settingsValidator.AreSettingsValid();
|
|
|
|
|
|
|
|
if (isDoneConfig)
|
|
|
|
{
|
|
|
|
var uberDbContext = serviceProvider.GetRequiredService<UberDbContext>();
|
|
|
|
var maintenanceModeService = serviceProvider.GetRequiredService<IMaintenanceModeNotConfigureService>();
|
|
|
|
|
|
|
|
maintenanceModeService.DisableMaintenanceMode();
|
|
|
|
DbInitializer.Initialize(uberDbContext);
|
|
|
|
}
|
|
|
|
}
|
2024-01-26 19:36:18 +03:00
|
|
|
|
2024-06-01 10:57:52 +03:00
|
|
|
app.UseCustomSwagger(app.Services);
|
|
|
|
|
2024-05-28 07:04:07 +03:00
|
|
|
app.UseMiddleware<MaintenanceModeMiddleware>();
|
2024-05-28 07:16:15 +03:00
|
|
|
app.UseMiddleware<CustomExceptionHandlerMiddleware>();
|
2024-06-15 21:53:00 +03:00
|
|
|
app.UseMiddleware<JwtRevocationMiddleware>();
|
2023-12-29 04:19:18 +03:00
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
app.Run();
|
|
|
|
}
|
|
|
|
}
|