using FastEndpoints; using FastEndpoints.Swagger; using GB5Shared.GOP.Qualifier; using GB5Shared.Connection; using GB5Shared.DaprCache; using GB5Shared.DateConverter; using static GB5Shared.DateConverter.GB5JsonOptions; using GB5Shared.DTO.Framework.CommonConfig; using GB5Shared.EntityHandler; using GB5Shared.GB5CommonFunction; using GB5Shared.GB5Library.Qualifier; using GB5Shared.GenerateAutoNumber; using GB5Shared.ListQuery; using GB5Shared.PubSub.OutBox; using GB5Shared.QueryExecutor; using GB5Shared.Telemetry; using GB5Shared.Validation; using GB5Shared.WorkFlow.WorkFlowEngine; using GB5Shared.WorkFlow.WorkFlowRunTime; using Microsoft.AspNetCore.ResponseCompression; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Caching.Hybrid; using OpenTelemetry; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using QMSDAL.Query.Matrix; using System.Reflection; using System.Text; // ------------------------- // Bootstrap // ------------------------- var builder = WebApplication.CreateBuilder(args); Console.OutputEncoding = Encoding.UTF8; // ── 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}"); // ------------------------- // Dapr + Controllers // ------------------------- builder.Services.AddDaprClient(); builder.Services.AddControllers() .AddDapr() .AddJsonOptions(o => { o.JsonSerializerOptions.PropertyNamingPolicy = null; o.JsonSerializerOptions.DictionaryKeyPolicy = null; o.JsonSerializerOptions.AddGB5Converters(); }); builder.Services.ConfigureHttpJsonOptions(o => { o.SerializerOptions.PropertyNamingPolicy = null; o.SerializerOptions.DictionaryKeyPolicy = null; o.SerializerOptions.AddGB5Converters(); }); // ------------------------- // Caching // ------------------------- #pragma warning disable EXTEXP0018 builder.Services.AddHybridCache(o => { o.DefaultEntryOptions = new HybridCacheEntryOptions(); o.DisableCompression = false; }); #pragma warning restore EXTEXP0018 builder.Services.AddMemoryCache(); builder.Services.AddDistributedMemoryCache(); // ------------------------- // Core Services // ------------------------- builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // ------------------------- // GB5 Core Engine Services // ------------------------- // Generic entity handler builder.Services.AddScoped(typeof(BaseEntityAppService<>)); // ------------------------- // Qualifier // ------------------------- builder.Services.AddScoped(); builder.Services.AddGB5QualifierEngine(); // ------------------------- // Workflow // ------------------------- builder.Services.AddScoped(); // ✅ REQUIRED builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // ------------------------- // Config // ------------------------- builder.Services.Configure( builder.Configuration.GetSection("Gb5SystemDTO")); // ------------------------- // Response Compression // ------------------------- builder.Services.AddResponseCompression(o => { o.EnableForHttps = true; o.Providers.Add(); }); builder.Services.Configure(o => { o.Level = System.IO.Compression.CompressionLevel.Fastest; }); // ------------------------- // Kestrel // ------------------------- builder.Services.Configure(o => { o.AllowSynchronousIO = true; }); // ── OpenTelemetry (Tracing + Metrics) ──────────────────────────────── builder.Services.AddGB5Telemetry(builder.Configuration, "GB5-QMS"); // ------------------------- // Assembly Scan // ------------------------- var qmsBLLAssembly = Assembly.Load("QMSBLL"); var qmsDALAssembly = Assembly.Load("QMSDAL"); builder.Services.Scan(scan => scan .FromAssemblies(qmsBLLAssembly, qmsDALAssembly) .AddClasses(c => c.Where(t => !t.IsAbstract && !t.IsInterface && (t.Namespace == null || !t.Namespace.Contains(".DTO")))) .AsImplementedInterfaces() .WithScopedLifetime()); // ------------------------- // List Query Infra // ------------------------- builder.Services.AddListInfrastructure(qmsDALAssembly); // ------------------------- // FastEndpoints // ------------------------- builder.Services.AddFastEndpoints(); // ------------------------- // Swagger // ------------------------- builder.Services.SwaggerDocument(o => { o.DocumentSettings = s => { s.Title = "GB5 QMS API"; s.Version = "v1"; s.Description = "GB5 Quality Management System Microservice"; }; }); // ------------------------- // Build // ------------------------- var app = builder.Build(); // ------------------------- // Middleware // ------------------------- app.UseResponseCompression(); app.UseCloudEvents(); app.UseMiddleware(); app.UseMiddleware(); app.UseFastEndpoints(c => { c.Serializer.Options.PropertyNamingPolicy = null; c.Serializer.Options.DictionaryKeyPolicy = null; c.Serializer.Options.AddGB5Converters(); }); app.MapControllers(); app.MapSubscribeHandler(); // ------------------------- // Swagger UI // ------------------------- app.UseOpenApi(); app.UseSwaggerUi(o => { o.Path = "/GB5Documentation"; o.DocumentPath = "/swagger/{documentName}/swagger.json"; o.TransformToExternalPath = (internalUiRoute, _) => "/qms" + internalUiRoute; }); // ------------------------- // Test Endpoint // ------------------------- app.MapGet("/", () => "Hello from GB5 QMS API (.NET 9)"); // ------------------------- // Security // ------------------------- app.UseAuthorization(); // ------------------------- // Run // ------------------------- app.Run();