Skip to content

Commit

Permalink
Add example endpoint to connect with the database
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Aug 23, 2024
1 parent b7e4d7e commit 445e1e3
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project>
<ItemGroup>
<PackageVersion Include="Grpc.AspNetCore.Server.Reflection" Version="2.65.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.8" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8" />
Expand Down
3 changes: 2 additions & 1 deletion src/Server.Controllers.Api.Grpc/Api.Grpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
</PropertyGroup>

<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Server"/>
<Protobuf Include="Protos\messagebus.proto" GrpcServices="Server"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" />
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
13 changes: 10 additions & 3 deletions src/Server.Controllers.Api.Grpc/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using MadWorldNL.MantaRayPlan.Extensions;
using MadWorldNL.MantaRayPlan.OpenTelemetry;
using Server.Controllers.Api.Grpc.Services;
using MadWorldNL.MantaRayPlan.Services;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -10,14 +10,21 @@
builder.AddDefaultOpenTelemetry(openTelemetryConfig);

builder.Services.AddGrpc();
builder.Services.AddGrpcReflection();

builder.AddDatabase();

builder.Services.AddHealthChecks();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.MapGrpcService<MessageBusServiceProxy>();

if (app.Environment.IsDevelopment())
{
app.MapGrpcReflectionService();
}

app.MapHealthChecks("/healthz");

app.MapGet("/",
Expand Down
21 changes: 0 additions & 21 deletions src/Server.Controllers.Api.Grpc/Protos/greet.proto

This file was deleted.

16 changes: 16 additions & 0 deletions src/Server.Controllers.Api.Grpc/Protos/messagebus.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";

option csharp_namespace = "MantaRayPlan";

package messagebus;

import "google/protobuf/empty.proto";

service MessageBusService {
rpc GetStatus (google.protobuf.Empty) returns (MessageBusStatusReply);
}

message MessageBusStatusReply {
string message = 1;
int32 counter = 2;
}
22 changes: 0 additions & 22 deletions src/Server.Controllers.Api.Grpc/Services/GreeterService.cs

This file was deleted.

34 changes: 34 additions & 0 deletions src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using MadWorldNL.MantaRayPlan.MessageBuses;
using MantaRayPlan;

namespace MadWorldNL.MantaRayPlan.Services;

public class MessageBusServiceProxy(IMessageBusRepository messageBusRepository, ILogger<MessageBusServiceProxy> logger)
: MessageBusService.MessageBusServiceBase
{
public override async Task<MessageBusStatusReply> GetStatus(Empty request, ServerCallContext context)
{
try
{
var status = await messageBusRepository.FindStatusAsync() ?? new MessageBusStatus();

return new MessageBusStatusReply()
{
Counter = status.Count
};
}
catch (Exception ex)
{
const string message = "Database error";

logger.LogError(ex, message);

return new MessageBusStatusReply()
{
Message = message
};
}
}
}
2 changes: 1 addition & 1 deletion src/Server.Controllers.Api.Grpc/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http1AndHttp2"
"Protocols": "Http2"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ await _dbContext.MessageBusStatus
public async Task<MessageBusStatus?> FindStatusAsync()
{
return await _dbContext.MessageBusStatus
.OrderBy(s => s.Id)
.LastOrDefaultAsync();
}

Expand Down

0 comments on commit 445e1e3

Please sign in to comment.