Skip to content

Commit 6beb1c8

Browse files
Hub Service: allow invalid config and log problems
1 parent 7aafb21 commit 6beb1c8

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

src/Certify.Models/Reporting/SystemStatusCategories.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class SystemStatusKeys
1616
public const string HUB_API_STARTUP_SVCPORTENV = "hub.api.startup.serviceportenv";
1717
public const string HUB_API_STARTUP_ENVIRONMENT = "hub.api.startup.environment";
1818
public const string HUB_API_STARTUP_URL = "hub.api.startup.url";
19-
public const string HUB_API_STARTUP_SWAGGER = "hub.api.startup.swagger";
19+
public const string HUB_API_STARTUP_CUSTOMCONFIG = "hub.api.startup.customconfig";
20+
public const string HUB_API_STARTUP_APIDOCS = "hub.api.startup.apidocs";
2021
public const string HUB_API_STARTUP_SVC_STATUS_STREAM = "hub.api.startup.svc.stream";
2122

2223
public const string SERVICE_CORE_PLATFORM = "service.core.platform";

src/Certify.Server/Certify.Server.Hub.Api/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
300300

301301
AddSystemStatusItem(
302302
SystemStatusCategories.HUB_API,
303-
SystemStatusKeys.HUB_API_STARTUP_SWAGGER,
303+
SystemStatusKeys.HUB_API_STARTUP_APIDOCS,
304304
title: "API Docs UI enabled",
305305
description: $"Hub API Swagger docs available at /docs"
306306
);

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
var cwd = Path.GetDirectoryName(assembly.Location);
3131
if (cwd != null)
3232
{
33+
3334
System.Diagnostics.Debug.WriteLine($"Using working directory {cwd}");
3435
Directory.SetCurrentDirectory(cwd);
3536
}
@@ -57,7 +58,27 @@
5758
}
5859
#endif
5960

60-
builder.Configuration.AddJsonFile(hubSettings, optional: true, reloadOnChange: true);
61+
// load optional config but ignore errors if it doesn't exist or is invalid, otherwise service will fail to start
62+
var hubConfigFailed = false;
63+
builder.Configuration.AddJsonFile(p =>
64+
{
65+
p.Path = hubSettings;
66+
p.Optional = true;
67+
p.ReloadOnChange = true;
68+
p.OnLoadException = e =>
69+
{
70+
e.Ignore = true;
71+
hubConfigFailed = true;
72+
73+
AddSystemStatusItem(
74+
SystemStatusCategories.HUB_API,
75+
SystemStatusKeys.HUB_API_STARTUP_CUSTOMCONFIG,
76+
title: "Hub API Service Custom Config",
77+
description: $"Error loading config file {hubSettings} - {e}",
78+
hasError: true
79+
);
80+
};
81+
});
6182

6283
// if windows, run as service, otherwise run as console app
6384
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -289,7 +310,7 @@
289310

290311
AddSystemStatusItem(
291312
SystemStatusCategories.HUB_API,
292-
SystemStatusKeys.HUB_API_STARTUP_SWAGGER,
313+
SystemStatusKeys.HUB_API_STARTUP_APIDOCS,
293314
title: "API Docs UI enabled",
294315
description: $"Hub API docs available at /api/docs"
295316
);
@@ -358,6 +379,20 @@
358379
foreach (var statusItem in _systemStatusItems)
359380
{
360381
hubStateProvider.AddOrUpdateSystemStatusItem(statusItem);
382+
383+
if (statusItem.HasError)
384+
{
385+
app.Logger.LogError($"{statusItem.Key} - {statusItem.Title} - {statusItem.Description}");
386+
}
387+
else if (statusItem.HasWarning)
388+
{
389+
app.Logger.LogWarning($"{statusItem.Key} - {statusItem.Title} - {statusItem.Description}");
390+
}
391+
else
392+
{
393+
394+
app.Logger.LogInformation($"{statusItem.Key} - {statusItem.Title} - {statusItem.Description}");
395+
}
361396
}
362397

363398
app.WaitForShutdown();
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft": "Warning",
6-
"Microsoft.Hosting.Lifetime": "Information"
7-
}
8-
},
9-
"Kestrel": {
10-
"Endpoints": {
11-
"SvcHttpEndpoint": {
12-
"Url": "http://0.0.0.0:8080"
13-
}
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"Kestrel": {
10+
"Endpoints": {
11+
"SvcHttpEndpoint": {
12+
"Url": "http://0.0.0.0:8080"
13+
}
1414

15-
// example HTTP config. Before using, acquire a certificate for desired host e.g. certifyhub.intranet.yourdomain.com
16-
// Then add a Deployment Task (Export Certificate) to export pfx to where it's required e.g. C:\ProgramData\certify\internal-certs\hub.pfx , then run the task to export the initial certificate.
17-
// the choice of port and filename etc is arbitrary, as long as the process can access the file.
18-
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-9.0
19-
/*
20-
"HttpsInlineCertFile": {
15+
// Example HTTPS config. Remove /* and */ to use, keep the comma before the entry so that the json remains valid. Before using, acquire a certificate for desired host e.g. certifyhub.intranet.yourdomain.com
16+
// Then add a Deployment Task (Export Certificate) to export pfx to where it's required e.g. C:\ProgramData\certify\internal-certs\hub.pfx, then run the task to export the initial certificate.
17+
// the choice of port and filename etc is arbitrary, as long as the process can access the file.
18+
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-9.0
19+
/*
20+
,
21+
"HttpsInlineCertFile": {
2122
"Url": "https://0.0.0.0:9697",
2223
"Certificate": {
2324
"Path": "C:\\ProgramData\\certify\\internal-certs\\hub.pfx",
2425
"Password": ""
2526
}
2627
}
27-
*/
28-
}
28+
*/
2929
}
30+
}
3031
}

0 commit comments

Comments
 (0)