From be2d775c3adc253776fdaae1e3e809c26245ab04 Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Wed, 11 Mar 2026 18:20:56 +0400 Subject: [PATCH 01/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=20DT?= =?UTF-8?q?O=20=E2=80=9C=D0=A2=D0=BE=D0=B2=D0=B0=D1=80=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BA=D0=BB=D0=B0=D0=B4=D0=B5=E2=80=9D,=20=D0=BD=D0=B0?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D1=8B=20=D0=B3=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=B8=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20=D1=81?= =?UTF-8?q?=20=D0=BA=D1=8D=D1=88=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=B2=20Redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client.Wasm/Components/StudentCard.razor | 8 +- Client.Wasm/_Imports.razor | 2 +- Client.Wasm/wwwroot/appsettings.json | 2 +- CloudDevelopment.sln | 18 ++++ WarehouseItem.AppHost/AppHost.cs | 14 +++ .../Properties/launchSettings.json | 29 +++++ .../WarehouseItem.AppHost.csproj | 23 ++++ .../appsettings.Development.json | 8 ++ WarehouseItem.AppHost/appsettings.json | 9 ++ .../Controller/WarehouseItemController.cs | 36 +++++++ .../DTO/WarehouseItemDTO.cs | 54 ++++++++++ .../Generator/WarehouseItemGenerator.cs | 99 +++++++++++++++++ WarehouseItem.Generator/Program.cs | 41 +++++++ .../Properties/LaunchSettings.json | 38 +++++++ .../Service/IWarehouseItemService.cs | 8 ++ .../Service/WarehouseItemService.cs | 98 +++++++++++++++++ .../WarehouseItem.Generator.csproj | 20 ++++ .../appsettings.Development.json | 8 ++ WarehouseItem.Generator/appsettings.json | 15 +++ WarehouseItem.ServiceDefaults/Extensions.cs | 100 ++++++++++++++++++ .../WarehouseItem.ServiceDefaults.csproj | 22 ++++ 21 files changed, 646 insertions(+), 6 deletions(-) create mode 100644 WarehouseItem.AppHost/AppHost.cs create mode 100644 WarehouseItem.AppHost/Properties/launchSettings.json create mode 100644 WarehouseItem.AppHost/WarehouseItem.AppHost.csproj create mode 100644 WarehouseItem.AppHost/appsettings.Development.json create mode 100644 WarehouseItem.AppHost/appsettings.json create mode 100644 WarehouseItem.Generator/Controller/WarehouseItemController.cs create mode 100644 WarehouseItem.Generator/DTO/WarehouseItemDTO.cs create mode 100644 WarehouseItem.Generator/Generator/WarehouseItemGenerator.cs create mode 100644 WarehouseItem.Generator/Program.cs create mode 100644 WarehouseItem.Generator/Properties/LaunchSettings.json create mode 100644 WarehouseItem.Generator/Service/IWarehouseItemService.cs create mode 100644 WarehouseItem.Generator/Service/WarehouseItemService.cs create mode 100644 WarehouseItem.Generator/WarehouseItem.Generator.csproj create mode 100644 WarehouseItem.Generator/appsettings.Development.json create mode 100644 WarehouseItem.Generator/appsettings.json create mode 100644 WarehouseItem.ServiceDefaults/Extensions.cs create mode 100644 WarehouseItem.ServiceDefaults/WarehouseItem.ServiceDefaults.csproj diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..5d188b50 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №1 "Кэширование" + Вариант №23 "Товар на складе" + Выполнена Карповым Владимиром 6512 + Ссылка на форк diff --git a/Client.Wasm/_Imports.razor b/Client.Wasm/_Imports.razor index 31e16a84..5b318dbb 100644 --- a/Client.Wasm/_Imports.razor +++ b/Client.Wasm/_Imports.razor @@ -8,7 +8,7 @@ @using Microsoft.JSInterop @using Client.Wasm.Layout @using Client.Wasm.Components -@using Blazorise +@using Blazorise @using Blazorise.Components @using System.Text.Json @using System.Text.Json.Nodes \ No newline at end of file diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..8a3e0c6c 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "http://localhost:5204/api/warehouse-item" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..5eeb6bd3 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -5,6 +5,12 @@ VisualStudioVersion = 17.14.36811.4 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Wasm", "Client.Wasm\Client.Wasm.csproj", "{AE7EEA74-2FE0-136F-D797-854FD87E022A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.ServiceDefaults", "WarehouseItem.ServiceDefaults\WarehouseItem.ServiceDefaults.csproj", "{97B30C3C-3125-4E99-BA67-240DD8126A25}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.Generator", "WarehouseItem.Generator\WarehouseItem.Generator.csproj", "{A9B4BB9B-9F03-4C1F-AB67-9DAD2E4D66BA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.AppHost", "WarehouseItem.AppHost\WarehouseItem.AppHost.csproj", "{07AFB6CB-7359-432D-BF0B-14BA7C582AA5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +21,18 @@ Global {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.ActiveCfg = Release|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.Build.0 = Release|Any CPU + {97B30C3C-3125-4E99-BA67-240DD8126A25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {97B30C3C-3125-4E99-BA67-240DD8126A25}.Debug|Any CPU.Build.0 = Debug|Any CPU + {97B30C3C-3125-4E99-BA67-240DD8126A25}.Release|Any CPU.ActiveCfg = Release|Any CPU + {97B30C3C-3125-4E99-BA67-240DD8126A25}.Release|Any CPU.Build.0 = Release|Any CPU + {A9B4BB9B-9F03-4C1F-AB67-9DAD2E4D66BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A9B4BB9B-9F03-4C1F-AB67-9DAD2E4D66BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A9B4BB9B-9F03-4C1F-AB67-9DAD2E4D66BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A9B4BB9B-9F03-4C1F-AB67-9DAD2E4D66BA}.Release|Any CPU.Build.0 = Release|Any CPU + {07AFB6CB-7359-432D-BF0B-14BA7C582AA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07AFB6CB-7359-432D-BF0B-14BA7C582AA5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07AFB6CB-7359-432D-BF0B-14BA7C582AA5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07AFB6CB-7359-432D-BF0B-14BA7C582AA5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WarehouseItem.AppHost/AppHost.cs b/WarehouseItem.AppHost/AppHost.cs new file mode 100644 index 00000000..0e7fd34f --- /dev/null +++ b/WarehouseItem.AppHost/AppHost.cs @@ -0,0 +1,14 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("warehouse-item-cache") + .WithRedisInsight(containerName: "warehouse-item-insight"); + +var generator = builder.AddProject("generator") + .WithReference(cache, "warehouse-item-cache") + .WaitFor(cache); + +builder.AddProject("client") + .WithReference(generator) + .WaitFor(generator); + +builder.Build().Run(); diff --git a/WarehouseItem.AppHost/Properties/launchSettings.json b/WarehouseItem.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..330da1c2 --- /dev/null +++ b/WarehouseItem.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17129;http://localhost:15221", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21101", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22255" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15221", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19083", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20274" + } + } + } +} \ No newline at end of file diff --git a/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj new file mode 100644 index 00000000..b9e8bcb5 --- /dev/null +++ b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj @@ -0,0 +1,23 @@ + + + + + + Exe + net8.0 + enable + enable + ed7e1e47-dc98-4419-8424-85412466aa9b + + + + + + + + + + + + + diff --git a/WarehouseItem.AppHost/appsettings.Development.json b/WarehouseItem.AppHost/appsettings.Development.json new file mode 100644 index 00000000..1b2d3baf --- /dev/null +++ b/WarehouseItem.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} \ No newline at end of file diff --git a/WarehouseItem.AppHost/appsettings.json b/WarehouseItem.AppHost/appsettings.json new file mode 100644 index 00000000..888f884e --- /dev/null +++ b/WarehouseItem.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} \ No newline at end of file diff --git a/WarehouseItem.Generator/Controller/WarehouseItemController.cs b/WarehouseItem.Generator/Controller/WarehouseItemController.cs new file mode 100644 index 00000000..f3cae97b --- /dev/null +++ b/WarehouseItem.Generator/Controller/WarehouseItemController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Mvc; +using WarehouseItem.Generator.DTO; +using WarehouseItem.Generator.Service; + +namespace WarehouseItem.Generator.Controller; + +/// +/// API контроллер для работы с товарами склада. +/// +[ApiController] +[Route("api/warehouse-item")] +public sealed class WarehouseItemController(ILogger logger, IWarehouseItemService service) : ControllerBase +{ + /// + /// Получить товар по id. + /// + /// Идентификатор товара + /// Токен отмены + /// Данные товара + [HttpGet] + [ProducesResponseType(typeof(WarehouseItemDto), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task> Get([FromQuery] int id, CancellationToken cancellationToken) + { + if (id < 0) + { + return BadRequest(new { message = "id cannot be negative" }); + } + + logger.LogInformation("Request warehouse item id={id}.", id); + var dto = await service.GetAsync(id, cancellationToken); + logger.LogInformation("Response warehouse item id={id}.", id); + + return Ok(dto); + } +} diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs b/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs new file mode 100644 index 00000000..85f7026b --- /dev/null +++ b/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs @@ -0,0 +1,54 @@ +namespace WarehouseItem.Generator.DTO; + +public sealed class WarehouseItemDto +{ + /// + /// Уникальный идентификатор товара + /// + public int Id { get; set; } + + /// + /// Название продукта + /// + public string ProductName { get; set; } = string.Empty; + + /// + /// Категория товара + /// + public string Category { get; set; } = string.Empty; + + /// + /// Количество единиц на складе + /// + public int StockQuantity { get; set; } + + /// + /// Цена за единицу товара + /// + public decimal UnitPrice { get; set; } + + /// + /// Вес одной единицы в килограммах + /// + public double UnitWeight { get; set; } + + /// + /// Размеры одной единицы товара + /// + public string UnitDimensions { get; set; } = string.Empty; + + /// + /// Флаг хрупкого товара + /// + public bool IsFragile { get; set; } + + /// + /// Дата последней поставки + /// + public DateOnly LastDeliveryDate { get; set; } + + /// + /// Дата планируемой следующей поставки + /// + public DateOnly NextDeliveryDate { get; set; } +} diff --git a/WarehouseItem.Generator/Generator/WarehouseItemGenerator.cs b/WarehouseItem.Generator/Generator/WarehouseItemGenerator.cs new file mode 100644 index 00000000..b36a7aa5 --- /dev/null +++ b/WarehouseItem.Generator/Generator/WarehouseItemGenerator.cs @@ -0,0 +1,99 @@ +using Bogus; +using WarehouseItem.Generator.DTO; + +namespace WarehouseItem.Generator.Generator; + +/// +/// Генератор тестовых данных для товаров на складе +/// +public sealed class WarehouseItemGenerator(ILogger logger) +{ + /// + /// Максимальный размер запаса товара на складе + /// + private const int MaxStockQuantity = 25000; + /// + /// Минимальная цена за единицу товара + /// + private const decimal MinUnitPrice = 5m; + /// + /// Максимальная цена за единицу товара + /// + private const decimal MaxUnitPrice = 250_000m; + /// + /// Минимальный вес единицы товара в килограммах + /// + private const double MinUnitWeight = 0.01; + /// + /// Максимальный вес единицы товара в килограммах + /// + private const double MaxUnitWeight = 250.0; + /// + /// Минимальный размер измерения в сантиметрах + /// + private const int MinDimensionCm = 1; + /// + /// Максимальный размер измерения в сантиметрах + /// + private const int MaxDimensionCm = 99; + /// + /// Максимальное количество дней назад для даты последней поставки + /// + private const int MaxLastDeliveryDaysAgo = 365; + + /// + /// Faker для генерации тестовых данных товаров + /// + private static readonly Faker_faker = new Faker("ru") + .RuleFor(x => x.ProductName, f => f.Commerce.ProductName()) + .RuleFor(x => x.Category, f => f.Commerce.Department(1)) + .RuleFor(x => x.StockQuantity, f => f.Random.Int(0, MaxStockQuantity)) + .RuleFor(x => x.UnitPrice, + f => Math.Round(f.Random.Decimal(MinUnitPrice, MaxUnitPrice), 2, MidpointRounding.AwayFromZero)) + .RuleFor(x => x.UnitWeight, + f => Math.Round(f.Random.Double(MinUnitWeight, MaxUnitWeight), 2, MidpointRounding.AwayFromZero)) + .RuleFor(x => x.UnitDimensions, f => + { + var a = f.Random.Int(MinDimensionCm, MaxDimensionCm); + var b = f.Random.Int(MinDimensionCm, MaxDimensionCm); + var c = f.Random.Int(MinDimensionCm, MaxDimensionCm); + return $"{a:D2}x{b:D2}x{c:D2} см"; + }) + .RuleFor(x => x.IsFragile, f => f.Random.Bool(0.25f)) + .RuleFor(x => x.LastDeliveryDate, f => + { + var today = DateOnly.FromDateTime(DateTime.Today); + var daysAgo = f.Random.Int(0, MaxLastDeliveryDaysAgo); + return today.AddDays(-daysAgo); + }) + .RuleFor(x => x.NextDeliveryDate, (f, dto) => dto.LastDeliveryDate.AddDays(f.Random.Int(0, 90))); + + /// + /// Генерирует случайные данные товара с указанным идентификатором + /// + /// Уникальный идентификатор товара + /// Объект WarehouseItemDto с случайно сгенерированными данными + public WarehouseItemDto Generate(int id) + { + logger.LogInformation("Generating warehouse item for id={id}", id); + + var item = _faker.Generate(); + item.Id = id; + + logger.LogInformation("Warehouse item generated: {@Item}", new + { + item.Id, + item.ProductName, + item.Category, + item.StockQuantity, + item.UnitPrice, + item.UnitWeight, + item.UnitDimensions, + item.IsFragile, + item.LastDeliveryDate, + item.NextDeliveryDate + }); + + return item; + } +} diff --git a/WarehouseItem.Generator/Program.cs b/WarehouseItem.Generator/Program.cs new file mode 100644 index 00000000..02074e9f --- /dev/null +++ b/WarehouseItem.Generator/Program.cs @@ -0,0 +1,41 @@ +using WarehouseItem.Generator.Generator; +using WarehouseItem.Generator.Service; +using WarehouseItem.ServiceDefaults; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("warehouse-item-cache"); + +builder.Services.AddCors(options => +{ + options.AddPolicy("AllowLocalDev", policy => + { + policy + .AllowAnyOrigin() + .WithHeaders("Content-Type") + .WithMethods("GET"); + }); +}); + +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +app.UseCors("AllowLocalDev"); + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapControllers(); +app.MapDefaultEndpoints(); + +app.Run(); diff --git a/WarehouseItem.Generator/Properties/LaunchSettings.json b/WarehouseItem.Generator/Properties/LaunchSettings.json new file mode 100644 index 00000000..cbeaf732 --- /dev/null +++ b/WarehouseItem.Generator/Properties/LaunchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:39434", + "sslPort": 44329 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5204", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7291;http://localhost:5204", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/WarehouseItem.Generator/Service/IWarehouseItemService.cs b/WarehouseItem.Generator/Service/IWarehouseItemService.cs new file mode 100644 index 00000000..d965c5a3 --- /dev/null +++ b/WarehouseItem.Generator/Service/IWarehouseItemService.cs @@ -0,0 +1,8 @@ +using WarehouseItem.Generator.DTO; + +namespace WarehouseItem.Generator.Service; + +public interface IWarehouseItemService +{ + public Task GetAsync(int id, CancellationToken cancellationToken = default); +} diff --git a/WarehouseItem.Generator/Service/WarehouseItemService.cs b/WarehouseItem.Generator/Service/WarehouseItemService.cs new file mode 100644 index 00000000..ad65f291 --- /dev/null +++ b/WarehouseItem.Generator/Service/WarehouseItemService.cs @@ -0,0 +1,98 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Microsoft.Extensions.Caching.Distributed; +using WarehouseItem.Generator.DTO; +using WarehouseItem.Generator.Generator; + +namespace WarehouseItem.Generator.Service; + +public sealed class WarehouseItemService( + ILogger logger, + WarehouseItemGenerator generator, + IDistributedCache cache, + IConfiguration configuration) : IWarehouseItemService +{ + private const string CacheKeyPrefix = "warehouse-item:"; + private const int CacheExpirationTimeMinutesDefault = 15; + + private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web) + { + DefaultIgnoreCondition = JsonIgnoreCondition.Never + }; + + private readonly TimeSpan _cacheTtl = TimeSpan.FromMinutes( + configuration.GetValue("CacheSettings:ExpirationTimeMinutes", CacheExpirationTimeMinutesDefault)); + + public async Task GetAsync(int id, CancellationToken cancellationToken = default) + { + var cacheKey = $"{CacheKeyPrefix}{id}"; + + var cached = await TryReadCacheAsync(cacheKey, cancellationToken); + if (cached is not null) + { + return cached; + } + + var generated = generator.Generate(id); + await TryWriteCacheAsync(cacheKey, generated, cancellationToken); + + return generated; + } + + private async Task TryReadCacheAsync(string cacheKey, CancellationToken cancellationToken) + { + string? json; + try + { + json = await cache.GetStringAsync(cacheKey, cancellationToken); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Cache read failed for key={cacheKey}.", cacheKey); + return null; + } + + if (string.IsNullOrWhiteSpace(json)) + { + logger.LogInformation("Cache miss for key={cacheKey}.", cacheKey); + return null; + } + + try + { + var obj = JsonSerializer.Deserialize(json, _jsonOptions); + if (obj is null) + { + logger.LogWarning("Cache value for key={cacheKey} deserialized as null.", cacheKey); + return null; + } + + logger.LogInformation("Cache hit for id={id}.", obj.Id); + return obj; + } + catch (Exception ex) + { + logger.LogWarning(ex, "Cache JSON invalid for key={cacheKey}.", cacheKey); + return null; + } + } + + private async Task TryWriteCacheAsync(string cacheKey, WarehouseItemDto value, CancellationToken cancellationToken) + { + try + { + var options = new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheTtl + }; + + var json = JsonSerializer.Serialize(value, _jsonOptions); + await cache.SetStringAsync(cacheKey, json, options, cancellationToken); + logger.LogInformation("Cached id={id} for ttl={ttlMinutes}m.", value.Id, _cacheTtl.TotalMinutes); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Cache write failed for id={id}.", value.Id); + } + } +} diff --git a/WarehouseItem.Generator/WarehouseItem.Generator.csproj b/WarehouseItem.Generator/WarehouseItem.Generator.csproj new file mode 100644 index 00000000..1883551f --- /dev/null +++ b/WarehouseItem.Generator/WarehouseItem.Generator.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + WarehouseItem.Generator + + + + + + + + + + + + + diff --git a/WarehouseItem.Generator/appsettings.Development.json b/WarehouseItem.Generator/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/WarehouseItem.Generator/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/WarehouseItem.Generator/appsettings.json b/WarehouseItem.Generator/appsettings.json new file mode 100644 index 00000000..98599fad --- /dev/null +++ b/WarehouseItem.Generator/appsettings.json @@ -0,0 +1,15 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "WarehouseItemCache": { + "AbsoluteExpirationSeconds": 300 + }, + "ConnectionStrings": { + "redis": "" + }, + "AllowedHosts": "*" +} diff --git a/WarehouseItem.ServiceDefaults/Extensions.cs b/WarehouseItem.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..e1fe3ef1 --- /dev/null +++ b/WarehouseItem.ServiceDefaults/Extensions.cs @@ -0,0 +1,100 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace WarehouseItem.ServiceDefaults; + +// Common cross-service wiring for Aspire: telemetry, health checks, service discovery, and resilient HttpClient. +public static class Extensions +{ + private const string ReadyPath = "/health"; + private const string AlivePath = "/alive"; + private const string LiveTag = "live"; + + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.AddDefaultHealthChecks(); + builder.ConfigureOpenTelemetry(); + builder.Services.AddServiceDiscovery(); + builder.Services.ConfigureHttpClientDefaults(static http => + { + http.AddStandardResilienceHandler(); + http.AddServiceDiscovery(); + }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(static logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(static metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(options => + { + options.Filter = (HttpContext context) => !IsHealthRequest(context.Request.Path); + }) + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var otlpEndpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; + if (!string.IsNullOrWhiteSpace(otlpEndpoint)) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + .AddCheck("self", () => HealthCheckResult.Healthy(), new[] { LiveTag }); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + if (app.Environment.IsDevelopment()) + { + app.MapHealthChecks(ReadyPath); + app.MapHealthChecks(AlivePath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains(LiveTag) + }); + } + + return app; + } + + private static bool IsHealthRequest(PathString path) + => path.StartsWithSegments(ReadyPath) || path.StartsWithSegments(AlivePath); +} diff --git a/WarehouseItem.ServiceDefaults/WarehouseItem.ServiceDefaults.csproj b/WarehouseItem.ServiceDefaults/WarehouseItem.ServiceDefaults.csproj new file mode 100644 index 00000000..230756f9 --- /dev/null +++ b/WarehouseItem.ServiceDefaults/WarehouseItem.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + \ No newline at end of file From 3f061945997544c0f39b30b0b6d17f158f1006bc Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Fri, 13 Mar 2026 19:20:39 +0400 Subject: [PATCH 02/13] some fixes --- WarehouseItem.AppHost/AppHost.cs | 1 - .../WarehouseItem.AppHost.csproj | 6 +- .../DTO/WarehouseItemDTO.cs | 3 + WarehouseItem.Generator/Program.cs | 1 + .../Service/IWarehouseItemCache.cs | 27 +++++ .../Service/IWarehouseItemService.cs | 10 ++ .../Service/WarehouseItemCache.cs | 103 ++++++++++++++++++ .../Service/WarehouseItemService.cs | 90 ++------------- .../WarehouseItem.Generator.csproj | 2 +- WarehouseItem.Generator/appsettings.json | 3 - 10 files changed, 160 insertions(+), 86 deletions(-) create mode 100644 WarehouseItem.Generator/Service/IWarehouseItemCache.cs create mode 100644 WarehouseItem.Generator/Service/WarehouseItemCache.cs diff --git a/WarehouseItem.AppHost/AppHost.cs b/WarehouseItem.AppHost/AppHost.cs index 0e7fd34f..77d7c835 100644 --- a/WarehouseItem.AppHost/AppHost.cs +++ b/WarehouseItem.AppHost/AppHost.cs @@ -8,7 +8,6 @@ .WaitFor(cache); builder.AddProject("client") - .WithReference(generator) .WaitFor(generator); builder.Build().Run(); diff --git a/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj index b9e8bcb5..2550d0e2 100644 --- a/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj +++ b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj @@ -1,6 +1,6 @@ - + Exe @@ -11,8 +11,8 @@ - - + + diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs b/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs index 85f7026b..dc954c7b 100644 --- a/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs +++ b/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs @@ -1,5 +1,8 @@ namespace WarehouseItem.Generator.DTO; +/// +/// DTO для передачи данных о товаре на складе, включая идентификатор, название, категорию, количество, цену и другие характеристики. +/// public sealed class WarehouseItemDto { /// diff --git a/WarehouseItem.Generator/Program.cs b/WarehouseItem.Generator/Program.cs index 02074e9f..0d3c04ee 100644 --- a/WarehouseItem.Generator/Program.cs +++ b/WarehouseItem.Generator/Program.cs @@ -19,6 +19,7 @@ }); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddControllers(); diff --git a/WarehouseItem.Generator/Service/IWarehouseItemCache.cs b/WarehouseItem.Generator/Service/IWarehouseItemCache.cs new file mode 100644 index 00000000..845a9dc1 --- /dev/null +++ b/WarehouseItem.Generator/Service/IWarehouseItemCache.cs @@ -0,0 +1,27 @@ +using System.Threading; +using System.Threading.Tasks; +using WarehouseItem.Generator.DTO; + +namespace WarehouseItem.Generator.Service; + +/// +/// Интерфейс для кэширования товаров. +/// +public interface IWarehouseItemCache +{ + /// + /// Получить товар из кэша по идентификатору. + /// + /// Идентификатор товара. + /// Токен отмены. + /// DTO товара или null, если не найден в кэше. + public Task GetAsync(int id, CancellationToken cancellationToken = default); + + /// + /// Сохранить товар в кэш. + /// + /// Идентификатор товара. + /// DTO товара для сохранения. + /// Токен отмены. + public Task SetAsync(int id, WarehouseItemDto value, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/WarehouseItem.Generator/Service/IWarehouseItemService.cs b/WarehouseItem.Generator/Service/IWarehouseItemService.cs index d965c5a3..1ca1055f 100644 --- a/WarehouseItem.Generator/Service/IWarehouseItemService.cs +++ b/WarehouseItem.Generator/Service/IWarehouseItemService.cs @@ -1,8 +1,18 @@ +using System.Threading.Tasks; using WarehouseItem.Generator.DTO; namespace WarehouseItem.Generator.Service; +/// +/// Интерфейс для сервиса работы с товарами на складе. +/// public interface IWarehouseItemService { + /// + /// Получить товар по идентификатору. + /// + /// Идентификатор товара. + /// Токен отмены. + /// DTO товара. public Task GetAsync(int id, CancellationToken cancellationToken = default); } diff --git a/WarehouseItem.Generator/Service/WarehouseItemCache.cs b/WarehouseItem.Generator/Service/WarehouseItemCache.cs new file mode 100644 index 00000000..fe98b00e --- /dev/null +++ b/WarehouseItem.Generator/Service/WarehouseItemCache.cs @@ -0,0 +1,103 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using WarehouseItem.Generator.DTO; + +namespace WarehouseItem.Generator.Service; + +/// +/// Реализация кэширования товаров с использованием распределенного кэша. +/// +public sealed class WarehouseItemCache( + ILogger logger, + IDistributedCache cache, + IConfiguration configuration) : IWarehouseItemCache +{ + private const string CacheKeyPrefix = "warehouse-item:"; + private const int CacheExpirationTimeMinutesDefault = 15; + + private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web) + { + DefaultIgnoreCondition = JsonIgnoreCondition.Never + }; + + private readonly TimeSpan _cacheTtl = TimeSpan.FromMinutes( + configuration.GetValue("CacheSettings:ExpirationTimeMinutes", CacheExpirationTimeMinutesDefault)); + + /// + /// Получить товар из кэша по идентификатору. + /// + /// Идентификатор товара. + /// Токен отмены. + /// DTO товара или null, если не найден в кэше. + public async Task GetAsync(int id, CancellationToken cancellationToken = default) + { + var cacheKey = $"{CacheKeyPrefix}{id}"; + + string? json; + try + { + json = await cache.GetStringAsync(cacheKey, cancellationToken); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Cache read failed for key={cacheKey}.", cacheKey); + return null; + } + + if (string.IsNullOrWhiteSpace(json)) + { + logger.LogInformation("Cache miss for key={cacheKey}.", cacheKey); + return null; + } + + try + { + var obj = JsonSerializer.Deserialize(json, _jsonOptions); + if (obj is null) + { + logger.LogWarning("Cache value for key={cacheKey} deserialized as null.", cacheKey); + return null; + } + + logger.LogInformation("Cache hit for id={id}.", obj.Id); + return obj; + } + catch (Exception ex) + { + logger.LogWarning(ex, "Cache JSON invalid for key={cacheKey}.", cacheKey); + return null; + } + } + + /// + /// Сохранить товар в кэш. + /// + /// Идентификатор товара. + /// DTO товара для сохранения. + /// Токен отмены. + public async Task SetAsync(int id, WarehouseItemDto value, CancellationToken cancellationToken = default) + { + var cacheKey = $"{CacheKeyPrefix}{id}"; + + try + { + var options = new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheTtl + }; + + var json = JsonSerializer.Serialize(value, _jsonOptions); + await cache.SetStringAsync(cacheKey, json, options, cancellationToken); + logger.LogInformation("Cached id={id} for ttl={ttlMinutes}m.", value.Id, _cacheTtl.TotalMinutes); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Cache write failed for id={id}.", value.Id); + } + } +} \ No newline at end of file diff --git a/WarehouseItem.Generator/Service/WarehouseItemService.cs b/WarehouseItem.Generator/Service/WarehouseItemService.cs index ad65f291..23dcf713 100644 --- a/WarehouseItem.Generator/Service/WarehouseItemService.cs +++ b/WarehouseItem.Generator/Service/WarehouseItemService.cs @@ -1,98 +1,32 @@ -using System.Text.Json; -using System.Text.Json.Serialization; -using Microsoft.Extensions.Caching.Distributed; using WarehouseItem.Generator.DTO; using WarehouseItem.Generator.Generator; namespace WarehouseItem.Generator.Service; +/// +/// Реализация сервиса работы с товарами на складе. +/// public sealed class WarehouseItemService( - ILogger logger, WarehouseItemGenerator generator, - IDistributedCache cache, - IConfiguration configuration) : IWarehouseItemService + IWarehouseItemCache cache) : IWarehouseItemService { - private const string CacheKeyPrefix = "warehouse-item:"; - private const int CacheExpirationTimeMinutesDefault = 15; - - private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web) - { - DefaultIgnoreCondition = JsonIgnoreCondition.Never - }; - - private readonly TimeSpan _cacheTtl = TimeSpan.FromMinutes( - configuration.GetValue("CacheSettings:ExpirationTimeMinutes", CacheExpirationTimeMinutesDefault)); - + /// + /// Получить товар по идентификатору. Если товар не найден в кэше, генерирует новый и сохраняет в кэш. + /// + /// Идентификатор товара. + /// Токен отмены. + /// DTO товара. public async Task GetAsync(int id, CancellationToken cancellationToken = default) { - var cacheKey = $"{CacheKeyPrefix}{id}"; - - var cached = await TryReadCacheAsync(cacheKey, cancellationToken); + var cached = await cache.GetAsync(id, cancellationToken); if (cached is not null) { return cached; } var generated = generator.Generate(id); - await TryWriteCacheAsync(cacheKey, generated, cancellationToken); + await cache.SetAsync(id, generated, cancellationToken); return generated; } - - private async Task TryReadCacheAsync(string cacheKey, CancellationToken cancellationToken) - { - string? json; - try - { - json = await cache.GetStringAsync(cacheKey, cancellationToken); - } - catch (Exception ex) - { - logger.LogWarning(ex, "Cache read failed for key={cacheKey}.", cacheKey); - return null; - } - - if (string.IsNullOrWhiteSpace(json)) - { - logger.LogInformation("Cache miss for key={cacheKey}.", cacheKey); - return null; - } - - try - { - var obj = JsonSerializer.Deserialize(json, _jsonOptions); - if (obj is null) - { - logger.LogWarning("Cache value for key={cacheKey} deserialized as null.", cacheKey); - return null; - } - - logger.LogInformation("Cache hit for id={id}.", obj.Id); - return obj; - } - catch (Exception ex) - { - logger.LogWarning(ex, "Cache JSON invalid for key={cacheKey}.", cacheKey); - return null; - } - } - - private async Task TryWriteCacheAsync(string cacheKey, WarehouseItemDto value, CancellationToken cancellationToken) - { - try - { - var options = new DistributedCacheEntryOptions - { - AbsoluteExpirationRelativeToNow = _cacheTtl - }; - - var json = JsonSerializer.Serialize(value, _jsonOptions); - await cache.SetStringAsync(cacheKey, json, options, cancellationToken); - logger.LogInformation("Cached id={id} for ttl={ttlMinutes}m.", value.Id, _cacheTtl.TotalMinutes); - } - catch (Exception ex) - { - logger.LogWarning(ex, "Cache write failed for id={id}.", value.Id); - } - } } diff --git a/WarehouseItem.Generator/WarehouseItem.Generator.csproj b/WarehouseItem.Generator/WarehouseItem.Generator.csproj index 1883551f..d345185d 100644 --- a/WarehouseItem.Generator/WarehouseItem.Generator.csproj +++ b/WarehouseItem.Generator/WarehouseItem.Generator.csproj @@ -9,7 +9,7 @@ - + diff --git a/WarehouseItem.Generator/appsettings.json b/WarehouseItem.Generator/appsettings.json index 98599fad..3af4bd71 100644 --- a/WarehouseItem.Generator/appsettings.json +++ b/WarehouseItem.Generator/appsettings.json @@ -8,8 +8,5 @@ "WarehouseItemCache": { "AbsoluteExpirationSeconds": 300 }, - "ConnectionStrings": { - "redis": "" - }, "AllowedHosts": "*" } From 5d6b4e7d119a2ce9d35ef8624a4a5a8e8fb7754b Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 14 Mar 2026 09:56:39 +0400 Subject: [PATCH 03/13] deleted using --- WarehouseItem.Generator/Service/IWarehouseItemCache.cs | 2 -- WarehouseItem.Generator/Service/IWarehouseItemService.cs | 1 - WarehouseItem.Generator/Service/WarehouseItemCache.cs | 4 ---- 3 files changed, 7 deletions(-) diff --git a/WarehouseItem.Generator/Service/IWarehouseItemCache.cs b/WarehouseItem.Generator/Service/IWarehouseItemCache.cs index 845a9dc1..8013c044 100644 --- a/WarehouseItem.Generator/Service/IWarehouseItemCache.cs +++ b/WarehouseItem.Generator/Service/IWarehouseItemCache.cs @@ -1,5 +1,3 @@ -using System.Threading; -using System.Threading.Tasks; using WarehouseItem.Generator.DTO; namespace WarehouseItem.Generator.Service; diff --git a/WarehouseItem.Generator/Service/IWarehouseItemService.cs b/WarehouseItem.Generator/Service/IWarehouseItemService.cs index 1ca1055f..ea932c49 100644 --- a/WarehouseItem.Generator/Service/IWarehouseItemService.cs +++ b/WarehouseItem.Generator/Service/IWarehouseItemService.cs @@ -1,4 +1,3 @@ -using System.Threading.Tasks; using WarehouseItem.Generator.DTO; namespace WarehouseItem.Generator.Service; diff --git a/WarehouseItem.Generator/Service/WarehouseItemCache.cs b/WarehouseItem.Generator/Service/WarehouseItemCache.cs index fe98b00e..ddf951f1 100644 --- a/WarehouseItem.Generator/Service/WarehouseItemCache.cs +++ b/WarehouseItem.Generator/Service/WarehouseItemCache.cs @@ -1,10 +1,6 @@ using System.Text.Json; using System.Text.Json.Serialization; -using System.Threading; -using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; using WarehouseItem.Generator.DTO; namespace WarehouseItem.Generator.Service; From 81baeb1e49ad4742422b8e6802b3b5839d0ed746 Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 14 Mar 2026 10:01:02 +0400 Subject: [PATCH 04/13] test --- WarehouseItem.Generator/DTO/WarehouseItemDTO.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs b/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs index dc954c7b..09a8a230 100644 --- a/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs +++ b/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs @@ -54,4 +54,4 @@ public sealed class WarehouseItemDto /// Дата планируемой следующей поставки /// public DateOnly NextDeliveryDate { get; set; } -} +} \ No newline at end of file From 9212048845124eeadb457c0a9dade3be440207ef Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 14 Mar 2026 10:03:47 +0400 Subject: [PATCH 05/13] rename file --- .../DTO/WarehouseItemDto1.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 WarehouseItem.Generator/DTO/WarehouseItemDto1.cs diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDto1.cs b/WarehouseItem.Generator/DTO/WarehouseItemDto1.cs new file mode 100644 index 00000000..09a8a230 --- /dev/null +++ b/WarehouseItem.Generator/DTO/WarehouseItemDto1.cs @@ -0,0 +1,57 @@ +namespace WarehouseItem.Generator.DTO; + +/// +/// DTO для передачи данных о товаре на складе, включая идентификатор, название, категорию, количество, цену и другие характеристики. +/// +public sealed class WarehouseItemDto +{ + /// + /// Уникальный идентификатор товара + /// + public int Id { get; set; } + + /// + /// Название продукта + /// + public string ProductName { get; set; } = string.Empty; + + /// + /// Категория товара + /// + public string Category { get; set; } = string.Empty; + + /// + /// Количество единиц на складе + /// + public int StockQuantity { get; set; } + + /// + /// Цена за единицу товара + /// + public decimal UnitPrice { get; set; } + + /// + /// Вес одной единицы в килограммах + /// + public double UnitWeight { get; set; } + + /// + /// Размеры одной единицы товара + /// + public string UnitDimensions { get; set; } = string.Empty; + + /// + /// Флаг хрупкого товара + /// + public bool IsFragile { get; set; } + + /// + /// Дата последней поставки + /// + public DateOnly LastDeliveryDate { get; set; } + + /// + /// Дата планируемой следующей поставки + /// + public DateOnly NextDeliveryDate { get; set; } +} \ No newline at end of file From d977691df1e79f187e1c3aadb4064a05b91f9f2c Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 14 Mar 2026 10:07:32 +0400 Subject: [PATCH 06/13] rename --- .../DTO/WarehouseItemDto12.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 WarehouseItem.Generator/DTO/WarehouseItemDto12.cs diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDto12.cs b/WarehouseItem.Generator/DTO/WarehouseItemDto12.cs new file mode 100644 index 00000000..09a8a230 --- /dev/null +++ b/WarehouseItem.Generator/DTO/WarehouseItemDto12.cs @@ -0,0 +1,57 @@ +namespace WarehouseItem.Generator.DTO; + +/// +/// DTO для передачи данных о товаре на складе, включая идентификатор, название, категорию, количество, цену и другие характеристики. +/// +public sealed class WarehouseItemDto +{ + /// + /// Уникальный идентификатор товара + /// + public int Id { get; set; } + + /// + /// Название продукта + /// + public string ProductName { get; set; } = string.Empty; + + /// + /// Категория товара + /// + public string Category { get; set; } = string.Empty; + + /// + /// Количество единиц на складе + /// + public int StockQuantity { get; set; } + + /// + /// Цена за единицу товара + /// + public decimal UnitPrice { get; set; } + + /// + /// Вес одной единицы в килограммах + /// + public double UnitWeight { get; set; } + + /// + /// Размеры одной единицы товара + /// + public string UnitDimensions { get; set; } = string.Empty; + + /// + /// Флаг хрупкого товара + /// + public bool IsFragile { get; set; } + + /// + /// Дата последней поставки + /// + public DateOnly LastDeliveryDate { get; set; } + + /// + /// Дата планируемой следующей поставки + /// + public DateOnly NextDeliveryDate { get; set; } +} \ No newline at end of file From 0a154dc3370fe8116441c159cbc40f90a685e064 Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 14 Mar 2026 10:07:41 +0400 Subject: [PATCH 07/13] rename --- .../DTO/WarehouseItemDTO.cs | 57 ------------------- .../DTO/WarehouseItemDto1.cs | 57 ------------------- 2 files changed, 114 deletions(-) delete mode 100644 WarehouseItem.Generator/DTO/WarehouseItemDTO.cs delete mode 100644 WarehouseItem.Generator/DTO/WarehouseItemDto1.cs diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs b/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs deleted file mode 100644 index 09a8a230..00000000 --- a/WarehouseItem.Generator/DTO/WarehouseItemDTO.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace WarehouseItem.Generator.DTO; - -/// -/// DTO для передачи данных о товаре на складе, включая идентификатор, название, категорию, количество, цену и другие характеристики. -/// -public sealed class WarehouseItemDto -{ - /// - /// Уникальный идентификатор товара - /// - public int Id { get; set; } - - /// - /// Название продукта - /// - public string ProductName { get; set; } = string.Empty; - - /// - /// Категория товара - /// - public string Category { get; set; } = string.Empty; - - /// - /// Количество единиц на складе - /// - public int StockQuantity { get; set; } - - /// - /// Цена за единицу товара - /// - public decimal UnitPrice { get; set; } - - /// - /// Вес одной единицы в килограммах - /// - public double UnitWeight { get; set; } - - /// - /// Размеры одной единицы товара - /// - public string UnitDimensions { get; set; } = string.Empty; - - /// - /// Флаг хрупкого товара - /// - public bool IsFragile { get; set; } - - /// - /// Дата последней поставки - /// - public DateOnly LastDeliveryDate { get; set; } - - /// - /// Дата планируемой следующей поставки - /// - public DateOnly NextDeliveryDate { get; set; } -} \ No newline at end of file diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDto1.cs b/WarehouseItem.Generator/DTO/WarehouseItemDto1.cs deleted file mode 100644 index 09a8a230..00000000 --- a/WarehouseItem.Generator/DTO/WarehouseItemDto1.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace WarehouseItem.Generator.DTO; - -/// -/// DTO для передачи данных о товаре на складе, включая идентификатор, название, категорию, количество, цену и другие характеристики. -/// -public sealed class WarehouseItemDto -{ - /// - /// Уникальный идентификатор товара - /// - public int Id { get; set; } - - /// - /// Название продукта - /// - public string ProductName { get; set; } = string.Empty; - - /// - /// Категория товара - /// - public string Category { get; set; } = string.Empty; - - /// - /// Количество единиц на складе - /// - public int StockQuantity { get; set; } - - /// - /// Цена за единицу товара - /// - public decimal UnitPrice { get; set; } - - /// - /// Вес одной единицы в килограммах - /// - public double UnitWeight { get; set; } - - /// - /// Размеры одной единицы товара - /// - public string UnitDimensions { get; set; } = string.Empty; - - /// - /// Флаг хрупкого товара - /// - public bool IsFragile { get; set; } - - /// - /// Дата последней поставки - /// - public DateOnly LastDeliveryDate { get; set; } - - /// - /// Дата планируемой следующей поставки - /// - public DateOnly NextDeliveryDate { get; set; } -} \ No newline at end of file From 1fc1a118c290bff497ba493842bfa632ec421aa7 Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 14 Mar 2026 10:09:14 +0400 Subject: [PATCH 08/13] Final rename --- .../DTO/WarehouseItemDto.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 WarehouseItem.Generator/DTO/WarehouseItemDto.cs diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDto.cs b/WarehouseItem.Generator/DTO/WarehouseItemDto.cs new file mode 100644 index 00000000..09a8a230 --- /dev/null +++ b/WarehouseItem.Generator/DTO/WarehouseItemDto.cs @@ -0,0 +1,57 @@ +namespace WarehouseItem.Generator.DTO; + +/// +/// DTO для передачи данных о товаре на складе, включая идентификатор, название, категорию, количество, цену и другие характеристики. +/// +public sealed class WarehouseItemDto +{ + /// + /// Уникальный идентификатор товара + /// + public int Id { get; set; } + + /// + /// Название продукта + /// + public string ProductName { get; set; } = string.Empty; + + /// + /// Категория товара + /// + public string Category { get; set; } = string.Empty; + + /// + /// Количество единиц на складе + /// + public int StockQuantity { get; set; } + + /// + /// Цена за единицу товара + /// + public decimal UnitPrice { get; set; } + + /// + /// Вес одной единицы в килограммах + /// + public double UnitWeight { get; set; } + + /// + /// Размеры одной единицы товара + /// + public string UnitDimensions { get; set; } = string.Empty; + + /// + /// Флаг хрупкого товара + /// + public bool IsFragile { get; set; } + + /// + /// Дата последней поставки + /// + public DateOnly LastDeliveryDate { get; set; } + + /// + /// Дата планируемой следующей поставки + /// + public DateOnly NextDeliveryDate { get; set; } +} \ No newline at end of file From ff1d1129da4d0d9d18bcd93482e6ebdcfebe8b7b Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 14 Mar 2026 10:10:18 +0400 Subject: [PATCH 09/13] Final rename --- .../DTO/WarehouseItemDto12.cs | 57 ------------------- 1 file changed, 57 deletions(-) delete mode 100644 WarehouseItem.Generator/DTO/WarehouseItemDto12.cs diff --git a/WarehouseItem.Generator/DTO/WarehouseItemDto12.cs b/WarehouseItem.Generator/DTO/WarehouseItemDto12.cs deleted file mode 100644 index 09a8a230..00000000 --- a/WarehouseItem.Generator/DTO/WarehouseItemDto12.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace WarehouseItem.Generator.DTO; - -/// -/// DTO для передачи данных о товаре на складе, включая идентификатор, название, категорию, количество, цену и другие характеристики. -/// -public sealed class WarehouseItemDto -{ - /// - /// Уникальный идентификатор товара - /// - public int Id { get; set; } - - /// - /// Название продукта - /// - public string ProductName { get; set; } = string.Empty; - - /// - /// Категория товара - /// - public string Category { get; set; } = string.Empty; - - /// - /// Количество единиц на складе - /// - public int StockQuantity { get; set; } - - /// - /// Цена за единицу товара - /// - public decimal UnitPrice { get; set; } - - /// - /// Вес одной единицы в килограммах - /// - public double UnitWeight { get; set; } - - /// - /// Размеры одной единицы товара - /// - public string UnitDimensions { get; set; } = string.Empty; - - /// - /// Флаг хрупкого товара - /// - public bool IsFragile { get; set; } - - /// - /// Дата последней поставки - /// - public DateOnly LastDeliveryDate { get; set; } - - /// - /// Дата планируемой следующей поставки - /// - public DateOnly NextDeliveryDate { get; set; } -} \ No newline at end of file From 925e451febaea06b05e2c3be60a6b56eb9e5039f Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Tue, 14 Apr 2026 10:28:34 +0400 Subject: [PATCH 10/13] Lab 2 --- Client.Wasm/Components/StudentCard.razor | 2 +- Client.Wasm/wwwroot/appsettings.json | 18 ++--- CloudDevelopment.sln | 8 +++ WarehouseItem.AppHost/AppHost.cs | 25 +++++-- .../WarehouseItem.AppHost.csproj | 40 +++++------- .../LoadBalancer/QueryBasedLoadBalancer.cs | 65 +++++++++++++++++++ WarehouseItem.Gateway/Program.cs | 36 ++++++++++ .../WarehouseItem.Gateway.csproj | 17 +++++ WarehouseItem.Gateway/ocelot.json | 20 ++++++ ...ings.json => LaunchSettings.json.disabled} | 0 10 files changed, 194 insertions(+), 37 deletions(-) create mode 100644 WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs create mode 100644 WarehouseItem.Gateway/Program.cs create mode 100644 WarehouseItem.Gateway/WarehouseItem.Gateway.csproj create mode 100644 WarehouseItem.Gateway/ocelot.json rename WarehouseItem.Generator/Properties/{LaunchSettings.json => LaunchSettings.json.disabled} (100%) diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 5d188b50..9762d5e6 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,7 +4,7 @@ - Номер №1 "Кэширование" + Номер №2 "Балансировка нагрузки" Вариант №23 "Товар на складе" Выполнена Карповым Владимиром 6512 Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index 8a3e0c6c..a425e90a 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -1,10 +1,10 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "BaseAddress": "http://localhost:5204/api/warehouse-item" +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "BaseAddress": "http://localhost:5300/warehouse-item" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index 5eeb6bd3..e3853407 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -11,12 +11,18 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.Generator", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.AppHost", "WarehouseItem.AppHost\WarehouseItem.AppHost.csproj", "{07AFB6CB-7359-432D-BF0B-14BA7C582AA5}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.Gateway", "WarehouseItem.Gateway\WarehouseItem.Gateway.csproj", "{D0EFD087-0103-4ACE-AD32-F8A68EDE1988}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D0EFD087-0103-4ACE-AD32-F8A68EDE1988}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D0EFD087-0103-4ACE-AD32-F8A68EDE1988}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D0EFD087-0103-4ACE-AD32-F8A68EDE1988}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D0EFD087-0103-4ACE-AD32-F8A68EDE1988}.Release|Any CPU.Build.0 = Release|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -41,3 +47,5 @@ Global SolutionGuid = {90FE6B04-8381-437E-893A-FEBA1DA10AEE} EndGlobalSection EndGlobal + + diff --git a/WarehouseItem.AppHost/AppHost.cs b/WarehouseItem.AppHost/AppHost.cs index 77d7c835..c27ad0a0 100644 --- a/WarehouseItem.AppHost/AppHost.cs +++ b/WarehouseItem.AppHost/AppHost.cs @@ -1,13 +1,28 @@ -var builder = DistributedApplication.CreateBuilder(args); +var builder = DistributedApplication.CreateBuilder(args); var cache = builder.AddRedis("warehouse-item-cache") .WithRedisInsight(containerName: "warehouse-item-insight"); -var generator = builder.AddProject("generator") - .WithReference(cache, "warehouse-item-cache") - .WaitFor(cache); +var gateway = builder.AddProject("gateway") + .WithHttpEndpoint(name: "http", port: 5300) + .WithExternalHttpEndpoints(); + +const int generatorPortBase = 5200; +for (var i = 1; i <= 5; ++i) +{ + var generator = builder.AddProject($"generator-{i}") + .WithReference(cache, "warehouse-item-cache") + .WithHttpEndpoint(name: "http", port: generatorPortBase + i) + .WaitFor(cache); + + gateway.WaitFor(generator); +} builder.AddProject("client") - .WaitFor(generator); + .WithReference(gateway) + .WaitFor(gateway); builder.Build().Run(); + + + diff --git a/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj index 2550d0e2..5cd87dc4 100644 --- a/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj +++ b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj @@ -1,23 +1,19 @@ - - - - - Exe - net8.0 - enable - enable - ed7e1e47-dc98-4419-8424-85412466aa9b - - - - - - - - - - - - - + + + Exe + net8.0 + enable + enable + ed7e1e47-dc98-4419-8424-85412466aa9b + + + + + + + + + + + \ No newline at end of file diff --git a/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs new file mode 100644 index 00000000..876b3cc5 --- /dev/null +++ b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs @@ -0,0 +1,65 @@ +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.Values; + +namespace WarehouseItem.Gateway.LoadBalancer; + +/// +/// Query-based балансировщик нагрузки: выбирает downstream по query-параметру id. +/// Если параметр отсутствует/невалиден — выбирает случайный downstream. +/// +public sealed class QueryBasedLoadBalancer( + ILogger logger, + Func>> servicesProvider) : ILoadBalancer +{ + private const string IdParam = "id"; + + public string Type => nameof(QueryBasedLoadBalancer); + + public void Release(ServiceHostAndPort hostAndPort) { } + + public async Task> LeaseAsync(HttpContext httpContext) + { + var services = await servicesProvider(); + + var selectedIndex = ChooseIndex(httpContext, services.Count, out var parsedId); + + if (parsedId is null) + { + logger.LogWarning("Query param {param} missing/invalid; index={index} selected by random.", IdParam, + selectedIndex); + } + else + { + logger.LogInformation("Query-based selection: id={id} -> index={index}.", parsedId, selectedIndex); + } + + return new OkResponse(services[selectedIndex].HostAndPort); + } + + private static int ChooseIndex(HttpContext httpContext, int servicesCount, out int? id) + { + id = null; + + if (TryParseId(httpContext, out var parsed)) + { + id = parsed; + return parsed % servicesCount; + } + + return Random.Shared.Next(servicesCount); + } + + private static bool TryParseId(HttpContext httpContext, out int id) + { + id = 0; + + if (!httpContext.Request.Query.TryGetValue(IdParam, out var values) || values.Count == 0) + { + return false; + } + + var raw = values[0]; + return !string.IsNullOrWhiteSpace(raw) && int.TryParse(raw, out id) && id >= 0; + } +} diff --git a/WarehouseItem.Gateway/Program.cs b/WarehouseItem.Gateway/Program.cs new file mode 100644 index 00000000..ace626d3 --- /dev/null +++ b/WarehouseItem.Gateway/Program.cs @@ -0,0 +1,36 @@ +using Ocelot.DependencyInjection; +using Ocelot.Middleware; +using WarehouseItem.Gateway.LoadBalancer; +using WarehouseItem.ServiceDefaults; + +var builder = WebApplication.CreateBuilder(args); + +const string CorsPolicyName = "LocalDev"; + +builder.AddServiceDefaults(); + +builder.Services.AddCors(static options => +{ + options.AddPolicy(CorsPolicyName, static policy => + policy.AllowAnyOrigin() + .WithHeaders("Content-Type") + .WithMethods("GET")); +}); + +builder.Configuration + .AddJsonFile("ocelot.json", optional: false, reloadOnChange: true) + .AddOcelot(); + +builder.Services + .AddOcelot(builder.Configuration) + .AddCustomLoadBalancer((sp, _, discoveryProvider) => + new QueryBasedLoadBalancer( + sp.GetRequiredService>(), + discoveryProvider.GetAsync)); + +var app = builder.Build(); + +app.UseCors(CorsPolicyName); + +await app.UseOcelot(); +await app.RunAsync(); diff --git a/WarehouseItem.Gateway/WarehouseItem.Gateway.csproj b/WarehouseItem.Gateway/WarehouseItem.Gateway.csproj new file mode 100644 index 00000000..6d24591c --- /dev/null +++ b/WarehouseItem.Gateway/WarehouseItem.Gateway.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/WarehouseItem.Gateway/ocelot.json b/WarehouseItem.Gateway/ocelot.json new file mode 100644 index 00000000..41d0ba6b --- /dev/null +++ b/WarehouseItem.Gateway/ocelot.json @@ -0,0 +1,20 @@ +{ + "Routes": [ + { + "DownstreamPathTemplate": "/api/warehouse-item", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { "Host": "localhost", "Port": 5201 }, + { "Host": "localhost", "Port": 5202 }, + { "Host": "localhost", "Port": 5203 }, + { "Host": "localhost", "Port": 5204 }, + { "Host": "localhost", "Port": 5205 } + ], + "UpstreamPathTemplate": "/warehouse-item", + "UpstreamHttpMethod": [ "GET" ], + "LoadBalancerOptions": { + "Type": "QueryBasedLoadBalancer" + } + } + ] +} diff --git a/WarehouseItem.Generator/Properties/LaunchSettings.json b/WarehouseItem.Generator/Properties/LaunchSettings.json.disabled similarity index 100% rename from WarehouseItem.Generator/Properties/LaunchSettings.json rename to WarehouseItem.Generator/Properties/LaunchSettings.json.disabled From f68177762f1dc1d8ecea9d27b249186999d535be Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Wed, 22 Apr 2026 21:15:10 +0400 Subject: [PATCH 11/13] new changes --- Client.Wasm/wwwroot/appsettings.json | 16 ++++++++-------- WarehouseItem.AppHost/AppHost.cs | 5 +---- .../LoadBalancer/QueryBasedLoadBalancer.cs | 8 +++++++- WarehouseItem.Gateway/Program.cs | 2 +- WarehouseItem.Generator/Program.cs | 11 ----------- 5 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index a425e90a..165729ab 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -1,10 +1,10 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "BaseAddress": "http://localhost:5300/warehouse-item" + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "BaseAddress": "http://localhost:5300/warehouse-item" } diff --git a/WarehouseItem.AppHost/AppHost.cs b/WarehouseItem.AppHost/AppHost.cs index c27ad0a0..f7764f77 100644 --- a/WarehouseItem.AppHost/AppHost.cs +++ b/WarehouseItem.AppHost/AppHost.cs @@ -22,7 +22,4 @@ .WithReference(gateway) .WaitFor(gateway); -builder.Build().Run(); - - - +builder.Build().Run(); \ No newline at end of file diff --git a/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs index 876b3cc5..1ab67945 100644 --- a/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs +++ b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs @@ -1,4 +1,5 @@ -using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Errors; +using Ocelot.LoadBalancer.Interfaces; using Ocelot.Responses; using Ocelot.Values; @@ -22,6 +23,9 @@ public async Task> LeaseAsync(HttpContext httpConte { var services = await servicesProvider(); + if (services.Count == 0) + return new ErrorResponse(new NoServicesError()); + var selectedIndex = ChooseIndex(httpContext, services.Count, out var parsedId); if (parsedId is null) @@ -63,3 +67,5 @@ private static bool TryParseId(HttpContext httpContext, out int id) return !string.IsNullOrWhiteSpace(raw) && int.TryParse(raw, out id) && id >= 0; } } + +internal sealed class NoServicesError() : Error("No services available", OcelotErrorCode.UnableToFindDownstreamRouteError, 503); diff --git a/WarehouseItem.Gateway/Program.cs b/WarehouseItem.Gateway/Program.cs index ace626d3..f0904d80 100644 --- a/WarehouseItem.Gateway/Program.cs +++ b/WarehouseItem.Gateway/Program.cs @@ -12,7 +12,7 @@ builder.Services.AddCors(static options => { options.AddPolicy(CorsPolicyName, static policy => - policy.AllowAnyOrigin() + policy.WithOrigins("http://localhost:5127", "https://localhost:7282") .WithHeaders("Content-Type") .WithMethods("GET")); }); diff --git a/WarehouseItem.Generator/Program.cs b/WarehouseItem.Generator/Program.cs index 0d3c04ee..a1aa27dc 100644 --- a/WarehouseItem.Generator/Program.cs +++ b/WarehouseItem.Generator/Program.cs @@ -7,16 +7,6 @@ builder.AddServiceDefaults(); builder.AddRedisDistributedCache("warehouse-item-cache"); -builder.Services.AddCors(options => -{ - options.AddPolicy("AllowLocalDev", policy => - { - policy - .AllowAnyOrigin() - .WithHeaders("Content-Type") - .WithMethods("GET"); - }); -}); builder.Services.AddSingleton(); builder.Services.AddSingleton(); @@ -28,7 +18,6 @@ var app = builder.Build(); -app.UseCors("AllowLocalDev"); if (app.Environment.IsDevelopment()) { From 9cbfc4aa0a278621be0dc2f7014b7bc45ad5c0fb Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Wed, 22 Apr 2026 21:19:10 +0400 Subject: [PATCH 12/13] new changes --- WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs index 1ab67945..8a0098bf 100644 --- a/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs +++ b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs @@ -24,7 +24,9 @@ public async Task> LeaseAsync(HttpContext httpConte var services = await servicesProvider(); if (services.Count == 0) + { return new ErrorResponse(new NoServicesError()); + } var selectedIndex = ChooseIndex(httpContext, services.Count, out var parsedId); From cb75ac155bdababb22651cee4e349d0dc871bdfe Mon Sep 17 00:00:00 2001 From: Vouchiko Date: Sat, 9 May 2026 14:53:02 +0400 Subject: [PATCH 13/13] 3 lab --- .gitignore | 3 + CloudDevelopment.sln | 14 +- WarehouseItem.AppHost/AppHost.cs | 44 +++- .../warehouse-item-template-sns-s3.yaml | 59 +++++ .../WarehouseItem.AppHost.csproj | 8 +- .../appsettings.Development.json | 14 ++ .../Controller/S3StorageController.cs | 54 +++++ .../Controller/SnsSubscriberController.cs | 69 ++++++ WarehouseItem.FileService/Program.cs | 33 +++ .../Service/Messaging/ISubscriptionService.cs | 9 + .../Messaging/SnsSubscriptionService.cs | 42 ++++ .../Service/Storage/AwsFileService.cs | 131 +++++++++++ .../Service/Storage/IFileService.cs | 31 +++ .../WarehouseItem.FileService.csproj | 20 ++ .../appsettings.Development.json | 22 ++ WarehouseItem.FileService/appsettings.json | 9 + .../Controller/WarehouseItemController.cs | 4 +- WarehouseItem.Generator/Program.cs | 16 +- .../Service/Messaging/IPublisherService.cs | 14 ++ .../Service/Messaging/SnsPublisherService.cs | 45 ++++ .../Service/WarehouseItemService.cs | 11 +- .../WarehouseItem.Generator.csproj | 5 +- .../appsettings.Development.json | 14 ++ WarehouseItem.Tests/AppFixture.cs | 30 +++ WarehouseItem.Tests/IntegrationTests.cs | 217 ++++++++++++++++++ .../WarehouseItem.Tests.csproj | 42 ++++ 26 files changed, 943 insertions(+), 17 deletions(-) create mode 100644 WarehouseItem.AppHost/CloudFormation/warehouse-item-template-sns-s3.yaml create mode 100644 WarehouseItem.FileService/Controller/S3StorageController.cs create mode 100644 WarehouseItem.FileService/Controller/SnsSubscriberController.cs create mode 100644 WarehouseItem.FileService/Program.cs create mode 100644 WarehouseItem.FileService/Service/Messaging/ISubscriptionService.cs create mode 100644 WarehouseItem.FileService/Service/Messaging/SnsSubscriptionService.cs create mode 100644 WarehouseItem.FileService/Service/Storage/AwsFileService.cs create mode 100644 WarehouseItem.FileService/Service/Storage/IFileService.cs create mode 100644 WarehouseItem.FileService/WarehouseItem.FileService.csproj create mode 100644 WarehouseItem.FileService/appsettings.Development.json create mode 100644 WarehouseItem.FileService/appsettings.json create mode 100644 WarehouseItem.Generator/Service/Messaging/IPublisherService.cs create mode 100644 WarehouseItem.Generator/Service/Messaging/SnsPublisherService.cs create mode 100644 WarehouseItem.Tests/AppFixture.cs create mode 100644 WarehouseItem.Tests/IntegrationTests.cs create mode 100644 WarehouseItem.Tests/WarehouseItem.Tests.csproj diff --git a/.gitignore b/.gitignore index ce892922..e64ec814 100644 --- a/.gitignore +++ b/.gitignore @@ -396,6 +396,9 @@ MigrationBackup/ # Fody - auto-generated XML schema FodyWeavers.xsd +# Claude Code +.claude/ + # VS Code files for those working on multiple tools .vscode/* !.vscode/settings.json diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index e3853407..1cb72268 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -13,6 +13,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.AppHost", "Wa EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.Gateway", "WarehouseItem.Gateway\WarehouseItem.Gateway.csproj", "{D0EFD087-0103-4ACE-AD32-F8A68EDE1988}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.FileService", "WarehouseItem.FileService\WarehouseItem.FileService.csproj", "{681E8533-01D5-4D49-9D11-B00ED6618E1E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarehouseItem.Tests", "WarehouseItem.Tests\WarehouseItem.Tests.csproj", "{4C568669-19BD-45C5-A91C-75510D555889}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +43,14 @@ Global {07AFB6CB-7359-432D-BF0B-14BA7C582AA5}.Debug|Any CPU.Build.0 = Debug|Any CPU {07AFB6CB-7359-432D-BF0B-14BA7C582AA5}.Release|Any CPU.ActiveCfg = Release|Any CPU {07AFB6CB-7359-432D-BF0B-14BA7C582AA5}.Release|Any CPU.Build.0 = Release|Any CPU + {681E8533-01D5-4D49-9D11-B00ED6618E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {681E8533-01D5-4D49-9D11-B00ED6618E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {681E8533-01D5-4D49-9D11-B00ED6618E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {681E8533-01D5-4D49-9D11-B00ED6618E1E}.Release|Any CPU.Build.0 = Release|Any CPU + {4C568669-19BD-45C5-A91C-75510D555889}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C568669-19BD-45C5-A91C-75510D555889}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C568669-19BD-45C5-A91C-75510D555889}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C568669-19BD-45C5-A91C-75510D555889}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -47,5 +59,3 @@ Global SolutionGuid = {90FE6B04-8381-437E-893A-FEBA1DA10AEE} EndGlobalSection EndGlobal - - diff --git a/WarehouseItem.AppHost/AppHost.cs b/WarehouseItem.AppHost/AppHost.cs index f7764f77..f4a7ec41 100644 --- a/WarehouseItem.AppHost/AppHost.cs +++ b/WarehouseItem.AppHost/AppHost.cs @@ -1,19 +1,46 @@ -var builder = DistributedApplication.CreateBuilder(args); +using Amazon; +using Aspire.Hosting.LocalStack.Container; + +var builder = DistributedApplication.CreateBuilder(args); var cache = builder.AddRedis("warehouse-item-cache") + .WithImageTag("7.4-alpine") .WithRedisInsight(containerName: "warehouse-item-insight"); var gateway = builder.AddProject("gateway") .WithHttpEndpoint(name: "http", port: 5300) .WithExternalHttpEndpoints(); +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +var localstack = builder + .AddLocalStack("warehouse-item-localstack", awsConfig: awsConfig, configureContainer: container => + { + container.Lifetime = ContainerLifetime.Session; + container.DebugLevel = 1; + container.LogLevel = LocalStackLogLevel.Debug; + container.Port = 4566; + container.AdditionalEnvironmentVariables + .Add("DEBUG", "1"); + container.AdditionalEnvironmentVariables + .Add("SNS_CERT_URL_HOST", "sns.eu-central-1.amazonaws.com"); + }); + +var awsResources = builder + .AddAWSCloudFormationTemplate("files", "CloudFormation/warehouse-item-template-sns-s3.yaml", "warehouse-item") + .WithReference(awsConfig); + const int generatorPortBase = 5200; for (var i = 1; i <= 5; ++i) { var generator = builder.AddProject($"generator-{i}") .WithReference(cache, "warehouse-item-cache") .WithHttpEndpoint(name: "http", port: generatorPortBase + i) - .WaitFor(cache); + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SNS") + .WaitFor(awsResources); gateway.WaitFor(generator); } @@ -22,4 +49,15 @@ .WithReference(gateway) .WaitFor(gateway); -builder.Build().Run(); \ No newline at end of file +var fileService = builder.AddProject("warehouse-item-file-service") + .WithHttpEndpoint(name: "http", port: 5280) + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SNS") + .WithEnvironment("Settings__S3Hosting", "Localstack") + .WaitFor(awsResources); +fileService.WithEnvironment("AWS__Resources__SNSUrl", "http://host.docker.internal:5280/api/sns"); +fileService.WithEnvironment("AWS__Resources__SNSTopicArn", "arn:aws:sns:eu-central-1:000000000000:warehouse-item-topic"); + +builder.UseLocalStack(localstack); + +builder.Build().Run(); diff --git a/WarehouseItem.AppHost/CloudFormation/warehouse-item-template-sns-s3.yaml b/WarehouseItem.AppHost/CloudFormation/warehouse-item-template-sns-s3.yaml new file mode 100644 index 00000000..3a2e1979 --- /dev/null +++ b/WarehouseItem.AppHost/CloudFormation/warehouse-item-template-sns-s3.yaml @@ -0,0 +1,59 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: 'Cloud formation template for warehouse item project' + +Parameters: + BucketName: + Type: String + Description: Name for the S3 bucket + Default: 'warehouse-item-bucket' + + TopicName: + Type: String + Description: Name for the SNS topic + Default: 'warehouse-item-topic' + +Resources: + WarehouseItemBucket: + Type: AWS::S3::Bucket + Properties: + BucketName: !Ref BucketName + VersioningConfiguration: + Status: Suspended + Tags: + - Key: Name + Value: !Ref BucketName + - Key: Environment + Value: Sample + PublicAccessBlockConfiguration: + BlockPublicAcls: true + BlockPublicPolicy: true + IgnorePublicAcls: true + RestrictPublicBuckets: true + + WarehouseItemTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: !Ref TopicName + DisplayName: !Ref TopicName + Tags: + - Key: Name + Value: !Ref TopicName + - Key: Environment + Value: Sample + +Outputs: + S3BucketName: + Description: Name of the S3 bucket + Value: !Ref WarehouseItemBucket + + S3BucketArn: + Description: ARN of the S3 bucket + Value: !GetAtt WarehouseItemBucket.Arn + + SNSTopicName: + Description: Name of the SNS topic + Value: !GetAtt WarehouseItemTopic.TopicName + + SNSTopicArn: + Description: ARN of the SNS topic + Value: !Ref WarehouseItemTopic diff --git a/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj index 5cd87dc4..0559426c 100644 --- a/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj +++ b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj @@ -1,5 +1,5 @@ - + Exe net8.0 @@ -8,12 +8,14 @@ ed7e1e47-dc98-4419-8424-85412466aa9b - - + + + + \ No newline at end of file diff --git a/WarehouseItem.AppHost/appsettings.Development.json b/WarehouseItem.AppHost/appsettings.Development.json index 1b2d3baf..e496c093 100644 --- a/WarehouseItem.AppHost/appsettings.Development.json +++ b/WarehouseItem.AppHost/appsettings.Development.json @@ -4,5 +4,19 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "LocalStack": { + "UseLocalStack": true, + "Session": { + "AwsAccessKeyId": "test", + "AwsSecretAccessKey": "test", + "RegionName": "eu-central-1" + }, + "Config": { + "LocalStackHost": "localhost", + "UseSsl": false, + "UseLegacyPorts": false, + "EdgePort": 4566 + } } } \ No newline at end of file diff --git a/WarehouseItem.FileService/Controller/S3StorageController.cs b/WarehouseItem.FileService/Controller/S3StorageController.cs new file mode 100644 index 00000000..0bf71083 --- /dev/null +++ b/WarehouseItem.FileService/Controller/S3StorageController.cs @@ -0,0 +1,54 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text; +using System.Text.Json.Nodes; +using WarehouseItem.FileService.Service.Storage; + +namespace WarehouseItem.FileService.Controller; + +[ApiController] +[Route("api/s3")] +public class S3StorageController(IFileService fileService, ILogger logger) : ControllerBase +{ + [HttpGet] + [ProducesResponseType(200)] + [ProducesResponseType(500)] + public async Task>> ListFiles() + { + logger.LogInformation("Method {method} of {controller} was called.", nameof(ListFiles), + nameof(S3StorageController)); + try + { + var list = await fileService.GetFilesList(); + + logger.LogInformation("Got a list of {count} files from bucket.", list.Count); + return Ok(list); + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occured during {method} of {controller}.", nameof(ListFiles), + nameof(S3StorageController)); + return StatusCode(500, new { message = ex.Message }); + } + } + + [HttpGet("{key}")] + [ProducesResponseType(200)] + [ProducesResponseType(500)] + public async Task> GetFile(string key) + { + logger.LogInformation("Method {method} of {controller} was called.", nameof(GetFile), + nameof(S3StorageController)); + try + { + var node = await fileService.DownloadFile(key); + logger.LogInformation("Received json of {size} bytes.", Encoding.UTF8.GetByteCount(node.ToJsonString())); + return Ok(node); + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occured during {method} of {controller}.", nameof(GetFile), + nameof(S3StorageController)); + return StatusCode(500, new { message = ex.Message }); + } + } +} diff --git a/WarehouseItem.FileService/Controller/SnsSubscriberController.cs b/WarehouseItem.FileService/Controller/SnsSubscriberController.cs new file mode 100644 index 00000000..a83f0848 --- /dev/null +++ b/WarehouseItem.FileService/Controller/SnsSubscriberController.cs @@ -0,0 +1,69 @@ +using Amazon.SimpleNotificationService.Util; +using Microsoft.AspNetCore.Mvc; +using System.Text; +using WarehouseItem.FileService.Service.Storage; + +namespace WarehouseItem.FileService.Controller; + +[ApiController] +[Route("api/sns")] +public class SnsSubscriberController(IFileService fileService, ILogger logger) : ControllerBase +{ + /// + /// Вебхук для приема оповещений от SNS топика. + /// Используется также для подтверждения подписки при инициализации. + /// Всегда возвращает 200. + /// + [HttpPost] + [ProducesResponseType(200)] + public async Task ReceiveMessage() + { + logger.LogInformation("SNS webhook was called."); + try + { + using var reader = new StreamReader(Request.Body, Encoding.UTF8); + var jsonContent = await reader.ReadToEndAsync(); + + var snsMessage = Message.ParseMessage(jsonContent); + + switch (snsMessage.Type) + { + case "SubscriptionConfirmation": + { + logger.LogInformation("SubscriptionConfirmation was received."); + + using var httpClient = new HttpClient(); + var builder = new UriBuilder(new Uri(snsMessage.SubscribeURL)) + { + Scheme = "http", + Host = "localhost", + Port = 4566 + }; + var response = await httpClient.GetAsync(builder.Uri); + + if (!response.IsSuccessStatusCode) + { + var body = await response.Content.ReadAsStringAsync(); + throw new Exception($"SubscriptionConfirmation returned {response.StatusCode}: {body}."); + } + + logger.LogInformation("Subscription was successfully confirmed."); + break; + } + case "Notification": + { + await fileService.UploadFile(snsMessage.MessageText); + + logger.LogInformation("Notification was successfully processed."); + break; + } + } + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occurred while processing SNS notifications."); + } + + return Ok(); + } +} diff --git a/WarehouseItem.FileService/Program.cs b/WarehouseItem.FileService/Program.cs new file mode 100644 index 00000000..8bb5bb5b --- /dev/null +++ b/WarehouseItem.FileService/Program.cs @@ -0,0 +1,33 @@ +using Amazon.S3; +using Amazon.SimpleNotificationService; +using LocalStack.Client.Extensions; +using WarehouseItem.FileService.Service.Messaging; +using WarehouseItem.FileService.Service.Storage; +using WarehouseItem.ServiceDefaults; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +builder.Services.AddControllers(); + +builder.Services.AddLocalStack(builder.Configuration); + +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddAwsService(); +builder.Services.AddAwsService(); + +var app = builder.Build(); + +var scope = app.Services.CreateScope(); +var subscriptionService = scope.ServiceProvider.GetRequiredService(); +await subscriptionService.SubscribeEndpoint(); + +scope = app.Services.CreateScope(); +var s3Service = scope.ServiceProvider.GetRequiredService(); +await s3Service.EnsureBucketExists(); + +app.MapControllers(); +app.Run(); diff --git a/WarehouseItem.FileService/Service/Messaging/ISubscriptionService.cs b/WarehouseItem.FileService/Service/Messaging/ISubscriptionService.cs new file mode 100644 index 00000000..a9c78284 --- /dev/null +++ b/WarehouseItem.FileService/Service/Messaging/ISubscriptionService.cs @@ -0,0 +1,9 @@ +namespace WarehouseItem.FileService.Service.Messaging; + +public interface ISubscriptionService +{ + /// + /// Подписывается на SNS топик при старте приложения. + /// + Task SubscribeEndpoint(); +} diff --git a/WarehouseItem.FileService/Service/Messaging/SnsSubscriptionService.cs b/WarehouseItem.FileService/Service/Messaging/SnsSubscriptionService.cs new file mode 100644 index 00000000..438ea16e --- /dev/null +++ b/WarehouseItem.FileService/Service/Messaging/SnsSubscriptionService.cs @@ -0,0 +1,42 @@ +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; +using System.Net; + +namespace WarehouseItem.FileService.Service.Messaging; + +/// +/// Служба для подписки FileService на SNS топик при старте приложения. +/// +public class SnsSubscriptionService( + IAmazonSimpleNotificationService snsClient, + IConfiguration configuration, + ILogger logger) : ISubscriptionService +{ + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] ?? + throw new KeyNotFoundException("SNS topic ARN was not found in configuration"); + + public async Task SubscribeEndpoint() + { + logger.LogInformation("Sending subscribe request for topic {topic}", _topicArn); + var endpoint = configuration["AWS:Resources:SNSUrl"]; + + var request = new SubscribeRequest + { + TopicArn = _topicArn, + Protocol = "http", + Endpoint = endpoint, + ReturnSubscriptionArn = true + }; + var response = await snsClient.SubscribeAsync(request); + + if (response.HttpStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to subscribe to topic {topic}", _topicArn); + } + else + { + logger.LogInformation("Subscription request for topic {topic} succeeded, waiting for confirmation", + _topicArn); + } + } +} diff --git a/WarehouseItem.FileService/Service/Storage/AwsFileService.cs b/WarehouseItem.FileService/Service/Storage/AwsFileService.cs new file mode 100644 index 00000000..1738a83e --- /dev/null +++ b/WarehouseItem.FileService/Service/Storage/AwsFileService.cs @@ -0,0 +1,131 @@ +using Amazon.S3; +using Amazon.S3.Model; +using System.Net; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace WarehouseItem.FileService.Service.Storage; + +/// +/// Реализация IFileService для работы с Amazon S3. +/// +public class AwsFileService(IAmazonS3 client, IConfiguration configuration, ILogger logger) + : IFileService +{ + private readonly string _bucketName = configuration["AWS:Resources:S3BucketName"] + ?? throw new KeyNotFoundException("S3 bucket name was not found in configuration"); + + public async Task> GetFilesList() + { + var list = new List(); + + var request = new ListObjectsV2Request + { + BucketName = _bucketName, + Prefix = "", + Delimiter = "," + }; + var paginator = client.Paginators.ListObjectsV2(request); + + logger.LogInformation("Listing files in bucket {bucket}.", _bucketName); + await foreach (var response in paginator.Responses) + { + if (response?.S3Objects == null) + { + logger.LogWarning("Received null response from bucket {bucket}.", _bucketName); + continue; + } + + foreach (var obj in response.S3Objects) + { + if (obj == null) + { + logger.LogWarning("Received null object from bucket {bucket}.", _bucketName); + continue; + } + + list.Add(obj.Key); + } + } + + return list; + } + + public async Task UploadFile(string fileData) + { + logger.LogInformation("Uploading file data to S3."); + + var rootNode = JsonNode.Parse(fileData) ?? throw new ArgumentException("Passed string is not a valid JSON"); + var id = rootNode["Id"]?.GetValue() ?? throw new ArgumentException("Passed JSON has invalid structure"); + + using var stream = new MemoryStream(); + await JsonSerializer.SerializeAsync(stream, rootNode); + stream.Seek(0, SeekOrigin.Begin); + + logger.LogInformation("Uploading warehouse item id={Id} to bucket {bucket}.", id, _bucketName); + var request = new PutObjectRequest + { + BucketName = _bucketName, + Key = $"warehouse_item_{id}.json", + InputStream = stream + }; + + var response = await client.PutObjectAsync(request); + + if (response.HttpStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to upload warehouse item {id}: {code}.", id, response.HttpStatusCode); + return false; + } + + logger.LogInformation("Uploaded warehouse item {id} to {bucket}.", id, _bucketName); + return true; + } + + public async Task DownloadFile(string key) + { + logger.LogInformation("Downloading file {file} from bucket {bucket}.", key, _bucketName); + + try + { + var request = new GetObjectRequest + { + BucketName = _bucketName, + Key = key + }; + using var response = await client.GetObjectAsync(request); + + if (response.HttpStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to download file {file}, code: {code}.", key, response.HttpStatusCode); + throw new InvalidOperationException( + $"Error downloading file {key} - {response.HttpStatusCode}."); + } + + using var reader = new StreamReader(response.ResponseStream, Encoding.UTF8); + return JsonNode.Parse(await reader.ReadToEndAsync()) ?? + throw new InvalidOperationException($"Downloaded document is not a valid JSON."); + } + catch (Exception ex) + { + logger.LogError(ex, "Exception during downloading file {file}.", key); + throw; + } + } + + public async Task EnsureBucketExists() + { + logger.LogInformation("Checking whether bucket {bucket} exists.", _bucketName); + try + { + await client.EnsureBucketExistsAsync(_bucketName); + logger.LogInformation("Bucket {bucket} existence ensured.", _bucketName); + } + catch (Exception ex) + { + logger.LogError(ex, "Exception during checking bucket {bucket}.", _bucketName); + throw; + } + } +} diff --git a/WarehouseItem.FileService/Service/Storage/IFileService.cs b/WarehouseItem.FileService/Service/Storage/IFileService.cs new file mode 100644 index 00000000..458f35fb --- /dev/null +++ b/WarehouseItem.FileService/Service/Storage/IFileService.cs @@ -0,0 +1,31 @@ +using System.Text.Json.Nodes; + +namespace WarehouseItem.FileService.Service.Storage; + +/// +/// Интерфейс службы для работы с файлами в объектном хранилище S3. +/// +public interface IFileService +{ + /// + /// Сохраняет файл в хранилище. + /// + /// JSON-строка сохраняемого объекта + Task UploadFile(string fileData); + + /// + /// Возвращает список всех файлов из хранилища. + /// + Task> GetFilesList(); + + /// + /// Скачивает файл из хранилища и возвращает его как JsonNode. + /// + /// Ключ файла в бакете + Task DownloadFile(string key); + + /// + /// Создаёт S3 бакет при необходимости. + /// + Task EnsureBucketExists(); +} diff --git a/WarehouseItem.FileService/WarehouseItem.FileService.csproj b/WarehouseItem.FileService/WarehouseItem.FileService.csproj new file mode 100644 index 00000000..36091c3d --- /dev/null +++ b/WarehouseItem.FileService/WarehouseItem.FileService.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + WarehouseItem.FileService + + + + + + + + + + + + + diff --git a/WarehouseItem.FileService/appsettings.Development.json b/WarehouseItem.FileService/appsettings.Development.json new file mode 100644 index 00000000..24a0b147 --- /dev/null +++ b/WarehouseItem.FileService/appsettings.Development.json @@ -0,0 +1,22 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "LocalStack": { + "UseLocalStack": true, + "Session": { + "AwsAccessKeyId": "test", + "AwsSecretAccessKey": "test", + "RegionName": "eu-central-1" + }, + "Config": { + "LocalStackHost": "localhost", + "UseSsl": false, + "UseLegacyPorts": false, + "EdgePort": 4566 + } + } +} diff --git a/WarehouseItem.FileService/appsettings.json b/WarehouseItem.FileService/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/WarehouseItem.FileService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/WarehouseItem.Generator/Controller/WarehouseItemController.cs b/WarehouseItem.Generator/Controller/WarehouseItemController.cs index f3cae97b..c5d4b4a0 100644 --- a/WarehouseItem.Generator/Controller/WarehouseItemController.cs +++ b/WarehouseItem.Generator/Controller/WarehouseItemController.cs @@ -22,9 +22,9 @@ public sealed class WarehouseItemController(ILogger log [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> Get([FromQuery] int id, CancellationToken cancellationToken) { - if (id < 0) + if (id <= 0) { - return BadRequest(new { message = "id cannot be negative" }); + return BadRequest(new { message = "id must be greater than 0" }); } logger.LogInformation("Request warehouse item id={id}.", id); diff --git a/WarehouseItem.Generator/Program.cs b/WarehouseItem.Generator/Program.cs index a1aa27dc..92f773c8 100644 --- a/WarehouseItem.Generator/Program.cs +++ b/WarehouseItem.Generator/Program.cs @@ -1,15 +1,29 @@ +using Amazon.SimpleNotificationService; +using LocalStack.Client.Extensions; using WarehouseItem.Generator.Generator; using WarehouseItem.Generator.Service; +using WarehouseItem.Generator.Service.Messaging; using WarehouseItem.ServiceDefaults; var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); -builder.AddRedisDistributedCache("warehouse-item-cache"); +builder.AddRedisDistributedCache("warehouse-item-cache", configureOptions: options => +{ + options.AbortOnConnectFail = false; + options.SslClientAuthenticationOptions = host => new System.Net.Security.SslClientAuthenticationOptions + { + TargetHost = host, + RemoteCertificateValidationCallback = (_, _, _, _) => true // dev: доверяем self-signed сертификату Aspire + }; +}); +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddControllers(); diff --git a/WarehouseItem.Generator/Service/Messaging/IPublisherService.cs b/WarehouseItem.Generator/Service/Messaging/IPublisherService.cs new file mode 100644 index 00000000..1a347243 --- /dev/null +++ b/WarehouseItem.Generator/Service/Messaging/IPublisherService.cs @@ -0,0 +1,14 @@ +using WarehouseItem.Generator.DTO; + +namespace WarehouseItem.Generator.Service.Messaging; + +/// +/// Интерфейс службы для отправки сгенерированных объектов в брокер сообщений. +/// +public interface IPublisherService +{ + /// + /// Отправляет объект товара в брокер. + /// + Task SendMessage(WarehouseItemDto warehouseItem); +} diff --git a/WarehouseItem.Generator/Service/Messaging/SnsPublisherService.cs b/WarehouseItem.Generator/Service/Messaging/SnsPublisherService.cs new file mode 100644 index 00000000..bc91c5ee --- /dev/null +++ b/WarehouseItem.Generator/Service/Messaging/SnsPublisherService.cs @@ -0,0 +1,45 @@ +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; +using System.Net; +using System.Text.Json; +using WarehouseItem.Generator.DTO; + +namespace WarehouseItem.Generator.Service.Messaging; + +/// +/// Служба публикации товаров в SNS топик. +/// +public class SnsPublisherService( + IAmazonSimpleNotificationService client, + IConfiguration configuration, + ILogger logger) : IPublisherService +{ + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] + ?? throw new KeyNotFoundException( + "SNS topic ARN was not found in configuration"); + + public async Task SendMessage(WarehouseItemDto warehouseItem) + { + try + { + var json = JsonSerializer.Serialize(warehouseItem); + var request = new PublishRequest + { + Message = json, + TopicArn = _topicArn + }; + + var statusCode = (await client.PublishAsync(request)).HttpStatusCode; + if (statusCode != HttpStatusCode.OK) + { + throw new Exception($"SNS returned status code {statusCode}"); + } + + logger.LogInformation("Warehouse item id={Id} sent to file service via SNS", warehouseItem.Id); + } + catch (Exception ex) + { + logger.LogError(ex, "Unable to send warehouse item through SNS topic"); + } + } +} diff --git a/WarehouseItem.Generator/Service/WarehouseItemService.cs b/WarehouseItem.Generator/Service/WarehouseItemService.cs index 23dcf713..9e67dd28 100644 --- a/WarehouseItem.Generator/Service/WarehouseItemService.cs +++ b/WarehouseItem.Generator/Service/WarehouseItemService.cs @@ -1,5 +1,6 @@ using WarehouseItem.Generator.DTO; using WarehouseItem.Generator.Generator; +using WarehouseItem.Generator.Service.Messaging; namespace WarehouseItem.Generator.Service; @@ -8,14 +9,13 @@ namespace WarehouseItem.Generator.Service; /// public sealed class WarehouseItemService( WarehouseItemGenerator generator, - IWarehouseItemCache cache) : IWarehouseItemService + IWarehouseItemCache cache, + IPublisherService publisherService) : IWarehouseItemService { /// - /// Получить товар по идентификатору. Если товар не найден в кэше, генерирует новый и сохраняет в кэш. + /// Получить товар по идентификатору. Если товар не найден в кэше, генерирует новый, + /// сохраняет в кэш и отправляет в SNS для записи в S3. /// - /// Идентификатор товара. - /// Токен отмены. - /// DTO товара. public async Task GetAsync(int id, CancellationToken cancellationToken = default) { var cached = await cache.GetAsync(id, cancellationToken); @@ -26,6 +26,7 @@ public async Task GetAsync(int id, CancellationToken cancellat var generated = generator.Generate(id); await cache.SetAsync(id, generated, cancellationToken); + await publisherService.SendMessage(generated); return generated; } diff --git a/WarehouseItem.Generator/WarehouseItem.Generator.csproj b/WarehouseItem.Generator/WarehouseItem.Generator.csproj index d345185d..043859f5 100644 --- a/WarehouseItem.Generator/WarehouseItem.Generator.csproj +++ b/WarehouseItem.Generator/WarehouseItem.Generator.csproj @@ -9,8 +9,11 @@ - + + + + diff --git a/WarehouseItem.Generator/appsettings.Development.json b/WarehouseItem.Generator/appsettings.Development.json index 0c208ae9..24a0b147 100644 --- a/WarehouseItem.Generator/appsettings.Development.json +++ b/WarehouseItem.Generator/appsettings.Development.json @@ -4,5 +4,19 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "LocalStack": { + "UseLocalStack": true, + "Session": { + "AwsAccessKeyId": "test", + "AwsSecretAccessKey": "test", + "RegionName": "eu-central-1" + }, + "Config": { + "LocalStackHost": "localhost", + "UseSsl": false, + "UseLegacyPorts": false, + "EdgePort": 4566 + } } } diff --git a/WarehouseItem.Tests/AppFixture.cs b/WarehouseItem.Tests/AppFixture.cs new file mode 100644 index 00000000..1ad2ade3 --- /dev/null +++ b/WarehouseItem.Tests/AppFixture.cs @@ -0,0 +1,30 @@ +using Aspire.Hosting; + +namespace WarehouseItem.Tests; + +/// +/// Фикстура для интеграционных тестов — поднимает полный стек Aspire один раз для всех тестов класса. +/// +public class AppFixture : IAsyncLifetime +{ + public DistributedApplication App { get; private set; } = null!; + + private IDistributedApplicationTestingBuilder? _builder; + + public async Task InitializeAsync() + { + _builder = await DistributedApplicationTestingBuilder + .CreateAsync(); + _builder.Configuration["DcpPublisher:RandomizePorts"] = "false"; + + App = await _builder.BuildAsync(); + await App.StartAsync(); + } + + public async Task DisposeAsync() + { + await App.StopAsync(); + await App.DisposeAsync(); + await _builder!.DisposeAsync(); + } +} diff --git a/WarehouseItem.Tests/IntegrationTests.cs b/WarehouseItem.Tests/IntegrationTests.cs new file mode 100644 index 00000000..deb4470d --- /dev/null +++ b/WarehouseItem.Tests/IntegrationTests.cs @@ -0,0 +1,217 @@ +using Aspire.Hosting; +using System.Text.Json; +using WarehouseItem.Generator.DTO; + +namespace WarehouseItem.Tests; + +/// +/// Интеграционные тесты для проверки микросервисного пайплайна. +/// +/// Фикстура для запуска Aspire один раз для всех тестов класса. +public class IntegrationTests(AppFixture fixture) : IClassFixture +{ + private static readonly JsonSerializerOptions _jsonOptions = new() + { + PropertyNameCaseInsensitive = true + }; + + private static T? Deserialize(string json) => JsonSerializer.Deserialize(json, _jsonOptions); + + private readonly DistributedApplication _app = fixture.App; + + /// + /// Проверяет основной положительный сценарий: + /// запрос через Gateway → генерация → сохранение в S3 → данные идентичны. + /// + [Theory] + [InlineData(11)] + [InlineData(12)] + [InlineData(13)] + public async Task GetFromGateway_SavesToS3_AndDataIsIdentical(int id) + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + var fileClient = _app.CreateHttpClient("warehouse-item-file-service", "http"); + + var response = await gatewayClient.GetAsync($"/warehouse-item?id={id}"); + response.EnsureSuccessStatusCode(); + + var apiItem = Deserialize(await response.Content.ReadAsStringAsync()); + + await WaitForFileInS3Async(fileClient, id, TimeSpan.FromSeconds(10)); + + var s3Item = await GetItemFromS3Async(fileClient, id); + + Assert.NotNull(apiItem); + Assert.Equal(id, apiItem.Id); + Assert.NotNull(s3Item); + Assert.Equal(id, s3Item.Id); + Assert.Equivalent(apiItem, s3Item, strict: true); + } + + /// + /// Проверяет работу кэширования: + /// при повторных запросах одного id возвращается идентичный объект, + /// и S3 также возвращает тот же объект. + /// + [Fact] + public async Task SameIds_GivingSameObjects() + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + var fileClient = _app.CreateHttpClient("warehouse-item-file-service", "http"); + + var id = Random.Shared.Next(20, 30); + + var response = await gatewayClient.GetAsync($"/warehouse-item?id={id}"); + response.EnsureSuccessStatusCode(); + + var apiItem = Deserialize(await response.Content.ReadAsStringAsync()); + + await WaitForFileInS3Async(fileClient, id, TimeSpan.FromSeconds(10)); + + var s3Item = await GetItemFromS3Async(fileClient, id); + + var response1 = await gatewayClient.GetAsync($"/warehouse-item?id={id}"); + response1.EnsureSuccessStatusCode(); + + var apiItem1 = Deserialize(await response1.Content.ReadAsStringAsync()); + + await WaitForFileInS3Async(fileClient, id, TimeSpan.FromSeconds(10)); + + var s3Item1 = await GetItemFromS3Async(fileClient, id); + + Assert.NotNull(apiItem); + Assert.Equal(id, apiItem.Id); + Assert.NotNull(s3Item); + Assert.Equal(id, s3Item.Id); + Assert.Equivalent(apiItem, s3Item, strict: true); + + Assert.NotNull(apiItem1); + Assert.Equal(id, apiItem1.Id); + Assert.NotNull(s3Item1); + Assert.Equal(id, s3Item1.Id); + Assert.Equivalent(apiItem1, s3Item1, strict: true); + + Assert.Equivalent(apiItem, apiItem1, strict: true); + Assert.Equivalent(s3Item, s3Item1, strict: true); + } + + /// + /// Проверяет эндпоинт получения списка файлов в S3 и уникальность по Id. + /// Намеренно используется повторяющийся id (1001), чтобы убедиться, + /// что для одного идентификатора создаётся только один файл. + /// + [Fact] + public async Task S3_ReturnsCorrectListOfFiles() + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + var fileClient = _app.CreateHttpClient("warehouse-item-file-service", "http"); + + var ids = new[] { 1001, 1002, 1003, 1001 }; + + foreach (var id in ids) + { + await gatewayClient.GetAsync($"/warehouse-item?id={id}"); + await WaitForFileInS3Async(fileClient, id, TimeSpan.FromSeconds(10)); + } + + var listResponse = await fileClient.GetAsync("/api/s3"); + listResponse.EnsureSuccessStatusCode(); + + var fileList = Deserialize>(await listResponse.Content.ReadAsStringAsync()); + + Assert.NotNull(fileList); + + foreach (var id in ids) + { + Assert.Contains($"warehouse_item_{id}.json", fileList); + } + + var addedFiles = fileList.Where(f => f.StartsWith("warehouse_item_100")).ToList(); + Assert.Equal(3, addedFiles.Count); + } + + /// + /// Проверяет обработку некорректных значений id в Gateway. + /// + [Theory] + [InlineData("0")] + [InlineData("-1")] + [InlineData("asd")] + public async Task InvalidId_ReturnsBadRequest(string invalidId) + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + + var response = await gatewayClient.GetAsync($"/warehouse-item?id={invalidId}"); + + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + } + + /// + /// Проверяет поведение при отсутствии параметра id. + /// + [Fact] + public async Task MissingIdParameter_ReturnsBadRequest() + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + + var response = await gatewayClient.GetAsync("/warehouse-item"); + + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + } + + /// + /// Проверяет, что при некорректном id файл в S3 НЕ создаётся. + /// + [Theory] + [InlineData("0")] + [InlineData("-1")] + [InlineData("asd")] + public async Task InvalidId_DoesNotCreateFileInS3(string invalidId) + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + var fileClient = _app.CreateHttpClient("warehouse-item-file-service", "http"); + + var response = await gatewayClient.GetAsync($"/warehouse-item?id={invalidId}"); + + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + + await Task.Delay(5000); + + var fileName = $"warehouse_item_{invalidId}.json"; + var probe = await fileClient.GetAsync($"/api/s3/{fileName}"); + + Assert.False(probe.IsSuccessStatusCode, "Файл не должен был появиться в S3 при невалидном id"); + } + + /// + /// Ожидает появления файла в S3 с заданным id (поллинг с таймаутом). + /// Необходим из-за асинхронной природы SNS → FileService → S3. + /// + private static async Task WaitForFileInS3Async(HttpClient fileClient, int id, TimeSpan timeout) + { + var fileName = $"warehouse_item_{id}.json"; + var deadline = DateTime.UtcNow + timeout; + + while (DateTime.UtcNow < deadline) + { + var probe = await fileClient.GetAsync($"/api/s3/{fileName}"); + if (probe.IsSuccessStatusCode) + { + return; + } + + await Task.Delay(500); + } + + throw new TimeoutException($"File {fileName} did not appear in S3 within {timeout.TotalSeconds}s"); + } + + /// + /// Скачивает и десериализует WarehouseItemDto из S3 через FileService. + /// + private static async Task GetItemFromS3Async(HttpClient fileClient, int id) + { + var fileServiceResponse = await fileClient.GetAsync($"/api/s3/warehouse_item_{id}.json"); + return Deserialize(await fileServiceResponse.Content.ReadAsStringAsync())!; + } +} diff --git a/WarehouseItem.Tests/WarehouseItem.Tests.csproj b/WarehouseItem.Tests/WarehouseItem.Tests.csproj new file mode 100644 index 00000000..e04a3cae --- /dev/null +++ b/WarehouseItem.Tests/WarehouseItem.Tests.csproj @@ -0,0 +1,42 @@ + + + + net8.0 + enable + enable + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PreserveNewest + CloudFormation\warehouse-item-template-sns-s3.yaml + + + +