| | | 1 | | using ClientManager.API.Services; |
| | | 2 | | using ClientManager.Shared.Configuration; |
| | | 3 | | using ClientManager.Shared.Data; |
| | | 4 | | using ClientManager.Shared.Messaging; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | using Serilog; |
| | | 8 | | |
| | 0 | 9 | | var builder = WebApplication.CreateBuilder(args); |
| | | 10 | | |
| | | 11 | | // Logging |
| | 0 | 12 | | builder.Host.UseSerilog((context, loggerConfig) => loggerConfig.ReadFrom.Configuration(context.Configuration).Enrich.Fro |
| | | 13 | | |
| | | 14 | | // Load .env file if it exists |
| | 0 | 15 | | ConfigurationHelper.LoadDotEnvFile(); |
| | 0 | 16 | | builder.Configuration.AddEnvironmentVariables(); |
| | | 17 | | |
| | | 18 | | // Map configuration sections to strongly typed classes |
| | 0 | 19 | | builder.Services.Configure<PostgresConnectionConfiguration>(builder.Configuration.GetSection("POSTGRES")); |
| | 0 | 20 | | builder.Services.Configure<RabbitMQConnectionConfiguration>(builder.Configuration.GetSection("RABBITMQ")); |
| | | 21 | | |
| | | 22 | | // Add services to the container. |
| | | 23 | | // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi |
| | 0 | 24 | | builder.Services.AddEndpointsApiExplorer(); |
| | 0 | 25 | | builder.Services.AddSwaggerGen(); |
| | 0 | 26 | | builder.Services.AddControllers(); |
| | 0 | 27 | | builder.Services.AddSignalR(); |
| | 0 | 28 | | builder.Services.AddCors(); |
| | 0 | 29 | | builder.Services.AddHealthChecks(); |
| | 0 | 30 | | builder.Services.AddHostedService<EventForwarder>(); |
| | 0 | 31 | | builder.Services.AddScoped<IClientService, ClientService>(); |
| | | 32 | | |
| | | 33 | | // Context accessor using AsyncLocal to store per-message context |
| | 0 | 34 | | builder.Services.AddSingleton<IMessageContextAccessor, MessageContextAccessor>(); |
| | | 35 | | |
| | | 36 | | // Core publishing components |
| | 0 | 37 | | builder.Services.AddSingleton<IMessagePublisher, MessagePublisher>(); |
| | 0 | 38 | | builder.Services.AddSingleton<IRoutingConvention, RoutingConvention>(); |
| | | 39 | | |
| | | 40 | | // Message Publishing middleware |
| | 0 | 41 | | builder.Services.AddSingleton<IMessagePublishMiddleware, MessageValidationMiddleware>(); |
| | 0 | 42 | | builder.Services.AddSingleton<IMessagePublishMiddleware, ContextEnrichmentMiddleware>(); |
| | 0 | 43 | | builder.Services.AddSingleton<MessagePublishPipeline>(sp => |
| | 0 | 44 | | { |
| | 0 | 45 | | var middlewares = sp.GetServices<IMessagePublishMiddleware>(); |
| | 0 | 46 | | var pipeline = new MessagePublishPipeline(); |
| | 0 | 47 | | |
| | 0 | 48 | | foreach (var middleware in middlewares.Reverse()) |
| | 0 | 49 | | { |
| | 0 | 50 | | pipeline.Use(middleware); |
| | 0 | 51 | | } |
| | 0 | 52 | | |
| | 0 | 53 | | return pipeline; |
| | 0 | 54 | | }); |
| | | 55 | | |
| | | 56 | | // Broker and database connections |
| | 0 | 57 | | builder.Services.AddSingleton<IMessageBrokerFactory>(sp => |
| | 0 | 58 | | { |
| | 0 | 59 | | // Get RabbitMQ connection configuration from DI |
| | 0 | 60 | | var connectionConfiguration = sp.GetRequiredService<IOptions<RabbitMQConnectionConfiguration>>().Value; |
| | 0 | 61 | | return new MessageBrokerFactory(connectionConfiguration); |
| | 0 | 62 | | }); |
| | 0 | 63 | | builder.Services.AddDbContext<AppDbContext, ReadOnlyAppDbContext>( |
| | 0 | 64 | | (sp, options) => |
| | 0 | 65 | | { |
| | 0 | 66 | | // Get Postgres connection configuration from DI |
| | 0 | 67 | | var postgresConfig = sp.GetRequiredService<IOptions<PostgresConnectionConfiguration>>().Value; |
| | 0 | 68 | | options.UseNpgsql(postgresConfig.ToConnectionString()); |
| | 0 | 69 | | } |
| | 0 | 70 | | ); |
| | | 71 | | |
| | 0 | 72 | | var app = builder.Build(); |
| | | 73 | | |
| | | 74 | | // Configure the HTTP request pipeline. |
| | 0 | 75 | | if (app.Environment.IsDevelopment()) |
| | | 76 | | { |
| | | 77 | | // Go to http://localhost:5200/swagger/index.html to test the API |
| | 0 | 78 | | app.UseSwagger(); |
| | 0 | 79 | | app.UseSwaggerUI(c => |
| | 0 | 80 | | { |
| | 0 | 81 | | c.SwaggerEndpoint("/swagger/v1/swagger.json", "ClientManager API v1"); |
| | 0 | 82 | | }); |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | app.UseAuthorization(); |
| | 0 | 86 | | app.MapControllers(); |
| | 0 | 87 | | app.MapHub<NotificationHub>("/notifications"); |
| | 0 | 88 | | app.MapControllers(); |
| | 0 | 89 | | app.MapHealthChecks("/health"); |
| | | 90 | | |
| | 0 | 91 | | app.UseCors(options => options.WithOrigins("http://localhost:5173").AllowAnyMethod().AllowAnyHeader().AllowCredentials() |
| | | 92 | | |
| | 0 | 93 | | app.Run(); |