| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | using Elecciones.Database; | 
					
						
							|  |  |  | using Microsoft.EntityFrameworkCore; | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | using Serilog; | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | // Esta es la estructura estándar y recomendada. | 
					
						
							| 
									
										
										
										
											2025-08-14 12:37:57 -03:00
										 |  |  | var builder = WebApplication.CreateBuilder(args); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | // 1. Configurar Serilog. Esta es la forma correcta de integrarlo. | 
					
						
							|  |  |  | builder.Host.UseSerilog((context, services, configuration) => configuration | 
					
						
							|  |  |  |     .ReadFrom.Configuration(context.Configuration) | 
					
						
							|  |  |  |     .ReadFrom.Services(services) | 
					
						
							|  |  |  |     .Enrich.FromLogContext() | 
					
						
							|  |  |  |     .WriteTo.Console() | 
					
						
							|  |  |  |     .WriteTo.File("logs/api-.log", rollingInterval: RollingInterval.Day)); | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | // 2. Añadir servicios al contenedor. | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); | 
					
						
							|  |  |  | builder.Services.AddDbContext<EleccionesDbContext>(options => | 
					
						
							|  |  |  |     options.UseSqlServer(connectionString)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | builder.Services.AddControllers(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | var allowedOrigins = builder.Configuration["AllowedOrigins"]?.Split(',') ?? Array.Empty<string>(); | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | builder.Services.AddCors(options => | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     options.AddDefaultPolicy(policy => | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-14 15:51:19 -03:00
										 |  |  |         if (allowedOrigins.Any()) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             policy.WithOrigins(allowedOrigins) | 
					
						
							|  |  |  |                   .AllowAnyHeader() | 
					
						
							|  |  |  |                   .AllowAnyMethod(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | builder.Services.AddEndpointsApiExplorer(); | 
					
						
							|  |  |  | builder.Services.AddSwaggerGen(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-14 12:37:57 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | // 3. Construir la aplicación. | 
					
						
							| 
									
										
										
										
											2025-08-14 12:37:57 -03:00
										 |  |  | var app = builder.Build(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | // 4. Configurar el pipeline de peticiones HTTP. | 
					
						
							|  |  |  | // Añadimos el logging de peticiones de Serilog aquí. | 
					
						
							|  |  |  | app.UseSerilogRequestLogging(); | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-14 12:37:57 -03:00
										 |  |  | if (app.Environment.IsDevelopment()) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  |     app.UseSwagger(); | 
					
						
							|  |  |  |     app.UseSwaggerUI(); | 
					
						
							| 
									
										
										
										
											2025-08-14 12:37:57 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.UseHttpsRedirection(); | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | app.UseCors(); | 
					
						
							|  |  |  | app.UseAuthorization(); | 
					
						
							|  |  |  | app.MapControllers(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-15 17:31:51 -03:00
										 |  |  | // 5. Ejecutar la aplicación. | 
					
						
							|  |  |  | // El try/catch/finally se puede omitir; el logging de Serilog ya se encarga de los errores fatales. | 
					
						
							| 
									
										
										
										
											2025-08-14 13:12:16 -03:00
										 |  |  | app.Run(); |