using System.Reflection; using System.Text; using FastEndpoints; using FastEndpoints.Swagger; using GB5Shared.Auth.Jwt; using GB5Shared.Connection; using GB5Shared.Telemetry; using GB5Shared.Vault; using PartnerBLL.Options; using GB5Shared.DateConverter; using static GB5Shared.DateConverter.GB5JsonOptions; using GB5Shared.DTO.Framework.CommonConfig; using GB5Shared.GB5CommonFunction; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Validation; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Caching.Hybrid; using OpenTelemetry; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using PartnerSL.Services; // ------------------------- // Bootstrap // ------------------------- var builder = WebApplication.CreateBuilder(args); // ── Port from config — change "AppPort" in appsettings.json to use any port ── var appPort = builder.Configuration.GetValue("AppPort"); builder.WebHost.UseUrls($"http://0.0.0.0:{appPort}"); // ------------------------- // Console Encoding // ------------------------- Console.OutputEncoding = Encoding.UTF8; // ------------------------- // Dapr & Controllers Setup // ------------------------- builder.Services.AddDaprClient(); builder.Services.AddControllers() .AddDapr() .AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = null; options.JsonSerializerOptions.DictionaryKeyPolicy = null; options.JsonSerializerOptions.AddGB5Converters(); }); // ------------------------- // Caching // ------------------------- #pragma warning disable EXTEXP0018 builder.Services.AddHybridCache(options => { options.DefaultEntryOptions = new HybridCacheEntryOptions(); options.DisableCompression = false; }); #pragma warning restore EXTEXP0018 builder.Services.AddMemoryCache(); builder.Services.AddDistributedMemoryCache(); // ------------------------- // CORS Policy // ------------------------- // ------------------------- // Application Services // ------------------------- builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.Configure( builder.Configuration.GetSection("Gb5SystemDTO")); // Vault + shared JWT issuer — backs the new Partner M2M client-credentials token endpoint // (GetPartnerToken). Partner only ISSUES tokens here; it never validates one, so no // AddAuthentication/AddJwtBearer scheme is needed in this host — Entitlement is the validator. builder.Services.AddGB5Vault(builder.Configuration); builder.Services.AddGB5JwtIssuer(); builder.Services.Configure(builder.Configuration.GetSection(PartnerM2MJwtOptions.SectionName)); builder.Services.Configure(options => { options.AllowSynchronousIO = true; }); // ------------------------- // OpenTelemetry // ------------------------- builder.Services.AddGB5Telemetry(builder.Configuration, "GB5-PARTNER"); // ------------------------- // Domain Cache (Singleton + IHostedService) // ------------------------- builder.Services.AddSingleton(); builder.Services.AddHostedService(sp => sp.GetRequiredService()); builder.Services.AddSingleton(sp => sp.GetRequiredService()); // ------------------------- // Assembly Scan for DI // ------------------------- var partnerBLLAssembly = Assembly.Load("PartnerBLL"); var partnerDALAssembly = Assembly.Load("PartnerDAL"); builder.Services.Scan(scan => scan .FromAssemblies(partnerBLLAssembly, partnerDALAssembly) .AddClasses(c => c.Where(t => !t.IsAbstract && !t.IsInterface && (t.Namespace == null || !t.Namespace.Contains(".DTO")))) .AsImplementedInterfaces() .WithScopedLifetime()); // ------------------------- // FastEndpoints // ------------------------- var endpointAssemblies = new[] { Assembly.Load("PartnerSL"), Assembly.Load("PartnerBLL"), Assembly.Load("PartnerDAL"), Assembly.Load("GB5Shared") }; builder.Services.AddFastEndpoints(o => { o.Assemblies = endpointAssemblies; }); // ------------------------- // Swagger // ------------------------- builder.Services.SwaggerDocument(o => { o.DocumentSettings = s => { s.Title = "GB5 Partner API"; s.Version = "v1"; s.Description = "GB5 Partner Service — White-Label Engine"; }; o.EnableJWTBearerAuth = false; o.ShortSchemaNames = true; }); // ------------------------- // Build App // ------------------------- var app = builder.Build(); // ------------------------- // Middleware // ------------------------- app.UseMiddleware(); app.UseMiddleware(); app.UseMiddleware(); app.UseFastEndpoints(c => { c.Serializer.Options.PropertyNamingPolicy = null; c.Serializer.Options.DictionaryKeyPolicy = null; c.Serializer.Options.AddGB5Converters(); }); app.MapControllers(); // ------------------------- // Swagger UI // ------------------------- app.UseOpenApi(); app.UseSwaggerUi(o => { o.Path = "/GB5Documentation"; o.DocumentPath = "/swagger/{documentName}/swagger.json"; o.TransformToExternalPath = (internalUiRoute, _) => "/partner" + internalUiRoute; }); // ------------------------- // Test Endpoint // ------------------------- app.MapGet("/", () => "Hello from GB5Partner (.NET 9)"); // ------------------------- // HTTPS & Authorization // ------------------------- app.UseAuthorization(); // ------------------------- // Run the Application // ------------------------- app.Run();