diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..cd967fc3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..f3d48486 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №2 «Балансировка нагрузки» + Вариант №38 "Query Based" + Выполнена Елагиным Денисом 6513 + Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..7f22c193 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "http://localhost:5300/residential-building" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..30762b2c 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -5,6 +5,18 @@ 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}") = "ResidentialBuilding.Generator", "ResidentialBuilding.Generator\ResidentialBuilding.Generator.csproj", "{4C3748F7-BE7B-4C97-A656-D0D467E4BC5D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResidentialBuilding.AppHost", "ResidentialBuilding.AppHost\ResidentialBuilding.AppHost.csproj", "{248C1F9B-F012-4C7C-A458-4E2D0F918A70}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResidentialBuilding.ServiceDefaults", "ResidentialBuilding.ServiceDefaults\ResidentialBuilding.ServiceDefaults.csproj", "{3AEE6EF1-603F-411D-89C2-5CE78EBCAEBA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResidentialBuilding.Gateway", "ResidentialBuilding.Gateway\ResidentialBuilding.Gateway.csproj", "{2CC59865-F962-47FF-9C2F-1CB6F535316B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResidentialBuilding.FileService", "ResidentialBuilding.FileService\ResidentialBuilding.FileService.csproj", "{B5797E57-88BC-41C8-97A8-D226E4596503}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResidentialBuilding.Tests", "ResidentialBuilding.Tests\ResidentialBuilding.Tests.csproj", "{ADBF06EE-2008-4D96-8337-078A1C1E80DF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +27,30 @@ 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 + {4C3748F7-BE7B-4C97-A656-D0D467E4BC5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C3748F7-BE7B-4C97-A656-D0D467E4BC5D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C3748F7-BE7B-4C97-A656-D0D467E4BC5D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C3748F7-BE7B-4C97-A656-D0D467E4BC5D}.Release|Any CPU.Build.0 = Release|Any CPU + {248C1F9B-F012-4C7C-A458-4E2D0F918A70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {248C1F9B-F012-4C7C-A458-4E2D0F918A70}.Debug|Any CPU.Build.0 = Debug|Any CPU + {248C1F9B-F012-4C7C-A458-4E2D0F918A70}.Release|Any CPU.ActiveCfg = Release|Any CPU + {248C1F9B-F012-4C7C-A458-4E2D0F918A70}.Release|Any CPU.Build.0 = Release|Any CPU + {3AEE6EF1-603F-411D-89C2-5CE78EBCAEBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3AEE6EF1-603F-411D-89C2-5CE78EBCAEBA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3AEE6EF1-603F-411D-89C2-5CE78EBCAEBA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3AEE6EF1-603F-411D-89C2-5CE78EBCAEBA}.Release|Any CPU.Build.0 = Release|Any CPU + {2CC59865-F962-47FF-9C2F-1CB6F535316B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2CC59865-F962-47FF-9C2F-1CB6F535316B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2CC59865-F962-47FF-9C2F-1CB6F535316B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2CC59865-F962-47FF-9C2F-1CB6F535316B}.Release|Any CPU.Build.0 = Release|Any CPU + {B5797E57-88BC-41C8-97A8-D226E4596503}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B5797E57-88BC-41C8-97A8-D226E4596503}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B5797E57-88BC-41C8-97A8-D226E4596503}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B5797E57-88BC-41C8-97A8-D226E4596503}.Release|Any CPU.Build.0 = Release|Any CPU + {ADBF06EE-2008-4D96-8337-078A1C1E80DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ADBF06EE-2008-4D96-8337-078A1C1E80DF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ADBF06EE-2008-4D96-8337-078A1C1E80DF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ADBF06EE-2008-4D96-8337-078A1C1E80DF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ResidentialBuilding.AppHost/AppHost.cs b/ResidentialBuilding.AppHost/AppHost.cs new file mode 100644 index 00000000..14517138 --- /dev/null +++ b/ResidentialBuilding.AppHost/AppHost.cs @@ -0,0 +1,61 @@ +using Amazon; +using Aspire.Hosting.LocalStack.Container; + +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("residential-building-cache") + .WithRedisInsight(containerName: "residential-building-insight"); + +var gateway = builder.AddProject("gateway") + .WithEndpoint("http", endpoint => endpoint.Port = 5300) + .WithExternalHttpEndpoints(); + +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +var localstack = builder + .AddLocalStack("residential-building-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/residential-building-template-sns-s3.yaml", "residential-building") + .WithReference(awsConfig); + +const int generatorPortBase = 5200; +for (var i = 1; i <= 5; ++i) +{ + var generator = builder.AddProject($"generator-{i}") + .WithReference(cache, "residential-building-cache") + .WithEndpoint("http", endpoint => endpoint.Port = generatorPortBase + i) + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SNS") + .WaitFor(cache) + .WaitFor(awsResources); + + gateway.WaitFor(generator); +} + +builder.AddProject("client") + .WithReference(gateway) + .WaitFor(gateway); + +var fileService = builder.AddProject("residential-building-file-service") + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SNS") + .WithEnvironment("Settings__S3Hosting", "Localstack") + .WaitFor(awsResources); +fileService.WithEnvironment("AWS__Resources__SNSUrl", "http://host.docker.internal:5280/api/sns"); + +builder.UseLocalStack(localstack); + +builder.Build().Run(); \ No newline at end of file diff --git a/ResidentialBuilding.AppHost/CloudFormation/residential-building-template-sns-s3.yaml b/ResidentialBuilding.AppHost/CloudFormation/residential-building-template-sns-s3.yaml new file mode 100644 index 00000000..07f33505 --- /dev/null +++ b/ResidentialBuilding.AppHost/CloudFormation/residential-building-template-sns-s3.yaml @@ -0,0 +1,59 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: 'Cloud formation template for residential building project' + +Parameters: + BucketName: + Type: String + Description: Name for the S3 bucket + Default: 'residential-building-bucket' + + TopicName: + Type: String + Description: Name for the SNS topic + Default: 'residential-building-topic' + +Resources: + ResidentialBuildingBucket: + 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 + + ResidentialBuildingTopic: + 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 ResidentialBuildingBucket + + S3BucketArn: + Description: ARN of the S3 bucket + Value: !GetAtt ResidentialBuildingBucket.Arn + + SNSTopicName: + Description: Name of the SNS topic + Value: !GetAtt ResidentialBuildingTopic.TopicName + + SNSTopicArn: + Description: ARN of the SNS topic + Value: !Ref ResidentialBuildingTopic \ No newline at end of file diff --git a/ResidentialBuilding.AppHost/Properties/launchSettings.json b/ResidentialBuilding.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..330da1c2 --- /dev/null +++ b/ResidentialBuilding.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/ResidentialBuilding.AppHost/ResidentialBuilding.AppHost.csproj b/ResidentialBuilding.AppHost/ResidentialBuilding.AppHost.csproj new file mode 100644 index 00000000..677ac305 --- /dev/null +++ b/ResidentialBuilding.AppHost/ResidentialBuilding.AppHost.csproj @@ -0,0 +1,26 @@ + + + + + + Exe + net8.0 + enable + enable + ed7e1e47-dc98-4419-8424-85412466aa9b + + + + + + + + + + + + + + + + diff --git a/ResidentialBuilding.AppHost/appsettings.Development.json b/ResidentialBuilding.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/ResidentialBuilding.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ResidentialBuilding.AppHost/appsettings.json b/ResidentialBuilding.AppHost/appsettings.json new file mode 100644 index 00000000..a6b256bb --- /dev/null +++ b/ResidentialBuilding.AppHost/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + }, + "LocalStack": { + "UseLocalStack": true + } +} diff --git a/ResidentialBuilding.FileService/Controller/S3StorageController.cs b/ResidentialBuilding.FileService/Controller/S3StorageController.cs new file mode 100644 index 00000000..a28d4b72 --- /dev/null +++ b/ResidentialBuilding.FileService/Controller/S3StorageController.cs @@ -0,0 +1,68 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text; +using System.Text.Json.Nodes; +using ResidentialBuilding.FileService.Service.Storage; + +namespace ResidentialBuilding.FileService.Controller; + +/// +/// Контроллер для взаимодейсвия с S3 +/// +/// Служба для работы с S3 +/// Логгер +[ApiController] +[Route("api/s3")] +public class S3StorageController(IFileService fileService, ILogger logger) : ControllerBase +{ + /// + /// Метод для получения списка хранящихся в S3 файлов + /// + /// Список с ключами файлов + [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 }); + } + } + + /// + /// Получает строковое представление хранящегося в S3 документа + /// + /// Ключ файла + /// Строковое представление файла + [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 }); + } + } +} \ No newline at end of file diff --git a/ResidentialBuilding.FileService/Controller/SnsSubscriberController.cs b/ResidentialBuilding.FileService/Controller/SnsSubscriberController.cs new file mode 100644 index 00000000..f9be6fb8 --- /dev/null +++ b/ResidentialBuilding.FileService/Controller/SnsSubscriberController.cs @@ -0,0 +1,78 @@ +using Amazon.SimpleNotificationService.Util; +using Microsoft.AspNetCore.Mvc; +using System.Text; +using ResidentialBuilding.FileService.Service.Storage; + +namespace ResidentialBuilding.FileService.Controller; + +/// +/// Контроллер для приема сообщений от SNS +/// +/// Служба для работы с S3 +/// Логгер +[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(); + } +} \ No newline at end of file diff --git a/ResidentialBuilding.FileService/Program.cs b/ResidentialBuilding.FileService/Program.cs new file mode 100644 index 00000000..df74a07d --- /dev/null +++ b/ResidentialBuilding.FileService/Program.cs @@ -0,0 +1,34 @@ +using Amazon.S3; +using Amazon.SimpleNotificationService; +using LocalStack.Client.Extensions; +using Microsoft.AspNetCore.Builder; +using ResidentialBuilding.ServiceDefaults; +using ResidentialBuilding.FileService.Service.Messaging; +using ResidentialBuilding.FileService.Service.Storage; + +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(); \ No newline at end of file diff --git a/ResidentialBuilding.FileService/Properties/launchSettings.json b/ResidentialBuilding.FileService/Properties/launchSettings.json new file mode 100644 index 00000000..f68ddb41 --- /dev/null +++ b/ResidentialBuilding.FileService/Properties/launchSettings.json @@ -0,0 +1,40 @@ +{ + "profiles": { + "http": { + "commandName": "Project", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5280" + }, + "https": { + "commandName": "Project", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7099;http://localhost:5280" + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Container (Dockerfile)": { + "commandName": "Docker", + "launchBrowser": false, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "environmentVariables": { + "ASPNETCORE_HTTPS_PORTS": "8081", + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true, + "useSSL": true + } + } +} \ No newline at end of file diff --git a/ResidentialBuilding.FileService/ResidentialBuilding.FileService.csproj b/ResidentialBuilding.FileService/ResidentialBuilding.FileService.csproj new file mode 100644 index 00000000..25d51140 --- /dev/null +++ b/ResidentialBuilding.FileService/ResidentialBuilding.FileService.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + dotnet-ResidentialBuilding.FileService-3fc8b533-d1b8-4ada-b0c8-745b4c7331f7 + Linux + ResidentialBuilding.FileService + + + + + + + + + + + + + + + .dockerignore + + + diff --git a/ResidentialBuilding.FileService/Service/Messaging/ISubscriptionService.cs b/ResidentialBuilding.FileService/Service/Messaging/ISubscriptionService.cs new file mode 100644 index 00000000..900b8cc3 --- /dev/null +++ b/ResidentialBuilding.FileService/Service/Messaging/ISubscriptionService.cs @@ -0,0 +1,9 @@ +namespace ResidentialBuilding.FileService.Service.Messaging; + +public interface ISubscriptionService +{ + /// + /// Делает попытку подписаться на топик SNS + /// + public Task SubscribeEndpoint(); +} \ No newline at end of file diff --git a/ResidentialBuilding.FileService/Service/Messaging/SnsSubscriptionService.cs b/ResidentialBuilding.FileService/Service/Messaging/SnsSubscriptionService.cs new file mode 100644 index 00000000..70182c9a --- /dev/null +++ b/ResidentialBuilding.FileService/Service/Messaging/SnsSubscriptionService.cs @@ -0,0 +1,51 @@ +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; +using System.Net; + +namespace ResidentialBuilding.FileService.Service.Messaging; + +/// +/// Служба для подписки на SNS на старте приложения +/// +/// Клиент SNS +/// Конфигурация +/// Логгер +public class SnsSubscriptionService( + IAmazonSimpleNotificationService snsClient, + IConfiguration configuration, + ILogger logger) : ISubscriptionService +{ + /// + /// Уникальный идентификатор топика SNS + /// + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] ?? + throw new KeyNotFoundException("SNS topic link was not found in configuration"); + + /// + /// Делает попытку подписаться на топик SNS + /// + 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} is successfully, waiting for confirmation", + _topicArn); + } + } +} \ No newline at end of file diff --git a/ResidentialBuilding.FileService/Service/Storage/AwsFileService.cs b/ResidentialBuilding.FileService/Service/Storage/AwsFileService.cs new file mode 100644 index 00000000..cb89d11f --- /dev/null +++ b/ResidentialBuilding.FileService/Service/Storage/AwsFileService.cs @@ -0,0 +1,139 @@ +using Amazon.S3; +using Amazon.S3.Model; +using System.Net; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace ResidentialBuilding.FileService.Service.Storage; + +/// +/// Реализация интерфейса IFileService, отвечающая за работу с хранилищем Amazon S3 +/// +/// Клиент AWS SDK для работы с Amazon S3 +/// Конфигурация приложения, из которой извлекается имя 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("Began 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 {file}.", fileData); + + 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("Began uploading residential building with id={Id} onto bucket {bucket}.", id, + _bucketName); + var request = new PutObjectRequest + { + BucketName = _bucketName, + Key = $"residential_building_{id}.json", + InputStream = stream + }; + + var response = await client.PutObjectAsync(request); + + if (response.HttpStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to upload residential building {file}: {code}.", id, response.HttpStatusCode); + return false; + } + + logger.LogInformation("Finished uploading residential building {file} to {bucket}.", id, _bucketName); + return true; + } + + /// + public async Task DownloadFile(string key) + { + logger.LogInformation("Began 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 occurred 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 occurred 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, "Unhandled exception occurred during checking bucket {bucket}.", _bucketName); + throw; + } + } +} \ No newline at end of file diff --git a/ResidentialBuilding.FileService/Service/Storage/IFileService.cs b/ResidentialBuilding.FileService/Service/Storage/IFileService.cs new file mode 100644 index 00000000..706751a2 --- /dev/null +++ b/ResidentialBuilding.FileService/Service/Storage/IFileService.cs @@ -0,0 +1,33 @@ +using System.Text.Json.Nodes; + +namespace ResidentialBuilding.FileService.Service.Storage; + +/// +/// Интерфейс службы для манипуляции файлами в объектном хранилище +/// +public interface IFileService +{ + /// + /// Отправляет файл в хранилище + /// + /// Строковая репрезентация сохраняемого файла + public Task UploadFile(string fileData); + + /// + /// Получает список всех файлов из хранилища + /// + /// Список путей к файлам + public Task> GetFilesList(); + + /// + /// Получает строковую репрезентацию файла из хранилища + /// + /// Путь к файлу в бакете + /// Строковая репрезентация прочтенного файла + public Task DownloadFile(string filePath); + + /// + /// Создает S3 бакет при необходимости + /// + public Task EnsureBucketExists(); +} \ No newline at end of file diff --git a/ResidentialBuilding.FileService/appsettings.Development.json b/ResidentialBuilding.FileService/appsettings.Development.json new file mode 100644 index 00000000..b2dcdb67 --- /dev/null +++ b/ResidentialBuilding.FileService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/ResidentialBuilding.FileService/appsettings.json b/ResidentialBuilding.FileService/appsettings.json new file mode 100644 index 00000000..b2dcdb67 --- /dev/null +++ b/ResidentialBuilding.FileService/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/ResidentialBuilding.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs b/ResidentialBuilding.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs new file mode 100644 index 00000000..89678583 --- /dev/null +++ b/ResidentialBuilding.Gateway/LoadBalancer/QueryBasedLoadBalancer.cs @@ -0,0 +1,64 @@ +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.Values; + +namespace ResidentialBuilding.Gateway.LoadBalancer; + +/// +/// Query-based балансировщик нагрузки для Ocelot. +/// Выбирает downstream-сервис на основе query-параметра id запроса. +/// +/// Логгер. +/// Асинхронная функция, возвращающая актуальный список доступных downstream-сервисов. +public class QueryBasedLoadBalancer(ILogger logger, Func>> services) + : ILoadBalancer +{ + private const string IdQueryParamName = "id"; + + public string Type => nameof(QueryBasedLoadBalancer); + + /// + /// Метод вызывается Ocelot после завершения запроса к downstream-сервису. + /// В текущей реализации ничего не делает (не требуется для query-based подхода). + /// + public void Release(ServiceHostAndPort hostAndPort) { } + + /// + /// Основной метод выбора downstream-сервиса на основе query-параметра id текущего запроса. + /// + /// Контекст HTTP-запроса (используется для доступа к Query string). + /// + /// OkResponse с выбранным адресом сервиса в зависимости от query-параметра id, либо случайно выбранный адрес + /// сервиса, если query-параметр не задан. + /// + public async Task> LeaseAsync(HttpContext httpContext) + { + var currentServices = await services.Invoke(); + var query = httpContext.Request.Query; + + if (!query.TryGetValue(IdQueryParamName, out var idValues) || idValues.Count <= 0) + { + return SelectRandomService(currentServices); + } + + var idStr = idValues.First(); + + if (string.IsNullOrWhiteSpace(idStr) || !int.TryParse(idStr, out var id) || id < 0) + { + return SelectRandomService(currentServices); + } + + var index = id % currentServices.Count; + logger.LogInformation("Query based selected index={index}.", index); + + return new OkResponse(currentServices[index].HostAndPort); + } + + private OkResponse SelectRandomService(List currentServices) + { + var randomIndex = Random.Shared.Next(currentServices.Count); + logger.LogWarning("Query doesn't contain correct id parameter, index={randomIndex} selected by random.", randomIndex); + + return new OkResponse(currentServices[randomIndex].HostAndPort); + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Gateway/Program.cs b/ResidentialBuilding.Gateway/Program.cs new file mode 100644 index 00000000..3df66775 --- /dev/null +++ b/ResidentialBuilding.Gateway/Program.cs @@ -0,0 +1,35 @@ +using Ocelot.DependencyInjection; +using Ocelot.Middleware; +using ResidentialBuilding.Gateway.LoadBalancer; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddCors(options => +{ + options.AddPolicy("AllowLocalDev", 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) => + { + var logger = sp.GetRequiredService>(); + return new QueryBasedLoadBalancer(logger, discoveryProvider.GetAsync); + }); + +var app = builder.Build(); + +app.UseCors("AllowLocalDev"); +await app.UseOcelot(); + +await app.RunAsync(); diff --git a/ResidentialBuilding.Gateway/Properties/launchSettings.json b/ResidentialBuilding.Gateway/Properties/launchSettings.json new file mode 100644 index 00000000..e210075f --- /dev/null +++ b/ResidentialBuilding.Gateway/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:22127", + "sslPort": 44310 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5298", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7014;http://localhost:5298", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ResidentialBuilding.Gateway/ResidentialBuilding.Gateway.csproj b/ResidentialBuilding.Gateway/ResidentialBuilding.Gateway.csproj new file mode 100644 index 00000000..8123f6ca --- /dev/null +++ b/ResidentialBuilding.Gateway/ResidentialBuilding.Gateway.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/ResidentialBuilding.Gateway/appsettings.Development.json b/ResidentialBuilding.Gateway/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/ResidentialBuilding.Gateway/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ResidentialBuilding.Gateway/appsettings.json b/ResidentialBuilding.Gateway/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/ResidentialBuilding.Gateway/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/ResidentialBuilding.Gateway/ocelot.json b/ResidentialBuilding.Gateway/ocelot.json new file mode 100644 index 00000000..54ac5e1c --- /dev/null +++ b/ResidentialBuilding.Gateway/ocelot.json @@ -0,0 +1,20 @@ +{ + "Routes": [ + { + "DownstreamPathTemplate": "/api/residential-building", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { "Host": "localhost", "Port": 5201 }, + { "Host": "localhost", "Port": 5202 }, + { "Host": "localhost", "Port": 5203 }, + { "Host": "localhost", "Port": 5204 }, + { "Host": "localhost", "Port": 5205 } + ], + "UpstreamPathTemplate": "/residential-building", + "UpstreamHttpMethod": [ "GET" ], + "LoadBalancerOptions": { + "Type": "QueryBasedLoadBalancer" + } + } + ] +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Controller/ResidentialBuildingController.cs b/ResidentialBuilding.Generator/Controller/ResidentialBuildingController.cs new file mode 100644 index 00000000..0fbc4c23 --- /dev/null +++ b/ResidentialBuilding.Generator/Controller/ResidentialBuildingController.cs @@ -0,0 +1,37 @@ +using Generator.DTO; +using Generator.Service; +using Microsoft.AspNetCore.Mvc; + +namespace Generator.Controller; + +/// +/// Контроллер для объектов жилого строительства. +/// +[Route("api/residential-building")] +[ApiController] +public class ResidentialBuildingController(ILogger logger, IResidentialBuildingService residentialBuildingService) : ControllerBase +{ + /// + /// Получение объекта жилого строительства по id. + /// + /// Идентификатор объекта жилого строительства. + /// DTO объекта жилого строительства. + /// Успешное получение объекта. + /// Некорректный id. + [HttpGet] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task> GetResidentialBuilding([FromQuery] int id) + { + if (id <= 0) + { + return BadRequest("id must be >= 0"); + } + + logger.LogInformation("Getting residential building with Id={id}.", id); + var result = await residentialBuildingService.GetByIdAsync(id); + logger.LogInformation("Residential building with Id={id} successfully received.", id); + + return Ok(result); + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/DTO/ResidentialBuildingDto.cs b/ResidentialBuilding.Generator/DTO/ResidentialBuildingDto.cs new file mode 100644 index 00000000..2c9eac03 --- /dev/null +++ b/ResidentialBuilding.Generator/DTO/ResidentialBuildingDto.cs @@ -0,0 +1,57 @@ +namespace Generator.DTO; + +/// +/// DTO объекта жилого строительства +/// +public class ResidentialBuildingDto +{ + /// + /// Идентификатор в системе + /// + public int Id { get; set; } + + /// + /// Адрес + /// + public string Address { get; set; } = string.Empty; + + /// + /// Тип недвижимости + /// + public string PropertyType { get; set; } = string.Empty; + + /// + /// Год постройки + /// + public int BuildYear { get; set; } + + /// + /// Общая площадь + /// + public double TotalArea { get; set; } + + /// + /// Жилая площадь + /// + public double LivingArea { get; set; } + + /// + /// Этаж + /// + public int? Floor { get; set; } + + /// + /// Этажность + /// + public int TotalFloors { get; set; } + + /// + /// Кадастровый номер + /// + public string CadastralNumber { get; set; } = string.Empty; + + /// + /// Кадастровая стоимость + /// + public decimal CadastralValue { get; set; } +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Generator/ResidentialBuildingGenerator.cs b/ResidentialBuilding.Generator/Generator/ResidentialBuildingGenerator.cs new file mode 100644 index 00000000..0468a35c --- /dev/null +++ b/ResidentialBuilding.Generator/Generator/ResidentialBuildingGenerator.cs @@ -0,0 +1,100 @@ +using Bogus; +using Generator.DTO; + +namespace Generator.Generator; + +/// +/// Генератор объектов жилого строительства на основе Bogus +/// +public class ResidentialBuildingGenerator(ILogger logger) +{ + private const int MinBuildYear = 1900; + + private const double MinTotalArea = 10.0; + + private const double MaxTotalArea = 1000.0; + + private const double MinLivingAreaPartOfTotalArea = 0.5; + + private const double MaxLivingAreaPartOfTotalArea = 0.85; + + private const int MinTotalFloors = 1; + + private const int MaxTotalFloors = 100; + + private const double MinPricePerM2 = 35; + + private const double MaxPricePerM2 = 200; + + private static readonly string[] _propertyTypes = + [ + "Квартира", + "ИЖС", + "Апартаменты", + "Офис" + ]; + + private static readonly Faker _faker = new Faker("ru") + .RuleFor(x => x.Address, f => f.Address.FullAddress()) + .RuleFor(x => x.PropertyType, f => f.PickRandom(_propertyTypes)) + .RuleFor(x => x.BuildYear, f => f.Random.Int(MinBuildYear, DateTime.Today.Year)) + .RuleFor(x => x.TotalArea, f => Math.Round(f.Random.Double(MinTotalArea, MaxTotalArea), 2)) + .RuleFor(x => x.LivingArea, (f, dto) => + { + var livingAreaPartOfTotalArea = + f.Random.Double(MinLivingAreaPartOfTotalArea, MaxLivingAreaPartOfTotalArea); + return Math.Round(livingAreaPartOfTotalArea * dto.TotalArea, 2); + }) + .RuleFor(x => x.TotalFloors, f => f.Random.Int(MinTotalFloors, MaxTotalFloors)) + .RuleFor(x => x.Floor, (f, dto) => + { + if (dto.PropertyType is "ИЖС") + { + return null; + } + + return f.Random.Int(1, dto.TotalFloors); + }) + .RuleFor(x => x.CadastralNumber, f => + $"{f.Random.Int(1, 99):D2}:" + + $"{f.Random.Int(1, 99):D2}:" + + $"{f.Random.Int(1, 9999999):D7}:" + + $"{f.Random.Int(1, 9999):D4}") + .RuleFor(x => x.CadastralValue, (f, dto) => + { + var pricePerM2 = f.Random.Double(MinPricePerM2, MaxPricePerM2); + var price = dto.TotalArea * pricePerM2; + return (decimal)Math.Round(price, 2); + }); + + /// + /// Генерирует объект жилого строительства для заданного идентификатора + /// + /// Идентификатор объекта жилого строительства + /// Сгенерированный объект жилого строительства + public ResidentialBuildingDto Generate(int id) + { + logger.LogInformation("Generating Residential Building for Id={id}", id); + + var generatedObject = _faker.Generate(); + generatedObject.Id = id; + + logger.LogInformation( + "Residential building generated: {@Building}", + new + { + generatedObject.Id, + generatedObject.Address, + generatedObject.PropertyType, + generatedObject.BuildYear, + generatedObject.TotalArea, + generatedObject.LivingArea, + generatedObject.Floor, + generatedObject.TotalFloors, + generatedObject.CadastralNumber, + generatedObject.CadastralValue + }); + + return generatedObject; + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Program.cs b/ResidentialBuilding.Generator/Program.cs new file mode 100644 index 00000000..481ddf1f --- /dev/null +++ b/ResidentialBuilding.Generator/Program.cs @@ -0,0 +1,28 @@ +using Amazon.SimpleNotificationService; +using Generator.Generator; +using Generator.Service; +using Generator.Service.Cache; +using Generator.Service.Messaging; +using LocalStack.Client.Extensions; +using ResidentialBuilding.ServiceDefaults; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("residential-building-cache"); + +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); + +builder.Services.AddControllers(); + +var app = builder.Build(); + +app.MapControllers(); + +app.Run(); \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Properties/launchSettings.json b/ResidentialBuilding.Generator/Properties/launchSettings.json new file mode 100644 index 00000000..a340fa14 --- /dev/null +++ b/ResidentialBuilding.Generator/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://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:5200", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7291;http://localhost:5200", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ResidentialBuilding.Generator/ResidentialBuilding.Generator.csproj b/ResidentialBuilding.Generator/ResidentialBuilding.Generator.csproj new file mode 100644 index 00000000..eeaf2a82 --- /dev/null +++ b/ResidentialBuilding.Generator/ResidentialBuilding.Generator.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + Generator + + + + + + + + + + + + + + + + diff --git a/ResidentialBuilding.Generator/Service/Cache/CacheService.cs b/ResidentialBuilding.Generator/Service/Cache/CacheService.cs new file mode 100644 index 00000000..2fc2cdc5 --- /dev/null +++ b/ResidentialBuilding.Generator/Service/Cache/CacheService.cs @@ -0,0 +1,86 @@ +using Microsoft.Extensions.Caching.Distributed; +using System.Text.Json; + +namespace Generator.Service.Cache; + +/// +/// Реализация на базе (Redis). +/// +public class CacheService( + ILogger logger, + IDistributedCache cache, + IConfiguration configuration) : ICacheService +{ + private const string CacheKeyPrefix = "residential-building:"; + + private const int CacheExpirationTimeMinutesDefault = 15; + + private readonly TimeSpan _cacheExpirationTimeMinutes = + TimeSpan.FromMinutes(configuration.GetValue("CacheSettings:ExpirationTimeMinutes", + CacheExpirationTimeMinutesDefault)); + + /// + public async Task GetCache(int id, CancellationToken cancellationToken = default) + { + var cacheKey = $"{CacheKeyPrefix}{id}"; + + string? jsonCached; + try + { + jsonCached = await cache.GetStringAsync(cacheKey, cancellationToken); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Failed to read from distributed cache for key={cacheKey}.", cacheKey); + return default; + } + + if (string.IsNullOrEmpty(jsonCached)) + { + logger.LogWarning("Received cache for key={cacheKey} is null or empty.", cacheKey); + return default; + } + + T? objCached; + try + { + objCached = JsonSerializer.Deserialize(jsonCached); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Invalid JSON in cache for key {cacheKey}.", cacheKey); + return default; + } + + if (objCached is null) + { + logger.LogWarning("Cache for key {cacheKey} returned null.", cacheKey); + return default; + } + + logger.LogInformation("Cache for cache key {cacheKey} is valid, returned", cacheKey); + return objCached; + } + + /// + public async Task SetCache(int id, T obj, CancellationToken cancellationToken = default) + { + var cacheKey = $"{CacheKeyPrefix}{id}"; + + try + { + var cacheOptions = new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheExpirationTimeMinutes + }; + + await cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(obj), cacheOptions, cancellationToken); + return true; + } + catch(Exception ex) + { + logger.LogWarning(ex, "Failed to write object for cache key {cacheKey}.", cacheKey); + return false; + } + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Service/Cache/ICacheService.cs b/ResidentialBuilding.Generator/Service/Cache/ICacheService.cs new file mode 100644 index 00000000..1eb98c6c --- /dev/null +++ b/ResidentialBuilding.Generator/Service/Cache/ICacheService.cs @@ -0,0 +1,32 @@ +namespace Generator.Service.Cache; + +/// +/// Интерфейс сервиса кэширования, абстрагирующий работу с распределённым кэшем (Redis). +/// +public interface ICacheService +{ + /// + /// Получает объект из кэша по идентификатору. + /// + /// Тип десериализуемого объекта. + /// Идентификатор жилого здания (ключ кэша). + /// Токен отмены операции. + /// + /// Десериализованный объект типа , + /// или default, если объект отсутствует или произошла ошибка. + /// + public Task GetCache(int id, CancellationToken cancellationToken = default); + + /// + /// Сохраняет объект в распределённый кэш с заданным временем жизни. + /// + /// Тип сохраняемого объекта. + /// Идентификатор жилого здания (ключ кэша). + /// Объект для сохранения в кэше. + /// Токен отмены операции. + /// + /// true — если объект успешно сохранён, + /// false — если произошла ошибка при записи. + /// + public Task SetCache(int id, T obj, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Service/IResidentialBuildingService.cs b/ResidentialBuilding.Generator/Service/IResidentialBuildingService.cs new file mode 100644 index 00000000..02476703 --- /dev/null +++ b/ResidentialBuilding.Generator/Service/IResidentialBuildingService.cs @@ -0,0 +1,21 @@ +using Generator.DTO; + +namespace Generator.Service; + +/// +/// Сервис получения объектов жилого строительства по идентификатору. +/// Если удалось найти объект в кэше - возвращает его, иначе генерирует, кэширует и возвращает сгенерированный. +/// +public interface IResidentialBuildingService +{ + /// + /// Пытается найти в кэше объект с заданным идентификатором: + /// если удалось, то десериализует объект из JSON-а и возвращает; + /// если не удалось или произошла ошибка в ходе получения/десериализации, то генерирует объект, сохраняет в кэш и + /// возвращает сгенерированный. + /// + /// Идентификатор объекта жилого строительства. + /// Токен отмены. + /// DTO объекта жилого строительства. + public Task GetByIdAsync(int id, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Service/Messaging/IPublisherService.cs b/ResidentialBuilding.Generator/Service/Messaging/IPublisherService.cs new file mode 100644 index 00000000..4c1fcc10 --- /dev/null +++ b/ResidentialBuilding.Generator/Service/Messaging/IPublisherService.cs @@ -0,0 +1,15 @@ +using Generator.DTO; + +namespace Generator.Service.Messaging; + +/// +/// Интерфейс службы для отправки генерируемых объектов в брокер сообщений +/// +public interface IPublisherService +{ + /// + /// Отправляет сообщение в брокер + /// + /// Объект жилого строительства + public Task SendMessage(ResidentialBuildingDto residentialBuilding); +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Service/Messaging/SnsPublisherService.cs b/ResidentialBuilding.Generator/Service/Messaging/SnsPublisherService.cs new file mode 100644 index 00000000..308604f6 --- /dev/null +++ b/ResidentialBuilding.Generator/Service/Messaging/SnsPublisherService.cs @@ -0,0 +1,50 @@ +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; +using Generator.DTO; +using System.Net; +using System.Text.Json; + +namespace Generator.Service.Messaging; + +/// +/// Сервис для отправки сообщений в SNS +/// +/// Клиент SNS +/// Конфигурация +/// Логгер +public class SnsPublisherService( + IAmazonSimpleNotificationService client, + IConfiguration configuration, + ILogger logger) : IPublisherService +{ + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] + ?? throw new KeyNotFoundException( + "SNS topic link was not found in configuration"); + + /// + public async Task SendMessage(ResidentialBuildingDto residentialBuilding) + { + try + { + var json = JsonSerializer.Serialize(residentialBuilding); + 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("Residential building with id={Id} was sent to file service via SNS", + residentialBuilding.Id); + } + catch (Exception ex) + { + logger.LogError(ex, "Unable to send residential building through SNS topic"); + } + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/Service/ResidentialBuildingService.cs b/ResidentialBuilding.Generator/Service/ResidentialBuildingService.cs new file mode 100644 index 00000000..07d43f78 --- /dev/null +++ b/ResidentialBuilding.Generator/Service/ResidentialBuildingService.cs @@ -0,0 +1,36 @@ +using Generator.DTO; +using Generator.Generator; +using Generator.Service.Cache; +using Generator.Service.Messaging; + +namespace Generator.Service; + +/// +/// Реализация . +/// +public class ResidentialBuildingService( + ILogger logger, + ResidentialBuildingGenerator generator, + ICacheService cacheService, + IPublisherService messagingService + ) : IResidentialBuildingService +{ + + /// + public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) + { + var obj = await cacheService.GetCache(id, cancellationToken); + + if (obj is not null) + { + return obj; + } + + obj = generator.Generate(id); + await cacheService.SetCache(id, obj, cancellationToken); + await messagingService.SendMessage(obj); + + logger.LogInformation("Generated, cached and sent to SNS residential building with Id={id}", id); + return obj; + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Generator/appsettings.Development.json b/ResidentialBuilding.Generator/appsettings.Development.json new file mode 100644 index 00000000..af36b864 --- /dev/null +++ b/ResidentialBuilding.Generator/appsettings.Development.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "CacheSettings": { + "ExpirationTimeMinutes": 5 + } +} diff --git a/ResidentialBuilding.Generator/appsettings.json b/ResidentialBuilding.Generator/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/ResidentialBuilding.Generator/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/ResidentialBuilding.ServiceDefaults/Extensions.cs b/ResidentialBuilding.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..5e59484d --- /dev/null +++ b/ResidentialBuilding.ServiceDefaults/Extensions.cs @@ -0,0 +1,127 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +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 ResidentialBuilding.ServiceDefaults; + +// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + private const string HealthEndpointPath = "/health"; + private const string AlivenessEndpointPath = "/alive"; + + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/ResidentialBuilding.ServiceDefaults/ResidentialBuilding.ServiceDefaults.csproj b/ResidentialBuilding.ServiceDefaults/ResidentialBuilding.ServiceDefaults.csproj new file mode 100644 index 00000000..9fb1f653 --- /dev/null +++ b/ResidentialBuilding.ServiceDefaults/ResidentialBuilding.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/ResidentialBuilding.Tests/AppFixture.cs b/ResidentialBuilding.Tests/AppFixture.cs new file mode 100644 index 00000000..e8a6e06d --- /dev/null +++ b/ResidentialBuilding.Tests/AppFixture.cs @@ -0,0 +1,42 @@ +using Aspire.Hosting; + +namespace ResidentialBuilding.Tests; + +/// +/// Фикстура для интеграционных тестов, использующая .NET Aspire DistributedApplicationTesting. +/// Обеспечивает запуск полного стека приложения (AppHost) один раз для всех тестов класса, +/// использующих . +/// +public class AppFixture : IAsyncLifetime +{ + /// + /// Экземпляр запущенного распределённого приложения Aspire. + /// Доступен всем тестам через внедрение зависимости. + /// + 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(); + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Tests/IntegrationTests.cs b/ResidentialBuilding.Tests/IntegrationTests.cs new file mode 100644 index 00000000..bf4d20d7 --- /dev/null +++ b/ResidentialBuilding.Tests/IntegrationTests.cs @@ -0,0 +1,218 @@ +using Aspire.Hosting; +using Generator.DTO; +using System.Text.Json; + +namespace ResidentialBuilding.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("residential-building-file-service", "http"); + + var response = await gatewayClient.GetAsync($"/residential-building?id={id}"); + response.EnsureSuccessStatusCode(); + + var apiBuilding = Deserialize(await response.Content.ReadAsStringAsync()); + + await WaitForFileInS3Async(fileClient, id, TimeSpan.FromSeconds(10)); + + var s3Building = await GetBuildingFromS3Async(fileClient, id); + + Assert.NotNull(apiBuilding); + Assert.Equal(id, apiBuilding.Id); + Assert.NotNull(s3Building); + Assert.Equal(id, s3Building.Id); + Assert.Equivalent(apiBuilding, s3Building, strict: true); + } + + /// + /// Проверяет работу кэширования в Generator: + /// При повторных запросах одного и того же id возвращается идентичный объект, + /// и S3 тоже возвращает один и тот же объект. + /// + [Fact] + public async Task SameIds_GivingSameObjects() + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + var fileClient = _app.CreateHttpClient("residential-building-file-service", "http"); + + var id = Random.Shared.Next(20, 30); + + var response = await gatewayClient.GetAsync($"/residential-building?id={id}"); + response.EnsureSuccessStatusCode(); + + var apiBuilding = Deserialize(await response.Content.ReadAsStringAsync()); + + await WaitForFileInS3Async(fileClient, id, TimeSpan.FromSeconds(10)); + + var s3Building = await GetBuildingFromS3Async(fileClient, id); + + var response1 = await gatewayClient.GetAsync($"/residential-building?id={id}"); + response.EnsureSuccessStatusCode(); + + var apiBuilding1 = Deserialize(await response1.Content.ReadAsStringAsync()); + + await WaitForFileInS3Async(fileClient, id, TimeSpan.FromSeconds(10)); + + var s3Building1 = await GetBuildingFromS3Async(fileClient, id); + + Assert.NotNull(apiBuilding); + Assert.Equal(id, apiBuilding.Id); + Assert.NotNull(s3Building); + Assert.Equal(id, s3Building.Id); + Assert.Equivalent(apiBuilding, s3Building, strict: true); + + Assert.NotNull(apiBuilding1); + Assert.Equal(id, apiBuilding1.Id); + Assert.NotNull(s3Building1); + Assert.Equal(id, s3Building1.Id); + Assert.Equivalent(apiBuilding1, s3Building1, strict: true); + + Assert.Equivalent(apiBuilding, apiBuilding1, strict: true); + Assert.Equivalent(s3Building, s3Building1, strict: true); + } + + /// + /// Проверяет эндпоинт получения списка файлов в S3 и уникальность по Id. + /// Специально используется повторяющийся id (1001), чтобы убедиться, + /// что для одного идентификатора создаётся только один файл. + /// + [Fact] + public async Task S3_ReturnsCorrectListOfFiles() + { + var gatewayClient = _app.CreateHttpClient("gateway", "http"); + var fileClient = _app.CreateHttpClient("residential-building-file-service", "http"); + + var ids = new[] { 1001, 1002, 1003, 1001 }; + + foreach (var id in ids) + { + await gatewayClient.GetAsync($"/residential-building?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($"residential_building_{id}.json", fileList); + } + var addedFiles = fileList.Where(f => f.StartsWith("residential_building_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($"/residential-building?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("/residential-building"); + + 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("residential-building-file-service", "http"); + + var response = await gatewayClient.GetAsync($"/residential-building?id={invalidId}"); + + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + + await Task.Delay(5000); + + var fileName = $"residential_building_{invalidId}.json"; + var probe = await fileClient.GetAsync($"/api/s3/{fileName}"); + + Assert.False(probe.IsSuccessStatusCode, "Файл не должен был появиться в S3 при невалидном id"); + } + + /// + /// Ожидает появления файла в S3 с указанным id. + /// Поллинг с таймаутом — необходим из-за асинхронной природы связи SNS и FileService. + /// + private static async Task WaitForFileInS3Async(HttpClient fileClient, int id, TimeSpan timeout) + { + var fileName = $"residential_building_{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"); + } + + /// + /// Скачивает и десериализует объект ResidentialBuildingDto из S3 через FileService. + /// + private static async Task GetBuildingFromS3Async(HttpClient fileClient, int id) + { + var fileServiceResponse = await fileClient.GetAsync($"/api/s3/residential_building_{id}.json"); + Console.WriteLine(fileServiceResponse.Content.ReadAsStringAsync()); + return Deserialize(await fileServiceResponse.Content.ReadAsStringAsync()); + } +} \ No newline at end of file diff --git a/ResidentialBuilding.Tests/ResidentialBuilding.Tests.csproj b/ResidentialBuilding.Tests/ResidentialBuilding.Tests.csproj new file mode 100644 index 00000000..1e922405 --- /dev/null +++ b/ResidentialBuilding.Tests/ResidentialBuilding.Tests.csproj @@ -0,0 +1,42 @@ + + + + net8.0 + enable + enable + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PreserveNewest + CloudFormation\residential-building-template-sns-s3.yaml + + + +