From ab7a34ceb241b51fd23c4685a11c67eec5abe56d Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 6 Jun 2024 18:22:13 +0200 Subject: [PATCH] Create a gRPC service for publishing rankings --- Tasks/Lab11/RankingService/.dockerignore | 30 ++++++++++ Tasks/Lab11/RankingService/RankingService.sln | 25 ++++++++ .../RankingService/RankingService/Program.cs | 12 ++++ .../Properties/launchSettings.json | 21 +++++++ .../RankingService/Protos/ranking.proto | 20 +++++++ .../RankingService/RankingService.csproj | 19 ++++++ .../RankingService/Services/RankingService.cs | 58 +++++++++++++++++++ .../appsettings.Development.json | 8 +++ .../RankingService/appsettings.json | 14 +++++ 9 files changed, 207 insertions(+) create mode 100644 Tasks/Lab11/RankingService/.dockerignore create mode 100644 Tasks/Lab11/RankingService/RankingService.sln create mode 100644 Tasks/Lab11/RankingService/RankingService/Program.cs create mode 100644 Tasks/Lab11/RankingService/RankingService/Properties/launchSettings.json create mode 100644 Tasks/Lab11/RankingService/RankingService/Protos/ranking.proto create mode 100644 Tasks/Lab11/RankingService/RankingService/RankingService.csproj create mode 100644 Tasks/Lab11/RankingService/RankingService/Services/RankingService.cs create mode 100644 Tasks/Lab11/RankingService/RankingService/appsettings.Development.json create mode 100644 Tasks/Lab11/RankingService/RankingService/appsettings.json diff --git a/Tasks/Lab11/RankingService/.dockerignore b/Tasks/Lab11/RankingService/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/Tasks/Lab11/RankingService/.dockerignore @@ -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/** \ No newline at end of file diff --git a/Tasks/Lab11/RankingService/RankingService.sln b/Tasks/Lab11/RankingService/RankingService.sln new file mode 100644 index 0000000..1b84cf0 --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService.sln @@ -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 diff --git a/Tasks/Lab11/RankingService/RankingService/Program.cs b/Tasks/Lab11/RankingService/RankingService/Program.cs new file mode 100644 index 0000000..6b42b5e --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService/Program.cs @@ -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(); +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(); diff --git a/Tasks/Lab11/RankingService/RankingService/Properties/launchSettings.json b/Tasks/Lab11/RankingService/RankingService/Properties/launchSettings.json new file mode 100644 index 0000000..ac2de2a --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService/Properties/launchSettings.json @@ -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" +} \ No newline at end of file diff --git a/Tasks/Lab11/RankingService/RankingService/Protos/ranking.proto b/Tasks/Lab11/RankingService/RankingService/Protos/ranking.proto new file mode 100644 index 0000000..a1b1068 --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService/Protos/ranking.proto @@ -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; +} diff --git a/Tasks/Lab11/RankingService/RankingService/RankingService.csproj b/Tasks/Lab11/RankingService/RankingService/RankingService.csproj new file mode 100644 index 0000000..e749e3d --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService/RankingService.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + 5327ada4-dcdc-48d6-b679-b7202b6adc09 + + + + + + + + + + + + diff --git a/Tasks/Lab11/RankingService/RankingService/Services/RankingService.cs b/Tasks/Lab11/RankingService/RankingService/Services/RankingService.cs new file mode 100644 index 0000000..e4865cb --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService/Services/RankingService.cs @@ -0,0 +1,58 @@ +using Grpc.Core; +using RankingService; + +namespace RankingService.Services +{ + public class RankingService : Ranking.RankingBase + { + private readonly ILogger _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> competitors; + + public RankingService(ILogger logger) + { + _logger = logger; + + competitors = new Lazy>( + () => + { + List result = []; + + foreach (string rank in ranks) + { + string[] items = rank.Split(','); + + result.Add( + new() + { + Name = items[0], + Time = items[1] + }); + } + + return result; + }); + } + + public override Task GetRanking(RankingRequest request, ServerCallContext context) + { + RankingReply result = new RankingReply(); + result.Competitors.AddRange(competitors.Value); + return Task.FromResult(result); + } + } +} diff --git a/Tasks/Lab11/RankingService/RankingService/appsettings.Development.json b/Tasks/Lab11/RankingService/RankingService/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Tasks/Lab11/RankingService/RankingService/appsettings.json b/Tasks/Lab11/RankingService/RankingService/appsettings.json new file mode 100644 index 0000000..1aef507 --- /dev/null +++ b/Tasks/Lab11/RankingService/RankingService/appsettings.json @@ -0,0 +1,14 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Kestrel": { + "EndpointDefaults": { + "Protocols": "Http2" + } + } +}