MireaBackend/Endpoint/Program.cs

145 lines
5.5 KiB
C#
Raw Normal View History

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;
using Microsoft.AspNetCore.HttpOverrides;
2024-01-26 19:34:42 +03:00
using Microsoft.Extensions.Configuration;
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-10-07 02:25:36 +03:00
using Mirea.Api.Endpoint.Configuration.Core.Middleware;
2024-10-07 02:13:35 +03:00
using Mirea.Api.Endpoint.Configuration.Core.Startup;
using Mirea.Api.Endpoint.Configuration.Model;
2024-10-07 02:25:36 +03:00
using Mirea.Api.Endpoint.Configuration.Validation;
using Mirea.Api.Endpoint.Configuration.Validation.Validators;
using Mirea.Api.Security.Services;
2024-01-26 19:34:42 +03:00
using System;
using System.IO;
namespace Mirea.Api.Endpoint;
public class Program
{
public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration)
{
2024-06-01 11:10:42 +03:00
var dbSettings = configuration.Get<GeneralConfig>()?.DbSettings;
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
public static void Main(string[] args)
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var builder = WebApplication.CreateBuilder(args);
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);
builder.Host.AddCustomSerilog();
AddDatabase(builder.Services, builder.Configuration);
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>();
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();
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 =>
{
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
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);
secretForward.SaveSetting();
Console.WriteLine($"For the reverse proxy server to work correctly, use the header: '{secretForward.SecretForwardToken}-X-Forwarded-For'");
}
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.ForwardedForHeaderName = secretForward.SecretForwardToken + "-X-Forwarded-For";
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
builder.Services.AddCustomApiVersioning();
builder.Services.AddCustomSwagger();
2024-01-26 19:33:25 +03:00
builder.Services.AddJwtToken(builder.Configuration);
builder.Services.AddSecurity(builder.Configuration);
2024-08-10 23:03:28 +03:00
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(PathBuilder.Combine("DataProtection")));
var app = builder.Build();
app.UseForwardedHeaders();
2024-07-05 01:35:19 +03:00
app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/'));
app.UseCors("AllowAll");
app.UseCustomSerilog();
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);
}
}
app.UseCustomSwagger(app.Services);
app.UseMiddleware<CustomExceptionHandlerMiddleware>();
2024-08-12 21:54:05 +03:00
app.UseMiddleware<MaintenanceModeMiddleware>();
app.UseMiddleware<JwtRevocationMiddleware>();
2024-08-12 21:54:05 +03:00
app.UseMiddleware<CacheMaxAgeMiddleware>();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}