137 lines
5.0 KiB
C#
137 lines
5.0 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using Mirea.Api.DataAccess.Application;
|
|
using Mirea.Api.DataAccess.Persistence;
|
|
using Mirea.Api.DataAccess.Persistence.Common;
|
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
|
using Mirea.Api.Endpoint.Common.Services;
|
|
using Mirea.Api.Endpoint.Common.Settings;
|
|
using Mirea.Api.Endpoint.Configuration.AppConfig;
|
|
using Mirea.Api.Endpoint.Configuration.General;
|
|
using Mirea.Api.Endpoint.Configuration.General.Validators;
|
|
using Mirea.Api.Endpoint.Middleware;
|
|
using Mirea.Api.Security.Services;
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace Mirea.Api.Endpoint;
|
|
|
|
public class Program
|
|
{
|
|
public static IServiceCollection AddDatabase(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var dbSettings = configuration.Get<GeneralConfig>()?.DbSettings;
|
|
services.AddApplication();
|
|
services.AddPersistence(
|
|
dbSettings?.DatabaseProvider ?? DatabaseProvider.Sqlite,
|
|
dbSettings?.ConnectionStringSql ?? string.Empty);
|
|
|
|
return services;
|
|
}
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment());
|
|
builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true);
|
|
builder.Services.Configure<GeneralConfig>(builder.Configuration);
|
|
builder.Configuration.AddJsonFile(Admin.FilePath, optional: true, reloadOnChange: true);
|
|
builder.Services.Configure<Admin>(builder.Configuration);
|
|
|
|
builder.Host.AddCustomSerilog();
|
|
AddDatabase(builder.Services, builder.Configuration);
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddSingleton<IMaintenanceModeNotConfigureService, MaintenanceModeNotConfigureService>();
|
|
builder.Services.AddSingleton<IMaintenanceModeService, MaintenanceModeService>();
|
|
builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
|
|
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.AddCustomRedis(builder.Configuration);
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowAll", policy =>
|
|
{
|
|
policy.AllowAnyMethod();
|
|
policy.AllowAnyHeader();
|
|
policy.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
builder.WebHost.ConfigureKestrel(options =>
|
|
{
|
|
options.ListenAnyIP(
|
|
int.Parse(builder.Configuration.GetValue<string>("INTERNAL_PORT") ?? "8080"));
|
|
});
|
|
|
|
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
|
{
|
|
var secretForward = builder.Configuration.Get<GeneralConfig>();
|
|
|
|
if (string.IsNullOrEmpty(secretForward!.SecretForwardToken))
|
|
{
|
|
secretForward.SecretForwardToken = GeneratorKey.GenerateAlphaNumeric(16);
|
|
secretForward.SaveSetting();
|
|
}
|
|
|
|
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();
|
|
|
|
builder.Services.AddJwtToken(builder.Configuration);
|
|
builder.Services.AddSecurity(builder.Configuration);
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseForwardedHeaders();
|
|
app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/'));
|
|
app.UseCors("AllowAll");
|
|
app.UseCustomSerilog();
|
|
app.UseForwardedHeaders();
|
|
|
|
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<MaintenanceModeMiddleware>();
|
|
app.UseMiddleware<CustomExceptionHandlerMiddleware>();
|
|
app.UseMiddleware<JwtRevocationMiddleware>();
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
} |