Skip to content

Commit

Permalink
T #98 initial prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Sep 20, 2022
1 parent a746df5 commit 8ac432b
Show file tree
Hide file tree
Showing 188 changed files with 960 additions and 30 deletions.
25 changes: 25 additions & 0 deletions MicroServicePrototype/Gateway/Gateway.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32901.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gateway", "Gateway\Gateway.csproj", "{5C607BBB-667B-46E7-B562-BC62E2A9A003}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5C607BBB-667B-46E7-B562-BC62E2A9A003}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C607BBB-667B-46E7-B562-BC62E2A9A003}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C607BBB-667B-46E7-B562-BC62E2A9A003}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C607BBB-667B-46E7-B562-BC62E2A9A003}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C4709162-8F14-4413-AE8D-EA1F999B59A4}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions MicroServicePrototype/Gateway/Gateway/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"microsoft.dotnet-msidentity": {
"version": "1.0.6",
"commands": [
"dotnet-msidentity"
]
}
}
}
23 changes: 23 additions & 0 deletions MicroServicePrototype/Gateway/Gateway/Controllers/Home.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HuppyCore.Protos;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.Design;

namespace Gateway.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class Home : ControllerBase
{
private readonly Resource.ResourceClient _resourceService;
public Home(Resource.ResourceClient resourceService) { _resourceService = resourceService; }

[HttpGet("test")]
public async Task<IActionResult> GetCpuUsage()
{
var test = await _resourceService.GetCpuUsageAsyncAsync(new Google.Protobuf.WellKnownTypes.Empty());

return Ok(test);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Web.Resource;

namespace Gateway.Controllers
{
[Authorize]
[ApiController]
[Route("[controller]")]
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
25 changes: 25 additions & 0 deletions MicroServicePrototype/Gateway/Gateway/Gateway.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>aspnet-Gateway-53F2AED7-9112-4F6A-8364-CCF6E96C42A3</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.40.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.9" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.16.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Grpc.Net.Client" Version="2.48.0" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="..\..\HuppyCore\HuppyCore\Protos\resource.proto" GrpcServices="Client">
<Link>Protos\resource.proto</Link>
</Protobuf>
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions MicroServicePrototype/Gateway/Gateway/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Grpc.Net.Client;
using HuppyCore.Protos;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var channel = GrpcChannel.ForAddress("https://localhost:9001");
var client = new Resource.ResourceClient(channel);

builder.Services.AddSingleton(client);
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

//var client = new Greet.GreeterClient(channel);

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:46080",
"sslPort": 44369
}
},
"profiles": {
"Gateway": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7209;http://localhost:5209",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
13 changes: 13 additions & 0 deletions MicroServicePrototype/Gateway/Gateway/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Gateway
{
public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
25 changes: 25 additions & 0 deletions MicroServicePrototype/HuppyCore/HuppyCore.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32901.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HuppyCore", "HuppyCore\HuppyCore.csproj", "{350436B3-57C0-42ED-91DF-9AD15F7CE3FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{350436B3-57C0-42ED-91DF-9AD15F7CE3FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{350436B3-57C0-42ED-91DF-9AD15F7CE3FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{350436B3-57C0-42ED-91DF-9AD15F7CE3FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{350436B3-57C0-42ED-91DF-9AD15F7CE3FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6E591690-59D2-40B0-ADF4-8BA397063451}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions MicroServicePrototype/HuppyCore/HuppyCore/HuppyCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<None Remove="Protos\resource.proto" />
</ItemGroup>

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

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.40.0" />
<PackageReference Include="Grpc.Tools" Version="2.48.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions MicroServicePrototype/HuppyCore/HuppyCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using HuppyCore.Services;

var builder = WebApplication.CreateBuilder(args);

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682

// Add services to the container.
builder.Services.AddGrpc();

var app = builder.Build();

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

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();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"HuppyCore": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5091;https://localhost:7091",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
21 changes: 21 additions & 0 deletions MicroServicePrototype/HuppyCore/HuppyCore/Protos/greet.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

option csharp_namespace = "HuppyCore";

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;
}
15 changes: 15 additions & 0 deletions MicroServicePrototype/HuppyCore/HuppyCore/Protos/resource.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

option csharp_namespace = "HuppyCore.Protos";

package resource;

import "google/protobuf/empty.proto";

service Resource {
rpc GetCpuUsageAsync(google.protobuf.Empty) returns (CPUUsage);
}

message CPUUsage {
optional string usage = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Grpc.Core;
using HuppyCore;

namespace HuppyCore.Services
{
public class GreeterService : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
public GreeterService(ILogger<GreeterService> logger)
{
_logger = logger;
}

public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = "Hello " + request.Name
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using HuppyCore.Protos;
using System.Diagnostics;

namespace HuppyCore.Services
{
public class ResourceService : Resource.ResourceBase
{
public ResourceService() { }

public async override Task<CPUUsage> GetCpuUsageAsync(Empty request, ServerCallContext context)
{
var startTime = DateTime.UtcNow;
var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;

await Task.Delay(1000); // gets CPU time between 1000ms

var endTime = DateTime.UtcNow;
var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;

var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds;
var totalMsPassed = (endTime - startTime).TotalMilliseconds;
var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed);

return new CPUUsage()
{
Usage = string.Concat(Math.Round(cpuUsageTotal * 100, 2), '%')
};
}
}
}
Loading

0 comments on commit 8ac432b

Please sign in to comment.