diff --git a/src/server/ProxmoxConnector.Server.Application/ProxmoxConnector.Server.Application.csproj b/src/server/ProxmoxConnector.Server.Application/ProxmoxConnector.Server.Application.csproj index ce8567b..7df9d31 100644 --- a/src/server/ProxmoxConnector.Server.Application/ProxmoxConnector.Server.Application.csproj +++ b/src/server/ProxmoxConnector.Server.Application/ProxmoxConnector.Server.Application.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -6,6 +6,7 @@ + diff --git a/src/server/ProxmoxConnector.Server.Application/Queries/Query.cs b/src/server/ProxmoxConnector.Server.Application/Queries/Query.cs new file mode 100644 index 0000000..c7248c1 --- /dev/null +++ b/src/server/ProxmoxConnector.Server.Application/Queries/Query.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace ProxmoxConnector.Server.Application.Queries +{ + public abstract class Query : IRequest, IBaseRequest + { + } +} diff --git a/src/server/ProxmoxConnector.Server.Application/Queries/System/GetSystemVersion.cs b/src/server/ProxmoxConnector.Server.Application/Queries/System/GetSystemVersion.cs new file mode 100644 index 0000000..68c5f0c --- /dev/null +++ b/src/server/ProxmoxConnector.Server.Application/Queries/System/GetSystemVersion.cs @@ -0,0 +1,39 @@ +using MediatR; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +namespace ProxmoxConnector.Server.Application.Queries.System +{ + public class GetSystemVersion + { + public class Query : Query + { + public Query() + { + } + } + + public class Model + { + public string Version { get; set; } + } + + public class QueryHandler : IRequestHandler + { + public QueryHandler() + { + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var version = GetVersion(); + var result = new Model { Version = version }; + return await Task.FromResult(result); + } + + private string GetVersion() => + Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion; + } + } +} diff --git a/src/server/ProxmoxConnector.Server/Controllers/HealthController.cs b/src/server/ProxmoxConnector.Server/Controllers/HealthController.cs index 50d03b5..3baae81 100644 --- a/src/server/ProxmoxConnector.Server/Controllers/HealthController.cs +++ b/src/server/ProxmoxConnector.Server/Controllers/HealthController.cs @@ -1,7 +1,10 @@ -using Microsoft.AspNetCore.Authorization; +using MediatR; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using ProxmoxConnector.Server.Application.Queries.System; using System; +using System.Threading.Tasks; namespace ProxmoxConnector.Server.Controllers { @@ -11,10 +14,12 @@ namespace ProxmoxConnector.Server.Controllers public class HealthController : ControllerBase { private readonly ILogger _logger; + private readonly IMediator _mediator; - public HealthController(ILogger logger) + public HealthController(ILogger logger, IMediator mediator) { _logger = logger; + _mediator = mediator; } [AllowAnonymous] @@ -22,7 +27,15 @@ namespace ProxmoxConnector.Server.Controllers public IActionResult Ping() { _logger.LogInformation("Ping"); - return Ok($"System datetime: {DateTime.Now}"); + return Ok($"Ping success. System datetime: {DateTime.Now}"); + } + + [AllowAnonymous] + [HttpGet("version")] + public async Task GetSystemVersion([FromRoute] GetSystemVersion.Query query) + { + var result = await _mediator.Send(query); + return Ok(result); } } } diff --git a/src/server/ProxmoxConnector.Server/ProxmoxConnector.Server.csproj b/src/server/ProxmoxConnector.Server/ProxmoxConnector.Server.csproj index cd94eb2..00330fc 100644 --- a/src/server/ProxmoxConnector.Server/ProxmoxConnector.Server.csproj +++ b/src/server/ProxmoxConnector.Server/ProxmoxConnector.Server.csproj @@ -12,6 +12,7 @@ + diff --git a/src/server/ProxmoxConnector.Server/Startup.cs b/src/server/ProxmoxConnector.Server/Startup.cs index 40297b5..36979c1 100644 --- a/src/server/ProxmoxConnector.Server/Startup.cs +++ b/src/server/ProxmoxConnector.Server/Startup.cs @@ -1,11 +1,9 @@ +using MediatR; +using MediatR.Pipeline; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.OpenApi.Models; using NDB.Extensions.Swagger; using NDB.Extensions.Swagger.Constants; using NDB.Infrastructure.DatabaseMigration; @@ -14,10 +12,7 @@ using Newtonsoft.Json; using ProxmoxConnector.Server.Application; using ProxmoxConnector.Server.Domain.Abstractions; using ProxmoxConnector.Server.Services; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Reflection; namespace ProxmoxConnector.Server { @@ -38,6 +33,11 @@ namespace ProxmoxConnector.Server // Add basic authentication services.AddIdentityAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]); + // MediatR + services.AddMediatR(GetMediatRAssemblies()); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>)); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>)); + services.AddHttpContextAccessor(); services.AddScoped(); services.AddSwagger("ProxmoxConnector.Server", AuthorizationType.InhouseIdentity); @@ -69,5 +69,11 @@ namespace ProxmoxConnector.Server app.UseMigration(); } + + private Assembly[] GetMediatRAssemblies() + { + var assembly = typeof(Application.Queries.System.GetSystemVersion).Assembly; + return new Assembly[] { assembly }; + } } }