From 445e1e39bc7cfb52c9154a78eb35d9be6d2612e1 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Fri, 23 Aug 2024 18:03:06 +0200 Subject: [PATCH] Add example endpoint to connect with the database --- src/Directory.Packages.props | 1 + .../Api.Grpc.csproj | 3 +- src/Server.Controllers.Api.Grpc/Program.cs | 13 +++++-- .../Protos/greet.proto | 21 ------------ .../Protos/messagebus.proto | 16 +++++++++ .../Services/GreeterService.cs | 22 ------------ .../Services/MessageBusServiceProxy.cs | 34 +++++++++++++++++++ .../appsettings.json | 2 +- .../MessageBuses/MessageBusRepository.cs | 1 + 9 files changed, 65 insertions(+), 48 deletions(-) delete mode 100644 src/Server.Controllers.Api.Grpc/Protos/greet.proto create mode 100644 src/Server.Controllers.Api.Grpc/Protos/messagebus.proto delete mode 100644 src/Server.Controllers.Api.Grpc/Services/GreeterService.cs create mode 100644 src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index d729b0b..2c6f02e 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -1,5 +1,6 @@ + diff --git a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj index 3bce6f5..046bf93 100644 --- a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj +++ b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj @@ -7,11 +7,12 @@ - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Server.Controllers.Api.Grpc/Program.cs b/src/Server.Controllers.Api.Grpc/Program.cs index fea3082..5ba4c3e 100644 --- a/src/Server.Controllers.Api.Grpc/Program.cs +++ b/src/Server.Controllers.Api.Grpc/Program.cs @@ -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); @@ -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(); +app.MapGrpcService(); + +if (app.Environment.IsDevelopment()) +{ + app.MapGrpcReflectionService(); +} + app.MapHealthChecks("/healthz"); app.MapGet("/", diff --git a/src/Server.Controllers.Api.Grpc/Protos/greet.proto b/src/Server.Controllers.Api.Grpc/Protos/greet.proto deleted file mode 100644 index 6ced199..0000000 --- a/src/Server.Controllers.Api.Grpc/Protos/greet.proto +++ /dev/null @@ -1,21 +0,0 @@ -syntax = "proto3"; - -option csharp_namespace = "Server.Controllers.Api.Grpc"; - -package greet; - -// The greeting service definition. -service Greeter { - // Sends a greeting - rpc SayHello (HelloRequest) returns (HelloReply); -} - -// The request message containing the user's name. -message HelloRequest { - string name = 1; -} - -// The response message containing the greetings. -message HelloReply { - string message = 1; -} diff --git a/src/Server.Controllers.Api.Grpc/Protos/messagebus.proto b/src/Server.Controllers.Api.Grpc/Protos/messagebus.proto new file mode 100644 index 0000000..4b72ca3 --- /dev/null +++ b/src/Server.Controllers.Api.Grpc/Protos/messagebus.proto @@ -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; +} \ No newline at end of file diff --git a/src/Server.Controllers.Api.Grpc/Services/GreeterService.cs b/src/Server.Controllers.Api.Grpc/Services/GreeterService.cs deleted file mode 100644 index 28fa151..0000000 --- a/src/Server.Controllers.Api.Grpc/Services/GreeterService.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Grpc.Core; -using Server.Controllers.Api.Grpc; - -namespace Server.Controllers.Api.Grpc.Services; - -public class GreeterService : Greeter.GreeterBase -{ - private readonly ILogger _logger; - - public GreeterService(ILogger logger) - { - _logger = logger; - } - - public override Task SayHello(HelloRequest request, ServerCallContext context) - { - return Task.FromResult(new HelloReply - { - Message = "Hello " + request.Name - }); - } -} \ No newline at end of file diff --git a/src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs b/src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs new file mode 100644 index 0000000..61f1d90 --- /dev/null +++ b/src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs @@ -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 logger) + : MessageBusService.MessageBusServiceBase +{ + public override async Task 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 + }; + } + } +} \ No newline at end of file diff --git a/src/Server.Controllers.Api.Grpc/appsettings.json b/src/Server.Controllers.Api.Grpc/appsettings.json index 0be6fb0..f8f53df 100644 --- a/src/Server.Controllers.Api.Grpc/appsettings.json +++ b/src/Server.Controllers.Api.Grpc/appsettings.json @@ -20,7 +20,7 @@ }, "Kestrel": { "EndpointDefaults": { - "Protocols": "Http1AndHttp2" + "Protocols": "Http2" } } } diff --git a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs index 1aeed89..69d1a04 100644 --- a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs +++ b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs @@ -22,6 +22,7 @@ await _dbContext.MessageBusStatus public async Task FindStatusAsync() { return await _dbContext.MessageBusStatus + .OrderBy(s => s.Id) .LastOrDefaultAsync(); }