From dcb82414b9079cc9ea3b50ef700f565876197a9b Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Fri, 23 Aug 2024 18:55:05 +0200 Subject: [PATCH] Add nbgv and add server info endpoint --- Directory.Build.props | 9 +++++ src/WebApi/Endpoints/EndpointExtensions.cs | 3 ++ src/WebApi/Info/GetServerInfo.cs | 29 +++++++++++++++ .../Info/GetServerInfoTests.cs | 35 +++++++++++++++++++ vegasco-server.sln | 1 + version.json | 7 ++++ 6 files changed, 84 insertions(+) create mode 100644 Directory.Build.props create mode 100644 src/WebApi/Info/GetServerInfo.cs create mode 100644 tests/WebApi.Tests.Integration/Info/GetServerInfoTests.cs create mode 100644 version.json diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..5023fe7 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,9 @@ + + + + + all + 3.6.141 + + + \ No newline at end of file diff --git a/src/WebApi/Endpoints/EndpointExtensions.cs b/src/WebApi/Endpoints/EndpointExtensions.cs index 1481541..5cc9c9c 100644 --- a/src/WebApi/Endpoints/EndpointExtensions.cs +++ b/src/WebApi/Endpoints/EndpointExtensions.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Vegasco.WebApi.Cars; using Vegasco.WebApi.Common; using Vegasco.WebApi.Consumptions; +using Vegasco.WebApi.Info; namespace Vegasco.WebApi.Endpoints; @@ -46,5 +47,7 @@ public static class EndpointExtensions CreateConsumption.MapEndpoint(versionedApis); UpdateConsumption.MapEndpoint(versionedApis); DeleteConsumption.MapEndpoint(versionedApis); + + GetServerInfo.MapEndpoint(versionedApis); } } diff --git a/src/WebApi/Info/GetServerInfo.cs b/src/WebApi/Info/GetServerInfo.cs new file mode 100644 index 0000000..f63cd73 --- /dev/null +++ b/src/WebApi/Info/GetServerInfo.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Http.HttpResults; + +namespace Vegasco.WebApi.Info; + +public class GetServerInfo +{ + public record Response( + string FullVersion, + string CommitId, + DateTime CommitDate, + string Environment); + + public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder) + { + return builder + .MapGet("info/server", Endpoint) + .WithTags("Info"); + } + + private static Ok Endpoint( + IHostEnvironment environment) + { + return TypedResults.Ok(new Response( + ThisAssembly.AssemblyInformationalVersion, + ThisAssembly.GitCommitId, + ThisAssembly.GitCommitDate, + environment.EnvironmentName)); + } +} \ No newline at end of file diff --git a/tests/WebApi.Tests.Integration/Info/GetServerInfoTests.cs b/tests/WebApi.Tests.Integration/Info/GetServerInfoTests.cs new file mode 100644 index 0000000..1ce6a14 --- /dev/null +++ b/tests/WebApi.Tests.Integration/Info/GetServerInfoTests.cs @@ -0,0 +1,35 @@ +using System.Net.Http.Json; +using FluentAssertions; +using FluentAssertions.Extensions; +using Vegasco.WebApi.Info; + +namespace WebApi.Tests.Integration.Info; + +[Collection(SharedTestCollection.Name)] +public class GetServerInfoTests +{ + private readonly WebAppFactory _factory; + + public GetServerInfoTests(WebAppFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task GetServerInfo_ShouldReturnServerInfo_WhenCalled() + { + // Arrange + + // Act + using HttpResponseMessage response = await _factory.HttpClient.GetAsync("/v1/info/server"); + + // Assert + response.IsSuccessStatusCode.Should().BeTrue(); + var serverInfo = await response.Content.ReadFromJsonAsync(); + serverInfo!.Environment.Should().NotBeEmpty(); + serverInfo.CommitDate.Should().BeAfter(23.August(2024)) + .And.NotBeAfter(DateTime.Now); + serverInfo.CommitId.Should().MatchRegex(@"[0-9a-f]{40}"); + serverInfo.FullVersion.Should().MatchRegex(@"\d\.\d\.\d(-[0-9a-zA-Z]+)?(\+g?[0-9a-f]{10})?"); + } +} \ No newline at end of file diff --git a/vegasco-server.sln b/vegasco-server.sln index f91c064..7a363cd 100644 --- a/vegasco-server.sln +++ b/vegasco-server.sln @@ -10,6 +10,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A16251C2-47DB-4017-812B-CA18B280E049}" ProjectSection(SolutionItems) = preProject README.md = README.md + version.json = version.json EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{437DE053-1DAB-4EEF-BEA6-E3B5179692F8}" diff --git a/version.json b/version.json new file mode 100644 index 0000000..4aa0bc2 --- /dev/null +++ b/version.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json", + "version": "0.3", + "release": { + "firstUnstableTag": "alpha" + } +} \ No newline at end of file