Create a gRPC service for publishing rankings

This commit is contained in:
Manuel Thalmann 2024-06-06 18:22:13 +02:00
parent 429205e853
commit ab7a34ceb2
9 changed files with 207 additions and 0 deletions

View file

@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**

View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RankingService", "RankingService\RankingService.csproj", "{DE7F9795-772A-4134-895F-3F65193E86BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DE7F9795-772A-4134-895F-3F65193E86BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE7F9795-772A-4134-895F-3F65193E86BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE7F9795-772A-4134-895F-3F65193E86BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE7F9795-772A-4134-895F-3F65193E86BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6790DB4E-93FA-425B-94D4-36A687BC734A}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,12 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddGrpc();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapGrpcService<RankingService.Services.RankingService>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
app.Run();

View file

@ -0,0 +1,21 @@
{
"profiles": {
"http": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:8000"
},
"https": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:8001;http://localhost:8000"
}
},
"$schema": "http://json.schemastore.org/launchsettings.json"
}

View file

@ -0,0 +1,20 @@
syntax = "proto3";
option csharp_namespace = "RankingService";
package ranking;
service Ranking {
rpc GetRanking (RankingRequest) returns (RankingReply);
}
message RankingRequest {}
message RankingReply {
repeated Competitor competitors = 1;
}
message Competitor {
string name = 1;
string time = 2;
}

View file

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>5327ada4-dcdc-48d6-b679-b7202b6adc09</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Protobuf Include="Protos\ranking.proto" GrpcServices="Server" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.57.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,58 @@
using Grpc.Core;
using RankingService;
namespace RankingService.Services
{
public class RankingService : Ranking.RankingBase
{
private readonly ILogger<RankingService> _logger;
private readonly string[] ranks =
{
"Mueller Stefan,02:31:14",
"Marti Adrian,2:30:09",
"Kiptum Daniel,2:11:31",
"Ancay Tarcis,2:20:02",
"Kreibuhl Christian,2:21:47",
"Ott Michael,2:33:48",
"Menzi Christoph,2:27:26",
"Oliver Ruben,2:32:12",
"Elmer Beat,2:33:53",
"Kuehni Martin,2:33:36"
};
private Lazy<List<Competitor>> competitors;
public RankingService(ILogger<RankingService> logger)
{
_logger = logger;
competitors = new Lazy<List<Competitor>>(
() =>
{
List<Competitor> result = [];
foreach (string rank in ranks)
{
string[] items = rank.Split(',');
result.Add(
new()
{
Name = items[0],
Time = items[1]
});
}
return result;
});
}
public override Task<RankingReply> GetRanking(RankingRequest request, ServerCallContext context)
{
RankingReply result = new RankingReply();
result.Competitors.AddRange(competitors.Value);
return Task.FromResult(result);
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2"
}
}
}