Skip to content

Commit f2042bc

Browse files
Add "Include Error Detail=true" to API postgres connection strings (#44)
Adds an extension method .WithDevSettings that can be called on an IResourceBuilder<PostgresDatabaseResource> resource that appends common development settings to the connection string. Currently, it only adds "Include Error Detail=true" to show detailed error messages, but additional settings could be added as needed in the future.
1 parent 31e0adf commit f2042bc

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

Crucible.AppHost/AppHost.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public static void AddPlayer(this IDistributedApplicationBuilder builder, IResou
111111
if (!options.AddAllApplications && !options.Player)
112112
return;
113113

114-
var playerDb = postgres.AddDatabase("playerDb", "player");
114+
var playerDb = postgres.AddDatabase("playerDb", "player")
115+
.WithDevSettings();
115116

116117
builder.ConfigureApiSecrets(
117118
new Projects.Player_Api().ProjectPath,
@@ -149,8 +150,10 @@ public static void AddPlayer(this IDistributedApplicationBuilder builder, IResou
149150

150151
private static void AddPlayerVm(this IDistributedApplicationBuilder builder, IResourceBuilder<PostgresServerResource> postgres, IResourceBuilder<KeycloakResource> keycloak, bool startByDefault)
151152
{
152-
var vmDb = postgres.AddDatabase("vmDb", "player_vm");
153-
var vmLoggingDb = postgres.AddDatabase("vmLoggingDb", "player_vm_logging");
153+
var vmDb = postgres.AddDatabase("vmDb", "player_vm")
154+
.WithDevSettings();
155+
var vmLoggingDb = postgres.AddDatabase("vmLoggingDb", "player_vm_logging")
156+
.WithDevSettings();
154157

155158
builder.ConfigureApiSecrets(
156159
new Projects.Player_Vm_Api().ProjectPath,
@@ -205,7 +208,8 @@ public static void AddCaster(this IDistributedApplicationBuilder builder, IResou
205208
if (!options.AddAllApplications && !options.Caster)
206209
return;
207210

208-
var casterDb = postgres.AddDatabase("casterDb", "caster");
211+
var casterDb = postgres.AddDatabase("casterDb", "caster")
212+
.WithDevSettings();
209213

210214
builder.ConfigureApiSecrets(
211215
new Projects.Caster_Api().ProjectPath,
@@ -268,7 +272,8 @@ public static void AddAlloy(this IDistributedApplicationBuilder builder, IResour
268272
if (!options.AddAllApplications && !options.Alloy)
269273
return;
270274

271-
var alloyDb = postgres.AddDatabase("alloyDb", "alloy");
275+
var alloyDb = postgres.AddDatabase("alloyDb", "alloy")
276+
.WithDevSettings();
272277

273278
builder.ConfigureApiSecrets(
274279
new Projects.Alloy_Api().ProjectPath,
@@ -316,7 +321,8 @@ public static void AddTopoMojo(this IDistributedApplicationBuilder builder, IRes
316321
if (!options.AddAllApplications && !options.TopoMojo)
317322
return;
318323

319-
var topoDb = postgres.AddDatabase("topoDb", "topomojo");
324+
var topoDb = postgres.AddDatabase("topoDb", "topomojo")
325+
.WithDevSettings();
320326

321327
builder.ConfigureApiSecrets(
322328
new Projects.TopoMojo_Api().ProjectPath,
@@ -385,7 +391,8 @@ public static void AddSteamfitter(this IDistributedApplicationBuilder builder, I
385391
if (!options.AddAllApplications && !options.Steamfitter)
386392
return;
387393

388-
var steamfitterDb = postgres.AddDatabase("steamfitterDb", "steamfitter");
394+
var steamfitterDb = postgres.AddDatabase("steamfitterDb", "steamfitter")
395+
.WithDevSettings();
389396

390397
builder.ConfigureApiSecrets(
391398
new Projects.Steamfitter_Api().ProjectPath,
@@ -431,7 +438,8 @@ public static void AddCite(this IDistributedApplicationBuilder builder, IResourc
431438
if (!options.AddAllApplications && !options.Cite)
432439
return;
433440

434-
var citeDb = postgres.AddDatabase("citeDb", "cite");
441+
var citeDb = postgres.AddDatabase("citeDb", "cite")
442+
.WithDevSettings();
435443

436444
builder.ConfigureApiSecrets(
437445
new Projects.Cite_Api().ProjectPath,
@@ -477,7 +485,8 @@ public static void AddGallery(this IDistributedApplicationBuilder builder, IReso
477485
if (!options.AddAllApplications && !options.Gallery)
478486
return;
479487

480-
var galleryDb = postgres.AddDatabase("galleryDb", "gallery");
488+
var galleryDb = postgres.AddDatabase("galleryDb", "gallery")
489+
.WithDevSettings();
481490

482491
builder.ConfigureApiSecrets(
483492
new Projects.Gallery_Api().ProjectPath,
@@ -523,7 +532,8 @@ public static void AddBlueprint(this IDistributedApplicationBuilder builder, IRe
523532
if (!options.AddAllApplications && !options.Blueprint)
524533
return;
525534

526-
var blueprintDb = postgres.AddDatabase("blueprintDb", "blueprint");
535+
var blueprintDb = postgres.AddDatabase("blueprintDb", "blueprint")
536+
.WithDevSettings();
527537

528538
builder.ConfigureApiSecrets(
529539
new Projects.Blueprint_Api().ProjectPath,
@@ -568,7 +578,8 @@ public static void AddGameboard(this IDistributedApplicationBuilder builder, IRe
568578
if (!options.AddAllApplications && !options.Gameboard)
569579
return;
570580

571-
var gameboardDb = postgres.AddDatabase("gameboardDb", "gameboard");
581+
var gameboardDb = postgres.AddDatabase("gameboardDb", "gameboard")
582+
.WithDevSettings();
572583

573584
builder.ConfigureApiSecrets(
574585
new Projects.Gameboard_Api().ProjectPath,
@@ -781,4 +792,5 @@ private static void SetUserSecret(string projectFilePath, string key, string val
781792
return -1;
782793
}
783794
}
795+
784796
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 Carnegie Mellon University. All Rights Reserved.
2+
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.
3+
4+
public static class ConnectionStringExtensions
5+
{
6+
/// <summary>
7+
/// Append common dev settings to the connection string of a postgres database. e.g. "Include Error Detail=true".
8+
/// </summary>
9+
/// <param name="database">The postgres database whose connection string will be appended to</param>
10+
/// <returns>An IResourceWithConnectionString, rather than the original IResourceBuilder PostgresDatabaseResource.
11+
/// If the caller needs any specific properties of the original resource beyond those available in the IResourceWithConnectionString interface,
12+
/// they may need to be added to the AppendConnectionStringResource.</returns>
13+
public static IResourceBuilder<IResourceWithConnectionString> WithDevSettings(this IResourceBuilder<PostgresDatabaseResource> database)
14+
{
15+
return database.AddConnectionStringData($"Include Error Detail=True");
16+
}
17+
18+
// Adapted from https://github.com/dotnet/aspire/discussions/3605#discussioncomment-9090161
19+
// Adds a custom resource that appends to another resource's connection string
20+
public static IResourceBuilder<IResourceWithConnectionString> AddConnectionStringData(this IResourceBuilder<IResourceWithConnectionString> builder, ReferenceExpression.ExpressionInterpolatedStringHandler connectionStringData)
21+
{
22+
return builder.ApplicationBuilder.AddResource(new AppendConnectionStringResource(builder.Resource, ReferenceExpression.Create(connectionStringData)))
23+
.WithInitialState(new CustomResourceSnapshot
24+
{
25+
Properties = [],
26+
ResourceType = "ConnectionStringData",
27+
State = "Hidden" // Hide from the dashboard
28+
});
29+
}
30+
31+
class AppendConnectionStringResource(IResourceWithConnectionString previous, ReferenceExpression referenceExpression) :
32+
Resource($"{previous.Name}-ConnectionString"), IResourceWithConnectionString
33+
{
34+
public string? ConnectionStringEnvironmentVariable => previous.ConnectionStringEnvironmentVariable;
35+
36+
public ReferenceExpression ConnectionStringExpression => ReferenceExpression.Create($"{previous};{referenceExpression}");
37+
}
38+
}

Crucible.AppHost/Crucible.AppHost.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
89
<NoWarn>$(NoWarn);ASPIRECERTIFICATES001</NoWarn>
10+
<NoWarn>$(NoWarn);1591</NoWarn>
911
<UserSecretsId>98efcc77-ed92-4f61-8c56-50f0fbbb7eb2</UserSecretsId>
1012
</PropertyGroup>
1113

0 commit comments

Comments
 (0)