-
Notifications
You must be signed in to change notification settings - Fork 50
Маясов Данила Лаб. 2 Группа 6511 #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <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="..\CreditApp\CreditApp.ServiceDefaults\CreditApp.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
| using Ocelot.Responses; | ||
| using Ocelot.Values; | ||
|
|
||
| namespace Api.Gateway.LoadBalancing; | ||
|
|
||
| /// <summary> | ||
| /// Балансировщик нагрузки на основе параметра запроса. | ||
| /// Реплика определяется как остаток от деления id на число реплик: index = id % N. | ||
| /// </summary> | ||
| public class QueryBasedLoadBalancer(Func<Task<List<Service>>> services) : ILoadBalancer | ||
| { | ||
| public string Type => nameof(QueryBasedLoadBalancer); | ||
|
|
||
| public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext) | ||
| { | ||
| var list = await services(); | ||
|
|
||
| if (list.Count == 0) | ||
| throw new InvalidOperationException("No available downstream services."); | ||
|
|
||
| var query = httpContext.Request.Query; | ||
|
|
||
| if (!query.ContainsKey("id") || !int.TryParse(query["id"], out var id)) | ||
| { | ||
| return new OkResponse<ServiceHostAndPort>(list[0].HostAndPort); | ||
| } | ||
|
|
||
| var index = Math.Abs(id) % list.Count; | ||
| return new OkResponse<ServiceHostAndPort>(list[index].HostAndPort); | ||
| } | ||
|
|
||
| public void Release(ServiceHostAndPort hostAndPort) { } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| using Api.Gateway.LoadBalancing; | ||
| using Ocelot.DependencyInjection; | ||
| using Ocelot.Middleware; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
| builder.Services.AddServiceDiscovery(); | ||
| builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); | ||
|
|
||
| var overrides = new Dictionary<string, string?>(); | ||
| for (var i = 0; Environment.GetEnvironmentVariable($"services__credit-app-{i}__https__0") is { } url; i++) | ||
| { | ||
| var uri = new Uri(url); | ||
| overrides[$"Routes:0:DownstreamHostAndPorts:{i}:Host"] = uri.Host; | ||
| overrides[$"Routes:0:DownstreamHostAndPorts:{i}:Port"] = uri.Port.ToString(); | ||
| } | ||
|
|
||
| if (overrides.Count > 0) | ||
| builder.Configuration.AddInMemoryCollection(overrides); | ||
|
|
||
| builder.Services.AddOcelot() | ||
| .AddCustomLoadBalancer<QueryBasedLoadBalancer>((_, _, discoveryProvider) => new(discoveryProvider.GetAsync)); | ||
|
|
||
| var allowedOrigins = builder.Configuration.GetSection("CorsSettings:AllowedOrigins").Get<string[]>() ?? []; | ||
| builder.Services.AddCors(options => options.AddPolicy("AllowClient", policy => | ||
| policy.WithOrigins(allowedOrigins) | ||
| .AllowAnyMethod() | ||
| .AllowAnyHeader())); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.UseCors("AllowClient"); | ||
| app.MapDefaultEndpoints(); | ||
|
|
||
| await app.UseOcelot(); | ||
|
|
||
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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:63809", | ||
| "sslPort": 44394 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5087", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7265;http://localhost:5087", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "CorsSettings": { | ||
| "AllowedOrigins": [ | ||
| "http://localhost:5127", | ||
| "https://localhost:7282" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "Routes": [ | ||
| { | ||
| "UpstreamPathTemplate": "/credit-application", | ||
| "UpstreamHttpMethod": [ "GET" ], | ||
| "DownstreamPathTemplate": "/api/credit-application", | ||
| "DownstreamScheme": "https", | ||
| "DownstreamHostAndPorts": [ | ||
| { "Host": "localhost", "Port": 8000 }, | ||
| { "Host": "localhost", "Port": 8001 }, | ||
| { "Host": "localhost", "Port": 8002 } | ||
danlla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| "LoadBalancerOptions": { | ||
| "Type": "QueryBasedLoadBalancer" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,5 @@ | |
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "" | ||
| "BaseAddress": "https://localhost:7265/credit-application" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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="Aspire.StackExchange.Redis.DistributedCaching" Version="9.5.2" /> | ||
| <PackageReference Include="Bogus" Version="35.6.5" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\CreditApp\CreditApp.ServiceDefaults\CreditApp.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Folder Include="Models\" /> | ||
| <Folder Include="Services\" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| namespace CreditApp.Api.Models; | ||
|
|
||
| /// <summary> | ||
| /// Кредитная заявка | ||
| /// </summary> | ||
| public class CreditApplication | ||
| { | ||
| /// <summary> | ||
| /// Идентификатор в системе | ||
| /// </summary> | ||
| public int Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Тип кредита (например, "Потребительский", "Ипотека", "Автокредит") | ||
| /// </summary> | ||
| public required string LoanType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Запрашиваемая сумма, округлённая до двух знаков после запятой | ||
| /// </summary> | ||
| public decimal RequestedAmount { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Срок кредита в месяцах | ||
| /// </summary> | ||
| public int TermMonths { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Процентная ставка (не менее ставки ЦБ РФ), округлённая до двух знаков после запятой | ||
| /// </summary> | ||
| public double InterestRate { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата подачи заявки (не более двух лет назад от текущей даты) | ||
| /// </summary> | ||
| public DateOnly ApplicationDate { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Необходимость страховки | ||
| /// </summary> | ||
| public bool InsuranceRequired { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Статус заявки (например, "Новая", "В обработке", "Одобрена", "Отклонена") | ||
| /// </summary> | ||
| public required string Status { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата решения. Заполняется только для терминальных статусов ("Одобрена", "Отклонена") | ||
| /// </summary> | ||
| public DateOnly? DecisionDate { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Одобренная сумма. Заполняется только при статусе "Одобрена", не превышает <see cref="RequestedAmount"/> | ||
| /// </summary> | ||
| public decimal? ApprovedAmount { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using CreditApp.Api.Services; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
| builder.AddRedisDistributedCache("redis"); | ||
|
|
||
| builder.Services.AddSingleton<CreditApplicationGenerator>(); | ||
| builder.Services.AddScoped<ICreditApplicationService, CreditApplicationService>(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| app.MapGet("/api/credit-application", async (int id, ICreditApplicationService service) => | ||
| await service.GetOrGenerate(id)); | ||
|
|
||
| app.Run(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.