| 
									
										
										
										
											2025-02-16 20:15:22 +00:00
										 |  |  | using Asp.Versioning; | 
					
						
							| 
									
										
										
										
											2025-02-13 13:52:02 +02:00
										 |  |  | using DevOpsProject.CommunicationControl.API.DI; | 
					
						
							|  |  |  | using DevOpsProject.CommunicationControl.API.Middleware; | 
					
						
							|  |  |  | using DevOpsProject.Shared.Clients; | 
					
						
							| 
									
										
										
										
											2025-02-16 20:15:22 +00:00
										 |  |  | using DevOpsProject.Shared.Configuration; | 
					
						
							| 
									
										
										
										
											2025-02-13 13:52:02 +02:00
										 |  |  | using DevOpsProject.Shared.Models; | 
					
						
							|  |  |  | using Microsoft.Extensions.Options; | 
					
						
							| 
									
										
										
										
											2025-02-16 20:15:22 +00:00
										 |  |  | using Microsoft.OpenApi.Models; | 
					
						
							| 
									
										
										
										
											2025-02-13 13:52:02 +02:00
										 |  |  | using Polly; | 
					
						
							|  |  |  | using Polly.Extensions.Http; | 
					
						
							|  |  |  | using Serilog; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | internal class Program | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private static void Main(string[] args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         var builder = WebApplication.CreateBuilder(args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         builder.Host.UseSerilog((context, services, loggerConfig) => | 
					
						
							|  |  |  |             loggerConfig.ReadFrom.Configuration(context.Configuration) | 
					
						
							|  |  |  |                         .ReadFrom.Services(services) | 
					
						
							|  |  |  |                         .Enrich.FromLogContext()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 20:15:22 +00:00
										 |  |  |         builder.Services.AddApiVersioning(options => | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             options.DefaultApiVersion = new ApiVersion(1, 0); | 
					
						
							|  |  |  |             options.AssumeDefaultVersionWhenUnspecified = true; | 
					
						
							|  |  |  |             options.ReportApiVersions = true; | 
					
						
							|  |  |  |             options.ApiVersionReader = ApiVersionReader.Combine( | 
					
						
							|  |  |  |                 new UrlSegmentApiVersionReader(), | 
					
						
							|  |  |  |                 new HeaderApiVersionReader("X-Api-Version") | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         }).AddApiExplorer(options => | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             options.GroupNameFormat = "'v'VVV"; | 
					
						
							|  |  |  |             options.SubstituteApiVersionInUrl = true; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // TODO: consider this approach | 
					
						
							|  |  |  |         builder.Services.AddControllers().AddJsonOptions(options => | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             options.JsonSerializerOptions.PropertyNamingPolicy = null; | 
					
						
							|  |  |  |         });        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | 
					
						
							| 
									
										
										
										
											2025-02-13 13:52:02 +02:00
										 |  |  |         builder.Services.AddEndpointsApiExplorer(); | 
					
						
							| 
									
										
										
										
											2025-02-16 20:15:22 +00:00
										 |  |  |         builder.Services.AddSwaggerGen(c => | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             c.SwaggerDoc("v1", new OpenApiInfo { Title = "CommunicationControl - V1", Version = "v1.0" }); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2025-02-13 13:52:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // TODO: LATER - ADD OpenTelemtry | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         builder.Services.AddRedis(builder.Configuration); | 
					
						
							|  |  |  |         builder.Services.AddCommunicationControlLogic(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         builder.Services.Configure<OperationalAreaConfig>(builder.Configuration.GetSection("OperationalArea")); | 
					
						
							| 
									
										
										
										
											2025-02-16 20:15:22 +00:00
										 |  |  |         builder.Services.Configure<ComControlCommunicationConfiguration>(builder.Configuration.GetSection("CommunicationConfiguration")); | 
					
						
							| 
									
										
										
										
											2025-02-13 13:52:02 +02:00
										 |  |  |          | 
					
						
							|  |  |  |         var hiveRetryPolicy = HttpPolicyExtensions | 
					
						
							|  |  |  |             .HandleTransientHttpError() | 
					
						
							|  |  |  |             .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); | 
					
						
							| 
									
										
										
										
											2025-02-16 20:15:22 +00:00
										 |  |  |         builder.Services.AddHttpClient<CommunicationControlHttpClient>() | 
					
						
							| 
									
										
										
										
											2025-02-13 13:52:02 +02:00
										 |  |  |             .AddPolicyHandler(hiveRetryPolicy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var corsPolicyName = "AllowReactApp"; | 
					
						
							|  |  |  |         var localCorsPolicyName = "AllowLocalHtml"; | 
					
						
							|  |  |  |         builder.Services.AddCors(options => | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             options.AddPolicy(name: corsPolicyName, | 
					
						
							|  |  |  |                 policy => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     policy.AllowAnyOrigin() //SECURITY WARNING ! Never allow all origins | 
					
						
							|  |  |  |                         .AllowAnyMethod() | 
					
						
							|  |  |  |                         .AllowAnyHeader(); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             options.AddPolicy(name: localCorsPolicyName, | 
					
						
							|  |  |  |                 policy => | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     policy.AllowAnyOrigin() //SECURITY WARNING ! Never allow all origins | 
					
						
							|  |  |  |                         .AllowAnyMethod() | 
					
						
							|  |  |  |                         .AllowAnyHeader(); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         builder.Services.AddExceptionHandler<ExceptionHandlingMiddleware>(); | 
					
						
							|  |  |  |         builder.Services.AddProblemDetails(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var app = builder.Build(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         app.UseExceptionHandler(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (app.Environment.IsDevelopment()) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             app.UseSwagger(); | 
					
						
							|  |  |  |             app.UseSwaggerUI(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         app.UseCors(corsPolicyName); | 
					
						
							|  |  |  |         //app.UseCors(localCorsPolicyName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         app.UseAuthorization(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         app.MapControllers(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         app.Run(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |