Skip to content

Commit

Permalink
ch15 cleanup #177
Browse files Browse the repository at this point in the history
  • Loading branch information
christiannagel committed May 21, 2024
1 parent 5637cfb commit 8232f20
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 107 deletions.
16 changes: 2 additions & 14 deletions ch15/CodeBreaker.Bot/ApplicationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@ internal static class ApplicationServices
{
public static void AddApplicationServices(this IHostApplicationBuilder builder)
{
// when using gRPC to communicate with the game API, uncomment this section, and comment the AddHttpClient section
//builder.Services.AddSingleton<IGamesClient, GrpcGamesClient>()
// .AddGrpcClient<GrpcGame.GrpcGameClient>(
// client =>
// {
// //var resolver = sp.GetRequiredService<ServiceEndpointResolver>();

// //var endpointSource = await resolver.GetEndpointsAsync("https+http://gameapis", CancellationToken.None);

// //var endpoint = endpointSource.Endpoints
// // .Select(e => e.EndPoint).First();

// //client.Address = new Uri(endpoint.ToString() ?? throw new InvalidOperationException());

// var endpoint = builder.Configuration["services:gameapis:https:0"] ?? throw new InvalidOperationException();
// client.Address = new Uri(endpoint);

// // TODO: change to with a later version:
// // client.Address = new Uri("https://gameapis");
// client.Address = new Uri("https://gameapis");
// });

builder.Services.AddHttpClient<IGamesClient, GamesClient>(client =>
Expand Down
2 changes: 0 additions & 2 deletions ch15/Codebreaker.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.Extensions.Azure;

var builder = DistributedApplication.CreateBuilder(args);

string dataStore = builder.Configuration["DataStore"] ?? "InMemory";
Expand Down
21 changes: 7 additions & 14 deletions ch15/Codebreaker.BotQ/ApplicationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,21 @@ internal static class ApplicationServices
{
public static void AddApplicationServices(this IHostApplicationBuilder builder)
{

builder.AddAzureQueueClient("botqueue");
builder.Services.AddScoped<BotQueueClient>();



var botConfig = builder.Configuration.GetSection("Bot");
builder.Services.Configure<BotQueueClientOptions>(botConfig);
builder.Services.AddScoped<CodebreakerTimer>();
builder.Services.AddScoped<CodebreakerGameRunner>();

// turning off gRPC temporary, using REST instead
//builder.Services.AddSingleton<IGamesClient, GrpcGamesClient>()
// .AddGrpcClient<GrpcGame.GrpcGameClient>(
//client =>
//{
// var endpoint = builder.Configuration["services:gameapis:https:0"] ?? throw new InvalidOperationException();
// client.Address = new Uri(endpoint);

// // TODO: change to with a later version:
// // client.Address = new Uri("https://gameapis");
//});
// when using gRPC to communicate with the game API, uncomment this section, and comment the AddHttpClient section
builder.Services.AddSingleton<IGamesClient, GrpcGamesClient>()
.AddGrpcClient<GrpcGame.GrpcGameClient>(
client =>
{
client.Address = new Uri("https://gameapis");
});

builder.Services.AddHttpClient<IGamesClient, GamesClient>(client =>
{
Expand Down
10 changes: 3 additions & 7 deletions ch15/Codebreaker.GameAPIs/ApplicationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ public static async Task CreateOrUpdateDatabaseAsync(this WebApplication app)
try
{
using var scope = app.Services.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<GamesSqlServerContext>();

// TODO: update with .NET Aspire Preview 4
// var repo = scope.ServiceProvider.GetRequiredService<IGamesRepository>();
var repo = scope.ServiceProvider.GetRequiredService<IGamesRepository>();
if (repo is GamesSqlServerContext context)
{
await context.Database.MigrateAsync();
Expand All @@ -155,9 +152,8 @@ public static async Task CreateOrUpdateDatabaseAsync(this WebApplication app)
try
{
using var scope = app.Services.CreateScope();
// TODO: update with .NET Aspire Preview 4
var repo = scope.ServiceProvider.GetRequiredService<GamesCosmosContext>();
// var repo = scope.ServiceProvider.GetRequiredService<IGamesRepository>();

var repo = scope.ServiceProvider.GetRequiredService<IGamesRepository>();
if (repo is GamesCosmosContext context)
{
bool created = await context.Database.EnsureCreatedAsync();
Expand Down
13 changes: 1 addition & 12 deletions ch15/Codebreaker.GameAPIs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Confluent.Kafka;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
Expand Down Expand Up @@ -48,15 +46,6 @@

app.MapGameEndpoints();

// temporary turn off grpc services
// app.MapGrpcService<GrpcGameEndpoints>();

//string? mode = builder.Configuration["StartupMode"];
//if (mode == "OnPremises")
//{
// var kafkaproducer = app.Services.GetRequiredService<IProducer<string, string>>();
// await kafkaproducer.ProduceAsync("gamesummary", new Message<string, string> { Key = "init", Value = "init" });
//}

app.MapGrpcService<GrpcGameEndpoints>();

app.Run();
11 changes: 7 additions & 4 deletions ch15/Codebreaker.GameAPIs/Services/KafkaGameReportProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Codebreaker.GameAPIs.Services;

public class KafkaGameReportProducer(IProducer<string, string> producerClient, ILogger<KafkaGameReportProducer> logger)
: IGameReport
public sealed class KafkaGameReportProducer(IProducer<string, string> producerClient, ILogger<KafkaGameReportProducer> logger)
: IGameReport, IDisposable
{
public Task ReportGameEndedAsync(GameSummary game, CancellationToken cancellationToken = default)
{
Expand All @@ -21,9 +21,12 @@ public Task ReportGameEndedAsync(GameSummary game, CancellationToken cancellatio
_ = producerClient.ProduceAsync(topic, message, cancellationToken);
}

producerClient.Flush(TimeSpan.FromSeconds(5));

logger.GameCompletionSent(game.Id, "Kafka");
return Task.CompletedTask;
}

public void Dispose()
{
producerClient.Flush(TimeSpan.FromSeconds(5));
}
}
15 changes: 0 additions & 15 deletions ch15/Codebreaker.Live/Endpoints/GRPCLiveGameService.cs

This file was deleted.

20 changes: 0 additions & 20 deletions ch15/Codebreaker.Live/Endpoints/LiveGamesEndpoints.cs

This file was deleted.

1 change: 0 additions & 1 deletion ch15/Codebreaker.Live/Endpoints/LiveHub.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

namespace Codebreaker.Live.Endpoints;

// public class LiveHub(LiveGamesEventProcessor eventProcessor, LiveHubClientsCount clientsCounter, ILogger<LiveHub> logger) : Hub
public class LiveHub(LiveHubClientsCount clientsCounter, ILogger<LiveHub> logger) : Hub
{
public override Task OnConnectedAsync()
Expand Down
1 change: 0 additions & 1 deletion ch15/Codebreaker.Ranking/ApplicationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public static async Task CreateOrUpdateDatabaseAsync(this WebApplication app)
try
{
using var scope = app.Services.CreateScope();
// TODO: update with .NET Aspire Preview 4
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<RankingsContext>>();
using var context = await factory.CreateDbContextAsync();

Expand Down
6 changes: 0 additions & 6 deletions ch15/Codebreaker.Ranking/Codebreaker.Ranking.http

This file was deleted.

3 changes: 0 additions & 3 deletions ch15/Codebreaker.Ranking/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.AddServiceDefaults();
builder.AddApplicationServices();

Expand All @@ -20,7 +18,6 @@

app.MapApplicationEndpoints();


var eventProcessor = app.Services.GetRequiredService<IGameSummaryProcessor>();
await eventProcessor.StartProcessingAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

namespace Codebreaker.Ranking.Services;

public class GameSummaryEventProcessor(EventProcessorClient client, IDbContextFactory<RankingsContext> factory, ILogger<GameSummaryEventProcessor> logger) : IGameSummaryProcessor
public class GameSummaryEventProcessor(
EventProcessorClient client,
IDbContextFactory<RankingsContext> factory,
ILogger<GameSummaryEventProcessor> logger) : IGameSummaryProcessor
{
public async Task StartProcessingAsync(CancellationToken cancellationToken = default)
{
Expand Down
11 changes: 5 additions & 6 deletions ch15/Codebreaker.Ranking/Services/IGameSummaryProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

namespace Codebreaker.Ranking.Services
namespace Codebreaker.Ranking.Services;

public interface IGameSummaryProcessor
{
public interface IGameSummaryProcessor
{
Task StartProcessingAsync(CancellationToken cancellationToken = default);
Task StopProcessingAsync(CancellationToken cancellationToken = default);
}
Task StartProcessingAsync(CancellationToken cancellationToken = default);
Task StopProcessingAsync(CancellationToken cancellationToken = default);
}
3 changes: 2 additions & 1 deletion ch15/LiveTestClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddSingleton<StreamingLiveClient>();
builder.Services.Configure<LiveClientOptions>(builder.Configuration.GetSection("Codebreaker.Live"));
builder.Services.Configure<LiveClientOptions>(
builder.Configuration.GetSection("Codebreaker.Live"));
using var host = builder.Build();

var client = host.Services.GetRequiredService<StreamingLiveClient>();
Expand Down

0 comments on commit 8232f20

Please sign in to comment.