Răsfoiți Sursa

Better abstraction for the version information

- Including a lightweight plugin system for context information
Lukas Angerer 1 an în urmă
părinte
comite
ad7ce05d05

+ 1 - 1
WebTemplate/AppServer.cs

@@ -23,7 +23,7 @@ public class AppServer
         new JsonModule(),
         new JsonModule(),
         new SwaggerModule(),
         new SwaggerModule(),
         new ValidationModule(),
         new ValidationModule(),
-        new StatusEndpointModule(),
+        new StatusEndpointModule(_ => new GitVersionInfo()),
         new ControllersModule(),
         new ControllersModule(),
         // Keep the SpaRoutingModule at the very end
         // Keep the SpaRoutingModule at the very end
         new SpaRoutingModule(),
         new SpaRoutingModule(),

+ 24 - 0
WebTemplate/GitVersionInfo.cs

@@ -0,0 +1,24 @@
+using System.Reflection;
+
+namespace WebTemplate;
+
+public record GitVersionInfo : IVersionInfo
+{
+    public string AssemblyName { get; }
+    public string AssemblyVersion { get; }
+    public string Version { get; }
+    public string Commit { get; }
+    public string Branch { get; }
+    public bool IsDirty { get; }
+    
+    public GitVersionInfo()
+    {
+        var entryAssembly = Assembly.GetEntryAssembly();
+        AssemblyName = entryAssembly?.GetName().Name ?? "<unknown>";
+        AssemblyVersion = entryAssembly?.GetName().Version?.ToString() ?? "<unknown>";
+        Version = ThisAssembly.Git.Tag;
+        Commit = ThisAssembly.Git.Sha;
+        Branch = ThisAssembly.Git.Branch;
+        IsDirty = ThisAssembly.Git.IsDirty;
+    }
+}

+ 11 - 0
WebTemplate/IVersionInfo.cs

@@ -0,0 +1,11 @@
+namespace WebTemplate;
+
+public interface IVersionInfo
+{
+    string AssemblyName { get; }
+    string AssemblyVersion { get; }
+    string Version { get; }
+    string Commit { get; }
+    string Branch { get; }
+    bool IsDirty { get; }
+}

+ 7 - 0
WebTemplate/Status/IStatusReportProvider.cs

@@ -0,0 +1,7 @@
+namespace WebTemplate.Status;
+
+public interface IStatusReportProvider
+{
+    public string Key { get; }
+    public object StatusReport();
+}

+ 3 - 3
WebTemplate/Status/ServiceStatus.cs

@@ -1,4 +1,4 @@
-namespace WebTemplate.Status;
+namespace WebTemplate.Status;
 
 
 /// <summary>
 /// <summary>
 /// Overall service status result.
 /// Overall service status result.
@@ -6,5 +6,5 @@
 /// <param name="Status">The value "OK" if everything is OK. Otherwise this service will not actually return a 200
 /// <param name="Status">The value "OK" if everything is OK. Otherwise this service will not actually return a 200
 /// HTTP result</param>
 /// HTTP result</param>
 /// <param name="Version">Detailed version information</param>
 /// <param name="Version">Detailed version information</param>
-/// <param name="Environment">Collection of environment variables. Only enabled in _Development_ mode</param>
-public record ServiceStatus(string Status, VersionInfo Version, EnvironmentInfo Environment);
+/// <param name="Context">Collection of contextual information provided by different modules</param>
+public record ServiceStatus(string Status, IVersionInfo Version, Dictionary<string, object> Context);

+ 14 - 4
WebTemplate/Status/StatusEndpointModule.cs

@@ -1,24 +1,34 @@
-namespace WebTemplate.Status;
+namespace WebTemplate.Status;
 
 
 public class StatusEndpointModule : IAppConfigurationModule
 public class StatusEndpointModule : IAppConfigurationModule
 {
 {
+    private readonly Func<IServiceProvider, IVersionInfo> _versionProvider;
+
+    public StatusEndpointModule(Func<IServiceProvider, IVersionInfo> versionProvider)
+    {
+        _versionProvider = versionProvider;
+    }
+    
     public void ConfigureServices(IServiceCollection services, IConfigurationRoot config)
     public void ConfigureServices(IServiceCollection services, IConfigurationRoot config)
     {
     {
+        services.AddTransient<IVersionInfo>(_versionProvider);
     }
     }
 
 
     public void ConfigureApplication(WebApplication app)
     public void ConfigureApplication(WebApplication app)
     {
     {
-        app.MapGet("/v1/status", () => new ServiceStatus("OK", new VersionInfo(), new EnvironmentInfo(app.Environment)))
+        var reporter = new StatusReporter();
+
+        app.MapGet("v1/status", (IVersionInfo version, IServiceProvider serviceProvider) => new ServiceStatus("OK", version, reporter.StatusReport(serviceProvider)))
             .WithName("StatusService")
             .WithName("StatusService")
             .WithOpenApi(operation =>
             .WithOpenApi(operation =>
             {
             {
                 operation.Description =
                 operation.Description =
                     """
                     """
-                    Returns the overall service status, including version and environment information. This can be used
+                    Returns the overall service status, including version and context information. This can be used
                     as a health check endpoint.
                     as a health check endpoint.
                     """;
                     """;
 
 
                 return operation;
                 return operation;
             });
             });
     }
     }
-}
+}

+ 17 - 0
WebTemplate/Status/StatusReporter.cs

@@ -0,0 +1,17 @@
+namespace WebTemplate.Status;
+
+public class StatusReporter
+{
+    public Dictionary<string, object> StatusReport(IServiceProvider serviceProvider)
+    {
+        var result = new Dictionary<string, object>();
+        var providers = serviceProvider.GetServices<IStatusReportProvider>().OrderBy(p => p.Key).ToList();
+
+        foreach (var provider in providers)
+        {
+            result[provider.Key] = provider.StatusReport();
+        }
+
+        return result;
+    }
+}

+ 0 - 19
WebTemplate/Status/VersionInfo.cs

@@ -1,19 +0,0 @@
-namespace WebTemplate.Status;
-
-public record VersionInfo
-{
-    public string AssemblyVersion { get; }
-    public string Version { get; }
-    public string Commit { get; }
-    public string Branch { get; }
-    public bool IsDirty { get; }
-    
-    public VersionInfo()
-    {
-        AssemblyVersion = typeof(VersionInfo).Assembly.GetName().Version?.ToString() ?? "<unknown>";
-        Version = ThisAssembly.Git.Tag;
-        Commit = ThisAssembly.Git.Sha;
-        Branch = ThisAssembly.Git.Branch;
-        IsDirty = ThisAssembly.Git.IsDirty;
-    }
-}