Files
devops-lab4/src/CommunicationControl/DevOpsProject.HiveMind.API/Program.cs

127 lines
3.8 KiB
C#
Raw Normal View History

2025-02-16 20:15:22 +00:00
using Asp.Versioning;
2025-02-18 09:47:50 +00:00
using Asp.Versioning.Builder;
2025-02-16 20:15:22 +00:00
using DevOpsProject.HiveMind.API.DI;
using DevOpsProject.HiveMind.API.Middleware;
2025-02-18 09:47:50 +00:00
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
2025-02-16 20:15:22 +00:00
using DevOpsProject.Shared.Clients;
using DevOpsProject.Shared.Configuration;
2025-02-18 09:47:50 +00:00
using DevOpsProject.Shared.Models;
using Microsoft.Extensions.Options;
2025-02-16 20:15:22 +00:00
using Microsoft.OpenApi.Models;
using Polly;
using Polly.Extensions.Http;
using Serilog;
2025-02-18 09:47:50 +00:00
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, services, loggerConfig) =>
loggerConfig.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext());
builder.Services.AddApiVersioning(options =>
2025-02-16 20:15:22 +00:00
{
2025-02-18 09:47:50 +00:00
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;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthorization();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "HiveMind - V1", Version = "v1.0" });
});
builder.Services.AddHiveMindLogic();
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
builder.Services.Configure<HiveCommunicationConfig>(builder.Configuration.GetSection("CommunicationConfiguration"));
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
var communicationControlTelemetryPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
builder.Services.AddHttpClient<HiveMindHttpClient>()
.AddPolicyHandler(communicationControlTelemetryPolicy);
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
// register NAMED client for connect request
builder.Services.AddHttpClient("HiveConnectClient");
string corsPolicyName = "HiveMindCorsPolicy";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: corsPolicyName,
policy =>
2025-02-16 20:15:22 +00:00
{
2025-02-18 09:47:50 +00:00
policy.AllowAnyOrigin() //SECURITY WARNING ! Never allow all origins
.AllowAnyMethod()
.AllowAnyHeader();
2025-02-16 20:15:22 +00:00
});
2025-02-18 09:47:50 +00:00
});
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
builder.Services.AddExceptionHandler<ExceptionHandlingMiddleware>();
builder.Services.AddProblemDetails();
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
var app = builder.Build();
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
using (var scope = app.Services.CreateScope())
{
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
try
{
var hiveMindService = scope.ServiceProvider.GetRequiredService<IHiveMindService>();
await hiveMindService.ConnectHive();
}
catch (Exception ex)
{
logger.LogError($"Error occured while connecting Hive to Communication Control. \nException text: {ex.Message}");
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
app.UseExceptionHandler();
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
app.UseCors(corsPolicyName);
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
//app.UseHttpsRedirection();
app.UseAuthorization();
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
ApiVersionSet apiVersionSet = app.NewApiVersionSet()
.HasApiVersion(new ApiVersion(1))
.ReportApiVersions()
.Build();
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
RouteGroupBuilder groupBuilder = app.MapGroup("api/v{apiVersion:apiVersion}").WithApiVersionSet(apiVersionSet);
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
groupBuilder.MapGet("ping", (IOptionsSnapshot<HiveCommunicationConfig> config) =>
{
return Results.Ok(new
{
Timestamp = DateTime.Now,
ID = config.Value.HiveID
});
});
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
groupBuilder.MapPost("command", (MoveHiveMindCommand command, IHiveMindMovingService hiveMindMovingService) =>
{
hiveMindMovingService.MoveToLocation(command.Location);
return Results.Ok();
});
2025-02-16 20:15:22 +00:00
2025-02-18 09:47:50 +00:00
app.Run();