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:
@@ -25,6 +25,7 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
|
||||
HiveID = request.HiveID,
|
||||
HiveIP = request.HiveIP,
|
||||
HivePort = request.HivePort,
|
||||
HiveSchema = request.HiveSchema
|
||||
};
|
||||
|
||||
var hiveOperationalArea = await _communicationControlService.ConnectHive(hiveModel);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using Asp.Versioning;
|
||||
|
||||
namespace DevOpsProject.CommunicationControl.API.DI
|
||||
{
|
||||
public static class ApiVersioningConfiguration
|
||||
{
|
||||
public static IServiceCollection AddApiVersioningConfiguration(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.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;
|
||||
});
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace DevOpsProject.CommunicationControl.API.DI
|
||||
{
|
||||
public static class CorsConfiguration
|
||||
{
|
||||
public static IServiceCollection AddCorsConfiguration(this IServiceCollection serviceCollection, string corsPolicyName)
|
||||
{
|
||||
serviceCollection.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(name: corsPolicyName,
|
||||
policy =>
|
||||
{
|
||||
policy.AllowAnyOrigin() //SECURITY WARNING ! Never allow all origins
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using DevOpsProject.Shared.Clients;
|
||||
using Polly;
|
||||
using Polly.Extensions.Http;
|
||||
|
||||
namespace DevOpsProject.CommunicationControl.API.DI
|
||||
{
|
||||
public static class HttpClientsConfiguration
|
||||
{
|
||||
public static IServiceCollection AddHttpClientsConfiguration(this IServiceCollection serviceCollection)
|
||||
{
|
||||
var hiveRetryPolicy = HttpPolicyExtensions
|
||||
.HandleTransientHttpError()
|
||||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
||||
serviceCollection.AddHttpClient<CommunicationControlHttpClient>()
|
||||
.AddPolicyHandler(hiveRetryPolicy);
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace DevOpsProject.CommunicationControl.API.DI
|
||||
{
|
||||
public static class JsonControllerOptionsConfiguration
|
||||
{
|
||||
public static IServiceCollection AddJsonControllerOptionsConfiguration(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy = null;
|
||||
});
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using DevOpsProject.Shared.Configuration;
|
||||
using DevOpsProject.Shared.Models;
|
||||
|
||||
namespace DevOpsProject.CommunicationControl.API.DI
|
||||
{
|
||||
public static class OptionsConfiguration
|
||||
{
|
||||
public static IServiceCollection AddOptionsConfiguration(this IServiceCollection serviceCollection, IConfiguration configuration)
|
||||
{
|
||||
serviceCollection.Configure<OperationalAreaConfig>(configuration.GetSection("OperationalArea"));
|
||||
serviceCollection.Configure<ComControlCommunicationConfiguration>(configuration.GetSection("CommunicationConfiguration"));
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,6 @@
|
||||
using Asp.Versioning;
|
||||
using DevOpsProject.CommunicationControl.API.DI;
|
||||
using DevOpsProject.CommunicationControl.API.Middleware;
|
||||
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;
|
||||
|
||||
internal class Program
|
||||
@@ -21,26 +14,11 @@ internal class Program
|
||||
.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();
|
||||
|
||||
// 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
|
||||
builder.Services.AddJsonControllerOptionsConfiguration();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
@@ -52,36 +30,12 @@ internal class Program
|
||||
builder.Services.AddRedis(builder.Configuration);
|
||||
builder.Services.AddCommunicationControlLogic();
|
||||
|
||||
builder.Services.Configure<OperationalAreaConfig>(builder.Configuration.GetSection("OperationalArea"));
|
||||
builder.Services.Configure<ComControlCommunicationConfiguration>(builder.Configuration.GetSection("CommunicationConfiguration"));
|
||||
|
||||
var hiveRetryPolicy = HttpPolicyExtensions
|
||||
.HandleTransientHttpError()
|
||||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
||||
builder.Services.AddHttpClient<CommunicationControlHttpClient>()
|
||||
.AddPolicyHandler(hiveRetryPolicy);
|
||||
builder.Services.AddOptionsConfiguration(builder.Configuration);
|
||||
|
||||
builder.Services.AddHttpClientsConfiguration();
|
||||
|
||||
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.AddCorsConfiguration(corsPolicyName);
|
||||
|
||||
builder.Services.AddExceptionHandler<ExceptionHandlingMiddleware>();
|
||||
builder.Services.AddProblemDetails();
|
||||
@@ -97,7 +51,6 @@ internal class Program
|
||||
}
|
||||
|
||||
app.UseCors(corsPolicyName);
|
||||
//app.UseCors(localCorsPolicyName);
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
@@ -14,10 +14,8 @@
|
||||
"InitialSpeed_KM": 5,
|
||||
"TelemetryInterval_MS": 30000,
|
||||
"PingInterval_MS": 15000
|
||||
|
||||
},
|
||||
"CommunicationConfiguration": {
|
||||
"RequestScheme": "http",
|
||||
"HiveMindPath": "api/v1"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
@@ -45,7 +43,7 @@
|
||||
"rollingInterval": "Day",
|
||||
"rollOnFileSizeLimit": true,
|
||||
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
|
||||
"restrictedToMinimumLevel": "Warning"
|
||||
"restrictedToMinimumLevel": "Information"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user