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/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..9762d5e6 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №2 "Балансировка нагрузки" + Вариант №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..165729ab 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -1,4 +1,4 @@ -{ +{ "Logging": { "LogLevel": { "Default": "Information", @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "http://localhost:5300/warehouse-item" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..1cb72268 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -5,16 +5,52 @@ 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 +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 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 {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 + {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 diff --git a/WarehouseItem.AppHost/AppHost.cs b/WarehouseItem.AppHost/AppHost.cs new file mode 100644 index 00000000..f4a7ec41 --- /dev/null +++ b/WarehouseItem.AppHost/AppHost.cs @@ -0,0 +1,63 @@ +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) + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SNS") + .WaitFor(awsResources); + + gateway.WaitFor(generator); +} + +builder.AddProject("client") + .WithReference(gateway) + .WaitFor(gateway); + +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/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..0559426c --- /dev/null +++ b/WarehouseItem.AppHost/WarehouseItem.AppHost.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + enable + enable + 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 new file mode 100644 index 00000000..e496c093 --- /dev/null +++ b/WarehouseItem.AppHost/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 + } + } +} \ 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.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.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs new file mode 100644 index 00000000..8a0098bf --- /dev/null +++ b/WarehouseItem.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs @@ -0,0 +1,73 @@ +using Ocelot.Errors; +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(); + + if (services.Count == 0) + { + return new ErrorResponse(new NoServicesError()); + } + + 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; + } +} + +internal sealed class NoServicesError() : Error("No services available", OcelotErrorCode.UnableToFindDownstreamRouteError, 503); diff --git a/WarehouseItem.Gateway/Program.cs b/WarehouseItem.Gateway/Program.cs new file mode 100644 index 00000000..f0904d80 --- /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.WithOrigins("http://localhost:5127", "https://localhost:7282") + .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/Controller/WarehouseItemController.cs b/WarehouseItem.Generator/Controller/WarehouseItemController.cs new file mode 100644 index 00000000..c5d4b4a0 --- /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 must be greater than 0" }); + } + + 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..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 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..92f773c8 --- /dev/null +++ b/WarehouseItem.Generator/Program.cs @@ -0,0 +1,45 @@ +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", 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(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapControllers(); +app.MapDefaultEndpoints(); + +app.Run(); diff --git a/WarehouseItem.Generator/Properties/LaunchSettings.json.disabled b/WarehouseItem.Generator/Properties/LaunchSettings.json.disabled new file mode 100644 index 00000000..cbeaf732 --- /dev/null +++ b/WarehouseItem.Generator/Properties/LaunchSettings.json.disabled @@ -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/IWarehouseItemCache.cs b/WarehouseItem.Generator/Service/IWarehouseItemCache.cs new file mode 100644 index 00000000..8013c044 --- /dev/null +++ b/WarehouseItem.Generator/Service/IWarehouseItemCache.cs @@ -0,0 +1,25 @@ +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 new file mode 100644 index 00000000..ea932c49 --- /dev/null +++ b/WarehouseItem.Generator/Service/IWarehouseItemService.cs @@ -0,0 +1,17 @@ +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/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/WarehouseItemCache.cs b/WarehouseItem.Generator/Service/WarehouseItemCache.cs new file mode 100644 index 00000000..ddf951f1 --- /dev/null +++ b/WarehouseItem.Generator/Service/WarehouseItemCache.cs @@ -0,0 +1,99 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Microsoft.Extensions.Caching.Distributed; +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 new file mode 100644 index 00000000..9e67dd28 --- /dev/null +++ b/WarehouseItem.Generator/Service/WarehouseItemService.cs @@ -0,0 +1,33 @@ +using WarehouseItem.Generator.DTO; +using WarehouseItem.Generator.Generator; +using WarehouseItem.Generator.Service.Messaging; + +namespace WarehouseItem.Generator.Service; + +/// +/// Реализация сервиса работы с товарами на складе. +/// +public sealed class WarehouseItemService( + WarehouseItemGenerator generator, + IWarehouseItemCache cache, + IPublisherService publisherService) : IWarehouseItemService +{ + /// + /// Получить товар по идентификатору. Если товар не найден в кэше, генерирует новый, + /// сохраняет в кэш и отправляет в SNS для записи в S3. + /// + public async Task GetAsync(int id, CancellationToken cancellationToken = default) + { + var cached = await cache.GetAsync(id, cancellationToken); + if (cached is not null) + { + return cached; + } + + 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 new file mode 100644 index 00000000..043859f5 --- /dev/null +++ b/WarehouseItem.Generator/WarehouseItem.Generator.csproj @@ -0,0 +1,23 @@ + + + + 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..24a0b147 --- /dev/null +++ b/WarehouseItem.Generator/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.Generator/appsettings.json b/WarehouseItem.Generator/appsettings.json new file mode 100644 index 00000000..3af4bd71 --- /dev/null +++ b/WarehouseItem.Generator/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "WarehouseItemCache": { + "AbsoluteExpirationSeconds": 300 + }, + "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 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 + + + +