GetSystemVersion

master
Tudor Stanciu 2022-03-03 12:22:23 +02:00
parent 8b6aa4878c
commit 3267cc8a0d
6 changed files with 80 additions and 12 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="$(AutoMapperPackageVersion)" />
<PackageReference Include="MediatR" Version="$(MediatRPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />

View File

@ -0,0 +1,8 @@
using MediatR;
namespace ProxmoxConnector.Server.Application.Queries
{
public abstract class Query<TResponse> : IRequest<TResponse>, IBaseRequest
{
}
}

View File

@ -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<Model>
{
public Query()
{
}
}
public class Model
{
public string Version { get; set; }
}
public class QueryHandler : IRequestHandler<Query, Model>
{
public QueryHandler()
{
}
public async Task<Model> 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<AssemblyInformationalVersionAttribute>().InformationalVersion;
}
}
}

View File

@ -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<HealthController> _logger;
private readonly IMediator _mediator;
public HealthController(ILogger<HealthController> logger)
public HealthController(ILogger<HealthController> 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<IActionResult> GetSystemVersion([FromRoute] GetSystemVersion.Query query)
{
var result = await _mediator.Send(query);
return Ok(result);
}
}
}

View File

@ -12,6 +12,7 @@
<PackageReference Include="NDB.Security.Authentication.Identity" Version="$(NDBSecurityAuthenticationIdentityPackageVersion)" />
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogAspNetCorePackageVersion)" />
<PackageReference Include="Serilog.Sinks.SQLite" Version="$(SerilogSinksSQLitePackageVersion)" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="$(MediatRPackageVersion)" />
</ItemGroup>
<ItemGroup>

View File

@ -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<IUserService, UserService>();
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 };
}
}
}