2023-12-29 04:19:18 +03:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2024-08-10 23:03:28 +03:00
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2024-07-05 01:35:19 +03:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2024-07-04 23:46:43 +03:00
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
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-07-04 23:54:17 +03:00
|
|
|
using Mirea.Api.Endpoint.Common.Settings;
|
2024-09-07 04:28:07 +03:00
|
|
|
using Mirea.Api.Endpoint.Configuration.ApplicationConfiguration;
|
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-07-04 23:46:43 +03:00
|
|
|
using Mirea.Api.Security.Services;
|
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-07-04 23:45:33 +03:00
|
|
|
builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true);
|
2024-05-28 06:49:40 +03:00
|
|
|
builder.Services.Configure<GeneralConfig>(builder.Configuration);
|
2024-07-04 23:54:17 +03:00
|
|
|
builder.Configuration.AddJsonFile(Admin.FilePath, optional: true, reloadOnChange: true);
|
2024-06-28 22:52:58 +03:00
|
|
|
builder.Services.Configure<Admin>(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.AllowAnyMethod();
|
2024-07-07 23:20:54 +03:00
|
|
|
policy.AllowAnyHeader();
|
2024-06-01 10:57:52 +03:00
|
|
|
policy.AllowCredentials();
|
2024-10-07 01:42:00 +03:00
|
|
|
#if DEBUG
|
|
|
|
policy.WithOrigins("http://localhost:4200");
|
|
|
|
#endif
|
2024-01-26 19:32:10 +03:00
|
|
|
});
|
|
|
|
});
|
2024-01-26 19:33:25 +03:00
|
|
|
|
2024-07-05 01:35:19 +03:00
|
|
|
builder.WebHost.ConfigureKestrel(options =>
|
|
|
|
{
|
2024-07-05 02:37:12 +03:00
|
|
|
options.ListenAnyIP(
|
2024-07-05 01:35:19 +03:00
|
|
|
int.Parse(builder.Configuration.GetValue<string>("INTERNAL_PORT") ?? "8080"));
|
|
|
|
});
|
2024-07-05 00:44:55 +03:00
|
|
|
|
2024-07-04 23:46:43 +03:00
|
|
|
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
|
|
|
{
|
|
|
|
var secretForward = builder.Configuration.Get<GeneralConfig>();
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(secretForward!.SecretForwardToken))
|
|
|
|
{
|
2024-07-05 23:14:45 +03:00
|
|
|
secretForward.SecretForwardToken = GeneratorKey.GenerateAlphaNumeric(16);
|
2024-07-04 23:46:43 +03:00
|
|
|
secretForward.SaveSetting();
|
|
|
|
}
|
|
|
|
|
|
|
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
|
|
|
options.ForwardedForHeaderName = secretForward.SecretForwardToken + "-X-Forwarded-For";
|
2024-07-08 00:00:32 +03:00
|
|
|
options.KnownNetworks.Clear();
|
|
|
|
options.KnownProxies.Clear();
|
2024-07-04 23:46:43 +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
|
|
|
|
2024-08-10 23:03:28 +03:00
|
|
|
builder.Services.AddDataProtection()
|
|
|
|
.PersistKeysToFileSystem(new DirectoryInfo(PathBuilder.Combine("DataProtection")));
|
|
|
|
|
2023-12-29 04:19:18 +03:00
|
|
|
var app = builder.Build();
|
|
|
|
|
2024-07-08 00:00:32 +03:00
|
|
|
app.UseForwardedHeaders();
|
2024-07-05 01:35:19 +03:00
|
|
|
app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/'));
|
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:16:15 +03:00
|
|
|
app.UseMiddleware<CustomExceptionHandlerMiddleware>();
|
2024-08-12 21:54:05 +03:00
|
|
|
app.UseMiddleware<MaintenanceModeMiddleware>();
|
2024-06-15 21:53:00 +03:00
|
|
|
app.UseMiddleware<JwtRevocationMiddleware>();
|
2024-08-12 21:54:05 +03:00
|
|
|
app.UseMiddleware<CacheMaxAgeMiddleware>();
|
2023-12-29 04:19:18 +03:00
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
app.Run();
|
|
|
|
}
|
|
|
|
}
|