/
softailor
/
gridient
Обзор
Документация
Войти
/
softailor
/
gridient
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/Gridient.Host.PostgreSQL/Program.cs
149 строк
6 KB
Jenkins
Auto add
18 фев 2026, 10:19
18 фев 2026, 10:19
1e6c4f8
Код
Авторство
О чём код?
using Gridient.Behaviors; using Gridient.Configuring; using Gridient.Core.Extensions; using Gridient.Infrastructure.Authentication.Local.Controllers; using Gridient.Infrastructure.Authentication.Local.Extensions; using Gridient.Infrastructure.Authentication.Local.Persistence.PostgreSql.Extensions; using Gridient.Infrastructure.Authorization.Local.Controllers; using Gridient.Infrastructure.Authorization.Local.Extensions; using Gridient.Infrastructure.Authorization.Local.Persistence.PostgreSql.Extensions; using Gridient.Infrastructure.Persistence.PostgreSql.Extensions; using Gridient.Middlewares; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.EntityFrameworkCore; var logger = LoggingConfiguration.InitializeNLog(); var dbLoggerFactory = LoggingConfiguration.CreateDbLoggerFactory(); try { var builder = WebApplication.CreateBuilder(args); // Настройка Kestrel var backendPort = Environment.GetEnvironmentVariable("BACKEND_PORT") ?? "5285"; builder.WebHost.ConfigureKestrel(options => { options.ListenAnyIP(int.Parse(backendPort)); }); builder.Services.AddCors(builder.Configuration); builder.Services.Configure<ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost; options.KnownIPNetworks.Clear(); options.KnownProxies.Clear(); }); // Настройка аутентификации (JWT + OIDC + Cookie) //builder.Services.AddGridientLocalAuth(builder.Configuration); //builder.Services.AddGridientOidcAuth(builder.Configuration); //builder.Services.AddGridientAuthLocalPostgresPersistence(builder.Configuration); //builder.Services.AddControllers() // .AddApplicationPart(typeof(AuthController).Assembly) // .AddApplicationPart(typeof(AuthOidcController).Assembly); //builder.Services.AddGridientAuthentication(builder.Configuration); // ============================================================================ // АЛЬТЕРНАТИВНЫЕ КОНФИГУРАЦИИ АУТЕНТИФИКАЦИИ (ЗАКОММЕНТИРОВАНЫ) // ============================================================================ // Раскомментируйте один из вариантов ниже для тестирования отдельных режимов: // // 1. ТОЛЬКО LOCAL (JWT) - для мобильных приложений и API-клиентов builder.Services.AddGridientLocalAuth(builder.Configuration); builder.Services.AddGridientAuthLocalPostgresPersistence(builder.Configuration); builder.Services.AddControllers() .AddApplicationPart(typeof(AuthController).Assembly) .AddApplicationPart(typeof(GroupsController).Assembly) .AddApplicationPart(typeof(AccessRightsController).Assembly); builder.Services.AddGridientLocalAuthentication(builder.Configuration); // // 2. ТОЛЬКО OIDC (Cookie) - для браузерных приложений // builder.Services.AddGridientOidcAuth(builder.Configuration); // builder.Services.AddControllers() // .AddApplicationPart(typeof(AuthOidcController).Assembly); // builder.Services.AddGridientOidcAuthentication(builder.Configuration); // ============================================================================ builder.Services.AddAuthorization(); builder.Services.AddGridientSwagger(); // Persistence layer registration builder.Services.AddGridientPostgresPersistence(builder.Configuration); builder.Services.AddGridientLocalAuthorizationPersistencePostgreSql(builder.Configuration); #if DEBUG // Configure DEBUG logging for DbContext - override the registration from extension method var listsDbContextDescriptor = builder.Services.FirstOrDefault(s => s.ServiceType == typeof(Microsoft.EntityFrameworkCore.DbContextOptions<Gridient.Infrastructure.Persistence.PostgreSql.Infrastructure.ListsDbContext>)); if (listsDbContextDescriptor is not null) { builder.Services.Remove(listsDbContextDescriptor); } builder.Services.AddDbContext<Gridient.Infrastructure.Persistence.PostgreSql.Infrastructure.ListsDbContext>(options => { options.UseNpgsql(builder.Configuration.GetConnectionString("Lists")); options.UseLoggerFactory(dbLoggerFactory); }); #endif builder.Services.AddGridientCoreServices(); builder.Services.AddGridientLocalAuthorization(); // Регистрация MediatR handlers из всех сборок builder.Services.AddGridientMediatRHandlers(); // Регистрация Pipeline Behaviors из Core builder.Services.AddGridientCoreBehaviors(); // Регистрация опциональной функциональности логирования безопасности // SecurityLoggingBehavior регистрируется между LoggingBehavior и AuthorizationBehavior builder.Services.AddScoped<ISecurityEventLogger, ConsoleSecurityEventLogger>(); builder.Services.AddSecurityLoggingBehavior(); builder.Services.AddResponseCaching(); builder.Logging.ConfigureLogging(); builder.Host.UseGridientNLog(); builder.Services.AddGridientOpenTelemetry(builder.Configuration); builder.Services.AddGridientApplicationOptions(builder.Configuration); builder.Services.AddMemoryCache(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Gridient v1"); }); } app.UseForwardedHeaders(); app.UseMiddleware<ExceptionMiddleware>(); app.UseRouting(); app.UseResponseCaching(); app.UseCors(CorsExtention.CORS_NAME); app.UseOpenTelemetryPrometheusScrapingEndpoint(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run(); } catch (Exception ex) { logger.Error(ex, "Stopped 'Gridient' service because of exception"); throw; } finally { LoggingConfiguration.ShutdownNLog(); }