Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ApiGateway/ApiGateway.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Ocelot" Version="24.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="ocelot.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions ApiGateway/NoServicesAvailableError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Ocelot.Errors;

namespace ApiGateway;

public class NoServicesAvailableError(string message) : Error(message, OcelotErrorCode.UnknownError, 503)
{
}
42 changes: 42 additions & 0 deletions ApiGateway/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using ServiceDefaults;
using ApiGateway;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);

builder.Services.AddCors(options =>
{
if (builder.Environment.IsDevelopment())
{
options.AddDefaultPolicy(policy =>
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
}
else
{
var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? [];
options.AddDefaultPolicy(policy =>
policy.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod());
}
});

builder.Services
.AddOcelot(builder.Configuration)
.AddCustomLoadBalancer((serviceProvider, route, serviceDiscoveryProvider) =>
{
return new QueryBasedLoadBalancer(serviceDiscoveryProvider.GetAsync);
});

var app = builder.Build();

app.UseCors();
app.MapDefaultEndpoints();

await app.UseOcelot();

app.Run();

38 changes: 38 additions & 0 deletions ApiGateway/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:7138",
"sslPort": 44326
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5067",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7022;http://localhost:5067",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
47 changes: 47 additions & 0 deletions ApiGateway/QueryBasedLoadBalancer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Ocelot.LoadBalancer.Interfaces;
using Ocelot.Responses;
using Ocelot.Values;

namespace ApiGateway;

public class QueryBasedLoadBalancer : ILoadBalancer
{
private readonly Func<Task<List<Service>>> _services;

public QueryBasedLoadBalancer()
{
_services = () => Task.FromResult(new List<Service>());
}

public QueryBasedLoadBalancer(Func<Task<List<Service>>> services)
{
_services = services;
}

public string Type => nameof(QueryBasedLoadBalancer);

public void Release(ServiceHostAndPort hostAndPort) { }

public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext)
{
var services = await _services();

if (services == null || services.Count == 0)
{
return new ErrorResponse<ServiceHostAndPort>(
new NoServicesAvailableError("No services available for load balancing"));
}

if (!httpContext.Request.Query.TryGetValue("id", out var idValues)
|| !int.TryParse(idValues.FirstOrDefault(), out var id))
{
var firstService = services[0];
return new OkResponse<ServiceHostAndPort>(firstService.HostAndPort);
}

var replicaIndex = id % services.Count;
var selectedService = services[replicaIndex];

return new OkResponse<ServiceHostAndPort>(selectedService.HostAndPort);
}
}
8 changes: 8 additions & 0 deletions ApiGateway/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
15 changes: 15 additions & 0 deletions ApiGateway/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Cors": {
"AllowedOrigins": [
"https://example.com",
"https://api.example.com"
]
}
}
28 changes: 28 additions & 0 deletions ApiGateway/ocelot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"Routes": [
{
"DownstreamPathTemplate": "/course",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/course",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5000
},
{
"Host": "localhost",
"Port": 5001
},
{
"Host": "localhost",
"Port": 5002
}
],
"LoadBalancerOptions": {
"Type": "QueryBasedLoadBalancer"
}
}
],
"GlobalConfiguration": {}
}
23 changes: 23 additions & 0 deletions AppHost/AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.5.2" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.2" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.5.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ApiGateway\ApiGateway.csproj" />
<ProjectReference Include="..\Client.Wasm\Client.Wasm.csproj" />
<ProjectReference Include="..\GenerationService\GenerationService.csproj" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedis("cache")
.WithRedisInsight();

var generationService0 = builder.AddProject<Projects.GenerationService>("generation-service-0")
.WithReference(cache)
.WithEndpoint("http", endpoint => endpoint.Port = 5000)
.WaitFor(cache);

var generationService1 = builder.AddProject<Projects.GenerationService>("generation-service-1")
.WithReference(cache)
.WithEndpoint("http", endpoint => endpoint.Port = 5001)
.WaitFor(cache);

var generationService2 = builder.AddProject<Projects.GenerationService>("generation-service-2")
.WithReference(cache)
.WithEndpoint("http", endpoint => endpoint.Port = 5002)
.WaitFor(cache);

var apiGateway = builder.AddProject<Projects.ApiGateway>("api-gateway")
.WithEndpoint("http", endpoint => endpoint.Port = 5100)
.WithExternalHttpEndpoints()
.WaitFor(generationService0)
.WaitFor(generationService1)
.WaitFor(generationService2);

builder.AddProject<Projects.Client_Wasm>("client-wasm")
.WithExternalHttpEndpoints()
.WithReference(apiGateway)
.WaitFor(apiGateway);

builder.Build().Run();
17 changes: 17 additions & 0 deletions AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17136;http://localhost:15136",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21197",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:20131"
}
}
}
}
8 changes: 4 additions & 4 deletions Client.Wasm/Components/StudentCard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
</CardHeader>
<CardBody>
<UnorderedList Unstyled>
<UnorderedListItem>Номер <Strong>№X "Название лабораторной"</Strong></UnorderedListItem>
<UnorderedListItem>Вариант <Strong>№Х "Название варианта"</Strong></UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Фамилией Именем 65ХХ</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://puginarug.com/">Ссылка на форк</Link></UnorderedListItem>
<UnorderedListItem>Номер <Strong>№2 "Балансировка нагрузки"</Strong></UnorderedListItem>
<UnorderedListItem>Вариант <Strong>№12 "Учебный курс"</Strong></UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Прибыловым Александром 6511</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://github.com/AlexPrib/cloud-development">Ссылка на форк</Link></UnorderedListItem>
</UnorderedList>
</CardBody>
</Card>
6 changes: 3 additions & 3 deletions Client.Wasm/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5127",
"environmentVariables": {
Expand All @@ -22,7 +22,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7282;http://localhost:5127",
"environmentVariables": {
Expand All @@ -31,7 +31,7 @@
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
4 changes: 2 additions & 2 deletions Client.Wasm/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
}
},
"AllowedHosts": "*",
"BaseAddress": ""
}
"BaseAddress": "http://localhost:5100/course"
}
Loading