Clean up HiveMind Minimal API setup - move configuration to extenstion methods, same for CommunicationControl, move RequestSchema for Hive to HiveModel, make ComControl use it, set minimum log level for files to Information
94 lines
2.6 KiB
C#
94 lines
2.6 KiB
C#
using Asp.Versioning;
|
|
using Asp.Versioning.Builder;
|
|
using DevOpsProject.HiveMind.API.DI;
|
|
using DevOpsProject.HiveMind.API.Middleware;
|
|
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
|
|
using DevOpsProject.Shared.Configuration;
|
|
using DevOpsProject.Shared.Models;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.OpenApi.Models;
|
|
using Serilog;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Host.UseSerilog((context, services, loggerConfig) =>
|
|
loggerConfig.ReadFrom.Configuration(context.Configuration)
|
|
.ReadFrom.Services(services)
|
|
.Enrich.FromLogContext());
|
|
|
|
builder.Services.AddApiVersioningConfiguration();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddAuthorization();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "HiveMind - V1", Version = "v1.0" });
|
|
});
|
|
|
|
builder.Services.AddOptionsConfiguration(builder.Configuration);
|
|
|
|
builder.Services.AddHiveMindLogic();
|
|
|
|
builder.Services.AddHttpClientsConfiguration();
|
|
|
|
string corsPolicyName = "HiveMindCorsPolicy";
|
|
builder.Services.AddCorsConfiguration(corsPolicyName);
|
|
|
|
builder.Services.AddExceptionHandler<ExceptionHandlingMiddleware>();
|
|
builder.Services.AddProblemDetails();
|
|
|
|
var app = builder.Build();
|
|
|
|
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}");
|
|
Environment.Exit(1);
|
|
}
|
|
}
|
|
|
|
app.UseExceptionHandler();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseCors(corsPolicyName);
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
ApiVersionSet apiVersionSet = app.NewApiVersionSet()
|
|
.HasApiVersion(new ApiVersion(1))
|
|
.ReportApiVersions()
|
|
.Build();
|
|
RouteGroupBuilder groupBuilder = app.MapGroup("api/v{apiVersion:apiVersion}").WithApiVersionSet(apiVersionSet);
|
|
|
|
groupBuilder.MapGet("ping", (IOptionsSnapshot<HiveCommunicationConfig> config) =>
|
|
{
|
|
return Results.Ok(new
|
|
{
|
|
Timestamp = DateTime.Now,
|
|
ID = config.Value.HiveID
|
|
});
|
|
});
|
|
|
|
groupBuilder.MapPost("command", (MoveHiveMindCommand command, IHiveMindMovingService hiveMindMovingService) =>
|
|
{
|
|
hiveMindMovingService.MoveToLocation(command.Location);
|
|
return Results.Ok();
|
|
});
|
|
|
|
app.Run();
|