HiveMind PoC + P1, P3 fixes

This commit is contained in:
Даниил Соколов-Рудаков
2025-02-16 20:15:22 +00:00
committed by Kirill
parent 1744425d5f
commit 9f6c1f5135
45 changed files with 1024 additions and 119 deletions

View File

@@ -0,0 +1,49 @@
using Asp.Versioning;
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
using DevOpsProject.Shared.Configuration;
using DevOpsProject.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace DevOpsProject.HiveMind.API.Controllers
{
[ApiVersion("1.0")]
[ApiController]
[Route("api/v{version:apiVersion}")]
public class HiveMindController : Controller
{
private readonly IHiveMindService _hiveMindService;
private readonly IHiveMindMovingService _hiveMindMovingService;
public HiveMindController(IHiveMindService hiveMindService, IHiveMindMovingService hiveMindMovingService)
{
_hiveMindService = hiveMindService;
_hiveMindMovingService = hiveMindMovingService;
}
[HttpGet("ping")]
public IActionResult Ping(IOptionsSnapshot<HiveCommunicationConfig> config)
{
return Ok(new
{
Timestamp = DateTime.Now,
ID = config.Value.HiveID
});
}
[HttpPost("connect")]
public async Task<IActionResult> TriggerConnectHive()
{
await _hiveMindService.ConnectHive();
return Ok();
}
[HttpPost("command")]
public IActionResult MoveHideMind(MoveHiveMindCommand command)
{
_hiveMindMovingService.MoveToLocation(command.Location);
return Ok();
}
}
}

View File

@@ -0,0 +1,16 @@
using DevOpsProject.HiveMind.Logic.Services;
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
namespace DevOpsProject.HiveMind.API.DI
{
public static class LogicConfiguration
{
public static IServiceCollection AddHiveMindLogic(this IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<IHiveMindService, HiveMindService>();
serviceCollection.AddTransient<IHiveMindMovingService, HiveMindMovingService>();
return serviceCollection;
}
}
}

View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.2" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DevOpsProject.HiveMind.Logic\DevOpsProject.HiveMind.Logic.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@DevOpsProject.HiveMind.API_HostAddress = http://localhost:5149
GET {{DevOpsProject.HiveMind.API_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Diagnostics;
using System.Text.Json;
namespace DevOpsProject.HiveMind.API.Middleware
{
public class ExceptionHandlingMiddleware : IExceptionHandler
{
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
private readonly IHostEnvironment _hostEnvironment;
public ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger, IHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
_logger = logger;
}
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
_logger.LogError(exception, "Unhandled exception occured: {Message}", exception.Message);
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
httpContext.Response.ContentType = "application/json";
var errorResponse = new
{
Message = "Unexpected error occured",
Detail = _hostEnvironment.IsDevelopment() ? exception.ToString() : null
};
var jsonResponse = JsonSerializer.Serialize(errorResponse, new JsonSerializerOptions { WriteIndented = true });
await httpContext.Response.WriteAsync(jsonResponse, cancellationToken);
return true;
}
}
}

View File

@@ -0,0 +1,94 @@
using Asp.Versioning;
using DevOpsProject.HiveMind.API.DI;
using DevOpsProject.HiveMind.API.Middleware;
using DevOpsProject.Shared.Clients;
using DevOpsProject.Shared.Configuration;
using Microsoft.OpenApi.Models;
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());
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: double check following approach
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "HiveMind - V1", Version = "v1.0" });
});
builder.Services.AddHiveMindLogic();
builder.Services.Configure<HiveCommunicationConfig>(builder.Configuration.GetSection("CommunicationConfiguration"));
var communicationControlRetryPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
builder.Services.AddHttpClient<HiveMindHttpClient>()
.AddPolicyHandler(communicationControlRetryPolicy);
string corsPolicyName = "HiveMindCorsPolicy";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: corsPolicyName,
policy =>
{
policy.AllowAnyOrigin() //SECURITY WARNING ! Never allow all origins
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddExceptionHandler<ExceptionHandlingMiddleware>();
builder.Services.AddProblemDetails();
var app = builder.Build();
app.UseExceptionHandler();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors(corsPolicyName);
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:39179",
"sslPort": 44372
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5149",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7167;http://localhost:5149",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,49 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"System": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"restrictedToMinimumLevel": "Information"
}
},
{
"Name": "File",
"Args": {
"path": "Logs/log-.txt",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
"restrictedToMinimumLevel": "Warning"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"Properties": {
"Application": "DevOpsProject.HiveMind",
"Environment": "Development"
}
},
"CommunicationConfiguration": {
"RequestSchema": "http",
"CommunicationControlIP": "localhost",
"CommunicationControlPort": 8080,
"CommunicationControlPath": "api/v1/hive",
"HiveIP": "localhost",
"HivePort": 5149,
"HiveID": "1",
"InitialLocation": {
"Latitude": 48.719547,
"Longitude": 38.092680
}
},
"AllowedHosts": "*"
}