Skip to content

Commit bc8e5b0

Browse files
Hubservice: start in required working directory as service, update /api/docs
1 parent 4aeb4f4 commit bc8e5b0

File tree

1 file changed

+33
-11
lines changed
  • src/Certify.Server/Certify.Server.HubService

1 file changed

+33
-11
lines changed

src/Certify.Server/Certify.Server.HubService/Program.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System.Reflection;
2+
using System.Runtime.InteropServices;
23
using Certify.Client;
34
using Certify.Management;
45
using Certify.Models;
56
using Certify.Models.Reporting;
7+
using Certify.Server.Core;
68
using Certify.Server.Hub.Api.Middleware;
79
using Certify.Server.Hub.Api.Services;
810
using Certify.Server.Hub.Api.SignalR;
@@ -21,8 +23,29 @@
2123
List<ActionStep> _systemStatusItems = [];
2224
void AddSystemStatusItem(string systemStatusCategory, string systemStatusKey, string title, string description, bool hasError = false, bool hasWarning = false) => _systemStatusItems.Add(new ActionStep(systemStatusKey, systemStatusCategory, title, description, hasError, hasWarning));
2325

26+
var assembly = typeof(Certify.Server.Hub.Api.Startup).Assembly;
27+
28+
// set working directory so that when we are started as a service we can find our config
29+
var cwd = Path.GetDirectoryName(assembly.Location);
30+
if (cwd != null)
31+
{
32+
System.Diagnostics.Debug.WriteLine($"Using working directory {cwd}");
33+
Directory.SetCurrentDirectory(cwd);
34+
}
35+
else
36+
{
37+
System.Diagnostics.Debug.WriteLine($"Could not determine working directory");
38+
}
39+
2440
var builder = WebApplication.CreateBuilder(args);
2541

42+
// if windows, run as service, otherwise run as console app
43+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
44+
{
45+
builder.Services.AddWindowsService()
46+
.AddHostedService<WindowsBackgroundService>();
47+
}
48+
2649
builder.AddServiceDefaults();
2750

2851
AddSystemStatusItem(
@@ -34,7 +57,6 @@
3457

3558
// Add services to the container
3659

37-
var assembly = typeof(Certify.Server.Hub.Api.Startup).Assembly;
3860
var part = new AssemblyPart(assembly);
3961

4062
builder.Services.AddCors(options =>
@@ -219,27 +241,27 @@
219241

220242
app.MapDefaultControllerRoute().WithStaticAssets();
221243

222-
// publish scalar api docs endpoint in dev, e.g. https://localhost:44361/scalar/
223-
app.MapOpenApi();
224-
app.MapScalarApiReference();
244+
// publish scalar api docs endpoint in dev, e.g. https://localhost:44361/api/docs
225245

226246
// Enable middleware to serve generated Swagger as a JSON endpoint.
227247
app.UseSwagger();
228248

229-
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
230-
// specifying the Swagger JSON endpoint.
231-
app.UseSwaggerUI(c =>
249+
// Enable middleware to serve API docs
250+
app.MapScalarApiReference("/api/docs/", options =>
232251
{
233-
c.RoutePrefix = "docs";
234-
c.DocumentTitle = "Certify Management Hub API";
235-
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Certify Management Hub API");
252+
options
253+
.WithTitle("Certify Management Hub API")
254+
.WithTheme(ScalarTheme.Solarized)
255+
.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient)
256+
.WithOpenApiRoutePattern("/swagger/v1/swagger.json");
257+
236258
});
237259

238260
AddSystemStatusItem(
239261
SystemStatusCategories.HUB_API,
240262
SystemStatusKeys.HUB_API_STARTUP_SWAGGER,
241263
title: "API Docs UI enabled",
242-
description: $"Hub API Swagger docs available at /docs"
264+
description: $"Hub API docs available at /api/docs"
243265
);
244266

245267
// configure initialization of UI status hub, backend management hub etc

0 commit comments

Comments
 (0)