Program.cs cleanup, HiveModel change, log level change
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
This commit is contained in:
@@ -3,13 +3,10 @@ using Asp.Versioning.Builder;
|
||||
using DevOpsProject.HiveMind.API.DI;
|
||||
using DevOpsProject.HiveMind.API.Middleware;
|
||||
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
|
||||
using DevOpsProject.Shared.Clients;
|
||||
using DevOpsProject.Shared.Configuration;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Polly;
|
||||
using Polly.Extensions.Http;
|
||||
using Serilog;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -19,52 +16,23 @@ builder.Host.UseSerilog((context, services, loggerConfig) =>
|
||||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext());
|
||||
|
||||
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;
|
||||
});
|
||||
builder.Services.AddApiVersioningConfiguration();
|
||||
|
||||
// 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.AddOptionsConfiguration(builder.Configuration);
|
||||
|
||||
builder.Services.AddHiveMindLogic();
|
||||
|
||||
builder.Services.Configure<HiveCommunicationConfig>(builder.Configuration.GetSection("CommunicationConfiguration"));
|
||||
|
||||
var communicationControlTelemetryPolicy = HttpPolicyExtensions
|
||||
.HandleTransientHttpError()
|
||||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
||||
builder.Services.AddHttpClient<HiveMindHttpClient>()
|
||||
.AddPolicyHandler(communicationControlTelemetryPolicy);
|
||||
|
||||
// register NAMED client for connect request
|
||||
builder.Services.AddHttpClient("HiveConnectClient");
|
||||
builder.Services.AddHttpClientsConfiguration();
|
||||
|
||||
string corsPolicyName = "HiveMindCorsPolicy";
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(name: corsPolicyName,
|
||||
policy =>
|
||||
{
|
||||
policy.AllowAnyOrigin() //SECURITY WARNING ! Never allow all origins
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
builder.Services.AddCorsConfiguration(corsPolicyName);
|
||||
|
||||
builder.Services.AddExceptionHandler<ExceptionHandlingMiddleware>();
|
||||
builder.Services.AddProblemDetails();
|
||||
@@ -82,7 +50,7 @@ using (var scope = app.Services.CreateScope())
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError($"Error occured while connecting Hive to Communication Control. \nException text: {ex.Message}");
|
||||
System.Diagnostics.Process.GetCurrentProcess().Kill();
|
||||
Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +73,6 @@ 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) =>
|
||||
|
||||
Reference in New Issue
Block a user