Skip to content

Commit 5b3f983

Browse files
Refactor Cosmos and Event Hub configurations
Updated Cosmos DB connection strings to use "codebreaker" key. Removed inline database creation logic, delegating it to AppHost. Refactored Event Hub setup for clarity and added a new Cosmos container "GamesV3". Introduced new projects `CodeBreaker_Bot` and `Codebreaker_BotQ` with appropriate references and settings. Removed `CodeBreaker_Blazor` project configuration. Commented out RankingsContext database creation logic. Added TODOs for future updates and performed general cleanup.
1 parent 653ac56 commit 5b3f983

3 files changed

Lines changed: 52 additions & 48 deletions

File tree

src/services/gameapis/Codebreaker.GameAPIs/ApplicationServices.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void ConfigureCosmos(IHostApplicationBuilder builder)
4545
{
4646
builder.Services.AddDbContext<IGamesRepository, GamesCosmosContext>(options =>
4747
{
48-
string connectionString = builder.Configuration.GetConnectionString("codebreakercosmos") ?? throw new InvalidOperationException("Could not read the Cosmos connection-string");
48+
string connectionString = builder.Configuration.GetConnectionString("codebreaker") ?? throw new InvalidOperationException("Could not read the Cosmos connection-string");
4949
options.UseCosmos(connectionString, "codebreaker");
5050

5151
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
@@ -160,27 +160,27 @@ public static async Task CreateOrUpdateDatabaseAsync(this WebApplication app)
160160
}
161161
}
162162

163-
// The database is created from the AppHost AddDatabase method. The Cosmos container is created here - if it doesn't exist yet.
164-
if (app.Configuration["DataStore"] == "Cosmos")
165-
{
166-
try
167-
{
168-
using var scope = app.Services.CreateScope();
169-
// TODO: update with .NET Aspire Preview 4
170-
var repo = scope.ServiceProvider.GetRequiredService<GamesCosmosContext>();
171-
// var repo = scope.ServiceProvider.GetRequiredService<IGamesRepository>();
172-
if (repo is GamesCosmosContext context)
173-
{
174-
bool created = await context.Database.EnsureCreatedAsync();
175-
app.Logger.LogInformation("Database created: {created}", created);
176-
}
177-
}
178-
catch (Exception ex)
179-
{
180-
app.Logger.LogError(ex, "Error updating database");
181-
throw;
182-
}
183-
}
163+
// The database is created from the AppHost AddDatabase method. The Cosmos container is now created from the AppHost as well!
164+
//if (app.Configuration["DataStore"] == "Cosmos")
165+
//{
166+
// try
167+
// {
168+
// using var scope = app.Services.CreateScope();
169+
// // TODO: update with .NET Aspire Preview 4
170+
// var repo = scope.ServiceProvider.GetRequiredService<GamesCosmosContext>();
171+
// // var repo = scope.ServiceProvider.GetRequiredService<IGamesRepository>();
172+
// if (repo is GamesCosmosContext context)
173+
// {
174+
// bool created = await context.Database.EnsureCreatedAsync();
175+
// app.Logger.LogInformation("Database created: {created}", created);
176+
// }
177+
// }
178+
// catch (Exception ex)
179+
// {
180+
// app.Logger.LogError(ex, "Error updating database");
181+
// throw;
182+
// }
183+
//}
184184

185185
s_IsDatabaseUpdateComplete = true;
186186
}

src/services/host/Codebreaker.AppHost/Program.cs renamed to src/services/host/Codebreaker.AppHost/AppHost.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,34 @@
6161
var botQueue = storage.AddQueues("botqueue");
6262
var blob = storage.AddBlobs("checkpoints");
6363

64-
var eventHub = builder.AddAzureEventHubs("codebreakerevents")
65-
.AddHub("games");
64+
var eventHub = builder.AddAzureEventHubs("codebreakerevents");
65+
66+
eventHub.AddHub("games");
6667

6768
var cosmos = builder.AddAzureCosmosDB("codebreakercosmos")
6869
.AddCosmosDatabase("codebreaker");
6970

71+
cosmos.AddContainer("GamesV3", "/PartitionKey");
72+
7073
var gatewayKeyvault = builder.AddAzureKeyVault("gateway-keyvault");
7174
var userServiceKeyvault = builder.AddAzureKeyVault("users-keyvault");
7275

73-
var cosmoscreate = builder.AddProject<Projects.Codebreaker_CosmosCreate>("cosmoscreate")
74-
.WithReference(cosmos)
75-
.WithReference(insights)
76-
.WaitFor(cosmos)
77-
.WaitFor(insights);
78-
76+
// TODO: fix new eventhub namings
7977
var gameAPIs = builder.AddProject<Projects.Codebreaker_GameAPIs>("gameapis")
8078
.WithReference(cosmos)
8179
.WithReference(redis)
8280
.WithReference(insights)
8381
.WithReference(eventHub)
8482
.WithEnvironment("DataStore", dataStore)
85-
.WithEnvironment("StartupMode", startupMode)
86-
.WaitForCompletion(cosmoscreate); // we use cosmos with the Azure option
83+
.WithEnvironment("StartupMode", startupMode);
84+
85+
var bot = builder.AddProject<Projects.CodeBreaker_Bot>("bot")
86+
.WithReference(insights)
87+
.WithReference(botQueue)
88+
.WithReference(gameAPIs)
89+
.WithEnvironment("Bot__Loop", botLoop)
90+
.WithEnvironment("Bot__Delay", botDelay)
91+
.WaitFor(gameAPIs);
8792

8893
// TODO: change to use BotQ with Container App Jobs
8994
var botq = builder.AddProject<Projects.Codebreaker_BotQ>("botq")
@@ -131,7 +136,6 @@
131136
.WaitFor(gatewayKeyvault)
132137
.WaitFor(insights);
133138

134-
135139
builder.AddProject<Projects.CodeBreaker_Blazor>("blazor")
136140
.WithExternalHttpEndpoints()
137141
.WithReference(gateway)

src/services/ranking/Codebreaker.Ranking/ApplicationServices.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
3535

3636
builder.Services.AddDbContextFactory<RankingsContext>(options =>
3737
{
38-
string connectionString = builder.Configuration.GetConnectionString("codebreakercosmos") ?? throw new InvalidOperationException("Could not read the Cosmos connection-string");
38+
string connectionString = builder.Configuration.GetConnectionString("codebreaker") ?? throw new InvalidOperationException("Could not read the Cosmos connection-string");
3939
options.UseCosmos(connectionString, "codebreaker");
4040
});
4141

@@ -56,22 +56,22 @@ internal static bool IsDatabaseUpdateComplete
5656

5757
public static async Task CreateOrUpdateDatabaseAsync(this WebApplication app)
5858
{
59-
try
60-
{
61-
using var scope = app.Services.CreateScope();
62-
// TODO: update with .NET Aspire Preview 4
63-
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<RankingsContext>>();
64-
using var context = await factory.CreateDbContextAsync();
59+
//try
60+
//{
61+
// using var scope = app.Services.CreateScope();
62+
// // TODO: update with .NET Aspire Preview 4
63+
// var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<RankingsContext>>();
64+
// using var context = await factory.CreateDbContextAsync();
6565

66-
bool created = await context.Database.EnsureCreatedAsync();
67-
app.Logger.LogInformation("Database created: {created}", created);
66+
// bool created = await context.Database.EnsureCreatedAsync();
67+
// app.Logger.LogInformation("Database created: {created}", created);
6868

69-
}
70-
catch (Exception ex)
71-
{
72-
app.Logger.LogError(ex, "Error updating database");
73-
throw;
74-
}
69+
//}
70+
//catch (Exception ex)
71+
//{
72+
// app.Logger.LogError(ex, "Error updating database");
73+
// throw;
74+
//}
7575

7676
s_IsDatabaseUpdateComplete = true;
7777
}

0 commit comments

Comments
 (0)