/
rezvich
/
adapter_v
Обзор
Документация
Войти
/
rezvich
/
adapter_v
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Program.cs
101 строка
4 KB
waweda299
Add DB
22 дек 2025, 10:10
22 дек 2025, 10:10
459f88d
Код
Авторство
О чём код?
using adapter_v.Ferzl.Auth; using adapter_v.Repositories; using FluentMigrator.Runner; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Reflection; var host = Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(cfg => { cfg.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); cfg.AddEnvironmentVariables(); }) .ConfigureServices((ctx, services) => { services.AddSingleton<IConfiguration>(ctx.Configuration); // Миграции схемы БД (FluentMigrator) // Db:Provider = SqlServer | Postgres services.AddFluentMigratorCore() .ConfigureRunner(rb => { var provider = (ctx.Configuration.GetValue<string>("Db:Provider") ?? "SqlServer").Trim(); rb = provider.Equals("Postgres", StringComparison.OrdinalIgnoreCase) ? rb.AddPostgres() : rb.AddSqlServer(); rb.WithGlobalConnectionString(ctx.Configuration.GetConnectionString("Db")) .ScanIn(Assembly.GetExecutingAssembly()).For.Migrations(); }) .AddLogging(lb => lb.AddFluentMigratorConsole()); services.AddSingleton<IOutboxRepository, OutboxRepository>(); services.AddSingleton<IInboxRepository, InboxRepository>(); services.AddHttpClient("TokenService", http => { var baseUrl = ctx.Configuration.GetValue<string>("TokenService:BaseUrl") ?? "http://10.0.0.250:5005"; http.BaseAddress = new Uri(baseUrl); http.Timeout = TimeSpan.FromSeconds( ctx.Configuration.GetValue<int>("TokenService:TimeoutSeconds", 30) ); }); services.AddSingleton<IAccessTokenProvider>(sp => { var cfg = sp.GetRequiredService<IConfiguration>(); var apiKey = cfg.GetValue<string>("TokenService:ApiKey"); var ttlSeconds = cfg.GetValue<int>("TokenService:CacheTtlSeconds", 300); var skewSeconds = cfg.GetValue<int>("TokenService:CacheSkewSeconds", 10); var factory = sp.GetRequiredService<IHttpClientFactory>(); var http = factory.CreateClient("TokenService"); return new RemoteAccessTokenProvider( http, apiKey, tokenTtl: TimeSpan.FromSeconds(Math.Max(5, ttlSeconds)), safetySkew: TimeSpan.FromSeconds(Math.Max(0, skewSeconds))); }); // Typed HttpClient для SoapSender services.AddHttpClient<ISoapSender, SoapSender>((sp, http) => { var cfg = sp.GetRequiredService<IConfiguration>(); var soapSection = cfg.GetSection("Soap"); http.Timeout = TimeSpan.FromSeconds(soapSection.GetValue<int>("TimeoutSeconds", 120)); }) .ConfigurePrimaryHttpMessageHandler(sp => { var cfg = sp.GetRequiredService<IConfiguration>(); var soapSection = cfg.GetSection("Soap"); var dangerous = soapSection.GetValue<bool>("DangerousAcceptAnyServerCertificate"); var handler = new HttpClientHandler(); if (dangerous) { handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; } return handler; }); services.AddSingleton<Worker>(); services.AddHostedService<WorkerHostedService>(); }) .Build(); // Применяем миграции при старте процесса. // В production часто выносят это в отдельный шаг CI/CD, но для начала так проще. using (var scope = host.Services.CreateScope()) { var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>(); runner.MigrateUp(); } await host.RunAsync();