diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor
index 661f1181..9c90290f 100644
--- a/Client.Wasm/Components/StudentCard.razor
+++ b/Client.Wasm/Components/StudentCard.razor
@@ -4,10 +4,10 @@
- Номер №X "Название лабораторной"
- Вариант №Х "Название варианта"
- Выполнена Фамилией Именем 65ХХ
- Ссылка на форк
+ Номер №3 "Интеграционное тестирование"
+ Вариант №36 "SNS, Minio"
+ Выполнена Барышниковым Владиславом 6513
+ Ссылка на форк
diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json
index d1fe7ab3..62e100de 100644
--- a/Client.Wasm/wwwroot/appsettings.json
+++ b/Client.Wasm/wwwroot/appsettings.json
@@ -6,5 +6,5 @@
}
},
"AllowedHosts": "*",
- "BaseAddress": ""
+ "BaseAddress": "https://localhost:7200/company-employee"
}
diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln
index cb48241d..d133c877 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}") = "CompanyEmployee.Generator", "CompanyEmployee.Generator\CompanyEmployee.Generator.csproj", "{76AE3AB2-7AF9-4BAA-BE4F-1FDEE3D052AA}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.AppHost", "CompanyEmployee.AppHost\CompanyEmployee.AppHost.csproj", "{11A4C40B-7996-4885-88E1-FA478E8AC6B6}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.ServiceDefaults", "CompanyEmployee.ServiceDefaults\CompanyEmployee.ServiceDefaults.csproj", "{DF70F3CE-6CD9-4334-AB24-0A2BBBD081E8}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.ApiGateway", "CompanyEmployee.ApiGateway\CompanyEmployee.ApiGateway.csproj", "{00432BCA-E982-451B-9D3F-648FE20FF285}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.EventSink", "CompanyEmployee.EventSink\CompanyEmployee.EventSink.csproj", "{6EF5D762-90CB-48CA-8D24-1AC90560660F}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.Tests", "CompanyEmployee.Tests\CompanyEmployee.Tests.csproj", "{28BC6A02-0734-45B8-89E9-EE44E9927921}"
+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
+ {76AE3AB2-7AF9-4BAA-BE4F-1FDEE3D052AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {76AE3AB2-7AF9-4BAA-BE4F-1FDEE3D052AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {76AE3AB2-7AF9-4BAA-BE4F-1FDEE3D052AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {76AE3AB2-7AF9-4BAA-BE4F-1FDEE3D052AA}.Release|Any CPU.Build.0 = Release|Any CPU
+ {11A4C40B-7996-4885-88E1-FA478E8AC6B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {11A4C40B-7996-4885-88E1-FA478E8AC6B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {11A4C40B-7996-4885-88E1-FA478E8AC6B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {11A4C40B-7996-4885-88E1-FA478E8AC6B6}.Release|Any CPU.Build.0 = Release|Any CPU
+ {DF70F3CE-6CD9-4334-AB24-0A2BBBD081E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {DF70F3CE-6CD9-4334-AB24-0A2BBBD081E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {DF70F3CE-6CD9-4334-AB24-0A2BBBD081E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {DF70F3CE-6CD9-4334-AB24-0A2BBBD081E8}.Release|Any CPU.Build.0 = Release|Any CPU
+ {00432BCA-E982-451B-9D3F-648FE20FF285}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {00432BCA-E982-451B-9D3F-648FE20FF285}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {00432BCA-E982-451B-9D3F-648FE20FF285}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {00432BCA-E982-451B-9D3F-648FE20FF285}.Release|Any CPU.Build.0 = Release|Any CPU
+ {6EF5D762-90CB-48CA-8D24-1AC90560660F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {6EF5D762-90CB-48CA-8D24-1AC90560660F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {6EF5D762-90CB-48CA-8D24-1AC90560660F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {6EF5D762-90CB-48CA-8D24-1AC90560660F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {28BC6A02-0734-45B8-89E9-EE44E9927921}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {28BC6A02-0734-45B8-89E9-EE44E9927921}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {28BC6A02-0734-45B8-89E9-EE44E9927921}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {28BC6A02-0734-45B8-89E9-EE44E9927921}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/CompanyEmployee.ApiGateway/CompanyEmployee.ApiGateway.csproj b/CompanyEmployee.ApiGateway/CompanyEmployee.ApiGateway.csproj
new file mode 100644
index 00000000..cdaba523
--- /dev/null
+++ b/CompanyEmployee.ApiGateway/CompanyEmployee.ApiGateway.csproj
@@ -0,0 +1,17 @@
+
+
+
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CompanyEmployee.ApiGateway/Program.cs b/CompanyEmployee.ApiGateway/Program.cs
new file mode 100644
index 00000000..52e38398
--- /dev/null
+++ b/CompanyEmployee.ApiGateway/Program.cs
@@ -0,0 +1,38 @@
+using CompanyEmployee.ApiGateway;
+using Ocelot.DependencyInjection;
+using Ocelot.Middleware;
+
+var builder = WebApplication.CreateBuilder(args);
+
+builder.Services.AddCors(options =>
+{
+ options.AddPolicy("wasm", policy =>
+ policy.SetIsOriginAllowed(origin =>
+ {
+ try
+ {
+ var uri = new Uri(origin);
+ return uri.Host == "localhost";
+ }
+ catch
+ {
+ return false;
+ }
+ })
+ .WithMethods("GET")
+ .AllowAnyHeader());
+});
+
+builder.Configuration.AddJsonFile("ocelotSettings.json", optional: false, reloadOnChange: true);
+
+builder.Services.AddOcelot(builder.Configuration).AddCustomLoadBalancer((serviceProvider, _, discoveryProvider) =>
+ new QueryBasedLoadBalancer(discoveryProvider.GetAsync,
+ serviceProvider.GetRequiredService>()));
+
+var app = builder.Build();
+
+app.UseCors("wasm");
+
+await app.UseOcelot();
+
+await app.RunAsync();
\ No newline at end of file
diff --git a/CompanyEmployee.ApiGateway/Properties/launchSettings.json b/CompanyEmployee.ApiGateway/Properties/launchSettings.json
new file mode 100644
index 00000000..f759638c
--- /dev/null
+++ b/CompanyEmployee.ApiGateway/Properties/launchSettings.json
@@ -0,0 +1,38 @@
+{
+ "$schema": "http://json.schemastore.org/launchsettings.json",
+ "iisSettings": {
+ "windowsAuthentication": false,
+ "anonymousAuthentication": true,
+ "iisExpress": {
+ "applicationUrl": "http://localhost:23345",
+ "sslPort": 44320
+ }
+ },
+ "profiles": {
+ "http": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "applicationUrl": "http://localhost:5080",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "https": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "applicationUrl": "https://localhost:7236;http://localhost:5080",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "IIS Express": {
+ "commandName": "IISExpress",
+ "launchBrowser": true,
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ }
+ }
+}
diff --git a/CompanyEmployee.ApiGateway/QueryBasedLoadBalancer.cs b/CompanyEmployee.ApiGateway/QueryBasedLoadBalancer.cs
new file mode 100644
index 00000000..808538c3
--- /dev/null
+++ b/CompanyEmployee.ApiGateway/QueryBasedLoadBalancer.cs
@@ -0,0 +1,63 @@
+using Ocelot.DownstreamRouteFinder.Finder;
+using Ocelot.LoadBalancer.Interfaces;
+using Ocelot.Responses;
+using Ocelot.Values;
+
+namespace CompanyEmployee.ApiGateway;
+
+///
+/// Класс для балансировкик нагрузки с использованием параметра запроса.
+///
+/// Функция, возвращающая список downstream-сервисов
+/// Логгер
+public class QueryBasedLoadBalancer(Func>> services, ILogger logger)
+ : ILoadBalancer
+{
+
+ public string Type => nameof(QueryBasedLoadBalancer);
+
+ ///
+ /// Метод для выбора downstream-сервиса на основании параметра запроса.
+ /// Для выбора используется id из запроса. От него вычисляется остаток от деления на количество downstream-сервисов.
+ /// После этого выбирается реплика с соответствующим номером.
+ /// Если из запроса не удалось прочитать id, то используется случайный downstream-сервис.
+ ///
+ /// Контекст http-запроса
+ /// OkResponse с адресос выбранного сервиса, если получилось выбрать downstream-сервис, или
+ /// ErrorResponse, если определить downstream-сервис не удалось
+ ///
+ public async Task> LeaseAsync(HttpContext context)
+ {
+ var availableServices = await services.Invoke();
+ if (availableServices.Count == 0)
+ {
+ logger.LogError("No services available");
+ return new ErrorResponse(
+ new UnableToFindDownstreamRouteError(context.Request.Path.Value, context.Request.Method));
+ }
+
+ var idStr = context.Request.Query["id"].FirstOrDefault("error");
+ if (!int.TryParse(idStr, out var index))
+ {
+ logger.LogWarning($"Could not parse id: {idStr}, random replica will be selected", idStr);
+ index = Random.Shared.Next(availableServices.Count);
+ }
+
+ index %= availableServices.Count;
+ if (index < 0)
+ {
+ index += availableServices.Count;
+ }
+
+ logger.LogInformation($"Using replica with index: {index}", index);
+
+ return new OkResponse(availableServices[index].HostAndPort);
+ }
+
+ ///
+ /// Метод предназначен для уведомления о завершении запроса к downstream-сервису.
+ /// В query based балансировке не используется.
+ ///
+ /// Адрес downstream-сервиса
+ public void Release(ServiceHostAndPort hostAndPort) { }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.ApiGateway/appsettings.Development.json b/CompanyEmployee.ApiGateway/appsettings.Development.json
new file mode 100644
index 00000000..0c208ae9
--- /dev/null
+++ b/CompanyEmployee.ApiGateway/appsettings.Development.json
@@ -0,0 +1,8 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ }
+}
diff --git a/CompanyEmployee.ApiGateway/appsettings.json b/CompanyEmployee.ApiGateway/appsettings.json
new file mode 100644
index 00000000..10f68b8c
--- /dev/null
+++ b/CompanyEmployee.ApiGateway/appsettings.json
@@ -0,0 +1,9 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ },
+ "AllowedHosts": "*"
+}
diff --git a/CompanyEmployee.ApiGateway/ocelotSettings.json b/CompanyEmployee.ApiGateway/ocelotSettings.json
new file mode 100644
index 00000000..316473ec
--- /dev/null
+++ b/CompanyEmployee.ApiGateway/ocelotSettings.json
@@ -0,0 +1,20 @@
+{
+ "Routes": [
+ {
+ "DownstreamPathTemplate": "/company-employee",
+ "DownstreamScheme": "https",
+ "DownstreamHostAndPorts": [
+ { "Host": "localhost", "Port": 7301 },
+ { "Host": "localhost", "Port": 7302 },
+ { "Host": "localhost", "Port": 7303 },
+ { "Host": "localhost", "Port": 7304 },
+ { "Host": "localhost", "Port": 7305 }
+ ],
+ "UpstreamPathTemplate": "/company-employee",
+ "UpstreamHttpMethod": [ "GET" ],
+ "LoadBalancerOptions": {
+ "Type": "QueryBasedLoadBalancer"
+ }
+ }
+ ]
+}
\ No newline at end of file
diff --git a/CompanyEmployee.AppHost/AppHost.cs b/CompanyEmployee.AppHost/AppHost.cs
new file mode 100644
index 00000000..7856cbb3
--- /dev/null
+++ b/CompanyEmployee.AppHost/AppHost.cs
@@ -0,0 +1,66 @@
+using Amazon;
+using Aspire.Hosting.LocalStack.Container;
+
+var builder = DistributedApplication.CreateBuilder(args);
+
+var cache = builder.AddRedis("cache")
+ .WithRedisInsight(containerName: "redis-insight");
+
+var gateway = builder.AddProject("api-gateway")
+ .WithEndpoint("https", e => e.Port = 7200)
+ .WithExternalHttpEndpoints();
+
+var awsConfig = builder.AddAWSSDKConfig()
+ .WithProfile("default")
+ .WithRegion(RegionEndpoint.EUCentral1);
+
+var localstack = builder
+ .AddLocalStack("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("resources", "CloudFormation/company-employee-template-sns.yaml", "company-employee")
+ .WithReference(awsConfig);
+
+const int startGeneratorPort = 7301;
+for (var i = 0; i < 5; ++i)
+{
+ var generator = builder.AddProject($"generator-{i}")
+ .WithEndpoint("https", e => e.Port = startGeneratorPort + i)
+ .WithReference(cache)
+ .WithReference(awsResources)
+ .WithEnvironment("Settings__MessageBroker", "SNS")
+ .WaitFor(cache)
+ .WaitFor(awsResources);
+
+ gateway.WaitFor(generator);
+}
+
+var minio = builder.AddMinioContainer("minio");
+
+var eventSink = builder.AddProject("event-sink")
+ .WithReference(awsResources)
+ .WithEnvironment("Settings__MessageBroker", "SNS")
+ .WithEnvironment("Settings__S3Hosting", "Minio")
+ .WithEnvironment("AWS__Resources__SNSUrl", "http://host.docker.internal:5225/api/sns")
+ .WithEnvironment("AWS__Resources__MinioBucketName", "company-employee-bucket")
+ .WithReference(minio)
+ .WaitFor(minio)
+ .WaitFor(awsResources);
+
+var client = builder.AddProject("client")
+ .WithReference(gateway)
+ .WaitFor(gateway);
+
+builder.UseLocalStack(localstack);
+
+builder.Build().Run();
\ No newline at end of file
diff --git a/CompanyEmployee.AppHost/CompanyEmployee.AppHost.csproj b/CompanyEmployee.AppHost/CompanyEmployee.AppHost.csproj
new file mode 100644
index 00000000..75f409ff
--- /dev/null
+++ b/CompanyEmployee.AppHost/CompanyEmployee.AppHost.csproj
@@ -0,0 +1,33 @@
+
+
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+ 9511602b-1df2-46c9-8aeb-4a3bacf565a2
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Always
+
+
+
+
diff --git a/CompanyEmployee.AppHost/Properties/launchSettings.json b/CompanyEmployee.AppHost/Properties/launchSettings.json
new file mode 100644
index 00000000..4fac06bc
--- /dev/null
+++ b/CompanyEmployee.AppHost/Properties/launchSettings.json
@@ -0,0 +1,31 @@
+{
+ "$schema": "https://json.schemastore.org/launchsettings.json",
+ "profiles": {
+ "https": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "applicationUrl": "https://localhost:17150;http://localhost:15212",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development",
+ "DOTNET_ENVIRONMENT": "Development",
+ "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21292",
+ "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23155",
+ "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22112"
+ }
+ },
+ "http": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "applicationUrl": "http://localhost:15212",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development",
+ "DOTNET_ENVIRONMENT": "Development",
+ "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19225",
+ "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18266",
+ "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20084"
+ }
+ }
+ }
+}
diff --git a/CompanyEmployee.AppHost/appsettings.Development.json b/CompanyEmployee.AppHost/appsettings.Development.json
new file mode 100644
index 00000000..0c208ae9
--- /dev/null
+++ b/CompanyEmployee.AppHost/appsettings.Development.json
@@ -0,0 +1,8 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ }
+}
diff --git a/CompanyEmployee.AppHost/appsettings.json b/CompanyEmployee.AppHost/appsettings.json
new file mode 100644
index 00000000..a6b256bb
--- /dev/null
+++ b/CompanyEmployee.AppHost/appsettings.json
@@ -0,0 +1,12 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning",
+ "Aspire.Hosting.Dcp": "Warning"
+ }
+ },
+ "LocalStack": {
+ "UseLocalStack": true
+ }
+}
diff --git a/CompanyEmployee.EventSink/CompanyEmployee.EventSink.csproj b/CompanyEmployee.EventSink/CompanyEmployee.EventSink.csproj
new file mode 100644
index 00000000..e46a7122
--- /dev/null
+++ b/CompanyEmployee.EventSink/CompanyEmployee.EventSink.csproj
@@ -0,0 +1,21 @@
+
+
+
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CompanyEmployee.EventSink/Controller/S3StorageController.cs b/CompanyEmployee.EventSink/Controller/S3StorageController.cs
new file mode 100644
index 00000000..b543a488
--- /dev/null
+++ b/CompanyEmployee.EventSink/Controller/S3StorageController.cs
@@ -0,0 +1,68 @@
+using System.Text;
+using System.Text.Json.Nodes;
+using CompanyEmployee.EventSink.S3;
+using Microsoft.AspNetCore.Mvc;
+
+namespace CompanyEmployee.EventSink.Controller;
+
+///
+/// Контроллер для взаимодейсвия с S3
+///
+/// Служба для работы с S3
+/// Логгер
+[ApiController]
+[Route("api/s3")]
+public class S3StorageController(IS3Service s3Service, 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 s3Service.GetFileList();
+ 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 BadRequest(ex);
+ }
+ }
+
+ ///
+ /// Получает строковое представление хранящегося в 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 s3Service.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 BadRequest(ex);
+ }
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.EventSink/Controller/SnsSubscriberController.cs b/CompanyEmployee.EventSink/Controller/SnsSubscriberController.cs
new file mode 100644
index 00000000..fa6b9939
--- /dev/null
+++ b/CompanyEmployee.EventSink/Controller/SnsSubscriberController.cs
@@ -0,0 +1,68 @@
+using System.Text;
+using Amazon.SimpleNotificationService.Util;
+using CompanyEmployee.EventSink.S3;
+using Microsoft.AspNetCore.Mvc;
+
+namespace CompanyEmployee.EventSink.Controller;
+
+[ApiController]
+[Route("api/sns")]
+public class SnsSubscriberController(IS3Service s3Service, 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 s3Service.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/CompanyEmployee.EventSink/Messaging/SnsSubscriptionService.cs b/CompanyEmployee.EventSink/Messaging/SnsSubscriptionService.cs
new file mode 100644
index 00000000..31242f46
--- /dev/null
+++ b/CompanyEmployee.EventSink/Messaging/SnsSubscriptionService.cs
@@ -0,0 +1,50 @@
+using Amazon.SimpleNotificationService;
+using Amazon.SimpleNotificationService.Model;
+using System.Net;
+
+namespace CompanyEmployee.EventSink.Messaging;
+
+///
+/// Служба для подписки на SNS на старте приложения
+///
+/// Клиент SNS
+/// Конфигурация
+/// Логгер
+public class SnsSubscriptionService(
+ IAmazonSimpleNotificationService snsClient,
+ IConfiguration configuration,
+ ILogger logger)
+{
+ ///
+ /// Уникальный идентификатор топика 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 subscride request for {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}", _topicArn);
+ }
+
+ else
+ {
+ logger.LogInformation("Subscription request for {topic} is successful, waiting for confirmation", _topicArn);
+ }
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.EventSink/Program.cs b/CompanyEmployee.EventSink/Program.cs
new file mode 100644
index 00000000..553c7fe8
--- /dev/null
+++ b/CompanyEmployee.EventSink/Program.cs
@@ -0,0 +1,42 @@
+using Amazon.SimpleNotificationService;
+using CompanyEmployee.EventSink.Messaging;
+using CompanyEmployee.EventSink.S3;
+using CompanyEmployee.ServiceDefaults;
+using LocalStack.Client.Extensions;
+
+var builder = WebApplication.CreateBuilder(args);
+
+builder.AddServiceDefaults();
+
+builder.Services.AddControllers();
+builder.Services.AddEndpointsApiExplorer();
+builder.Services.AddSwaggerGen();
+
+builder.Services.AddLocalStack(builder.Configuration);
+builder.Services.AddAwsService();
+builder.AddMinioClient("minio");
+
+builder.Services.AddSingleton();
+builder.Services.AddSingleton();
+
+var app = builder.Build();
+
+using (var scope = app.Services.CreateScope())
+{
+ var snsService = scope.ServiceProvider.GetRequiredService();
+ var s3Service = scope.ServiceProvider.GetRequiredService();
+
+ await snsService.SubscribeEndpoint();
+ await s3Service.EnsureBucketExists();
+}
+
+app.MapDefaultEndpoints();
+
+if (app.Environment.IsDevelopment())
+{
+ app.UseSwagger();
+ app.UseSwaggerUI();
+}
+
+app.MapControllers();
+app.Run();
\ No newline at end of file
diff --git a/CompanyEmployee.EventSink/Properties/launchSettings.json b/CompanyEmployee.EventSink/Properties/launchSettings.json
new file mode 100644
index 00000000..72fdf7cc
--- /dev/null
+++ b/CompanyEmployee.EventSink/Properties/launchSettings.json
@@ -0,0 +1,41 @@
+{
+ "$schema": "http://json.schemastore.org/launchsettings.json",
+ "iisSettings": {
+ "windowsAuthentication": false,
+ "anonymousAuthentication": true,
+ "iisExpress": {
+ "applicationUrl": "http://localhost:48738",
+ "sslPort": 44332
+ }
+ },
+ "profiles": {
+ "http": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "launchUrl": "swagger",
+ "applicationUrl": "http://localhost:5225",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "https": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "launchUrl": "swagger",
+ "applicationUrl": "https://localhost:7252;http://localhost:5225",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "IIS Express": {
+ "commandName": "IISExpress",
+ "launchBrowser": true,
+ "launchUrl": "swagger",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ }
+ }
+}
diff --git a/CompanyEmployee.EventSink/S3/IS3Service.cs b/CompanyEmployee.EventSink/S3/IS3Service.cs
new file mode 100644
index 00000000..21143be2
--- /dev/null
+++ b/CompanyEmployee.EventSink/S3/IS3Service.cs
@@ -0,0 +1,33 @@
+using System.Text.Json.Nodes;
+
+namespace CompanyEmployee.EventSink.S3;
+
+///
+/// Интерфейс службы для манипуляции файлами в объектном хранилище
+///
+public interface IS3Service
+{
+ ///
+ /// Отправляет файл в хранилище
+ ///
+ /// Строковая репрезентация сохраняемого файла
+ public Task UploadFile(string fileData);
+
+ ///
+ /// Получает список всех файлов из хранилища
+ ///
+ /// Список путей к файлам
+ public Task> GetFileList();
+
+ ///
+ /// Получает строковую репрезентацию файла из хранилища
+ ///
+ /// Путь к файлу в бакете
+ /// Строковая репрезентация прочтенного файла
+ public Task DownloadFile(string filePath);
+
+ ///
+ /// Создает S3 бакет при необходимости
+ ///
+ public Task EnsureBucketExists();
+}
\ No newline at end of file
diff --git a/CompanyEmployee.EventSink/S3/S3MinioService.cs b/CompanyEmployee.EventSink/S3/S3MinioService.cs
new file mode 100644
index 00000000..e6f001fe
--- /dev/null
+++ b/CompanyEmployee.EventSink/S3/S3MinioService.cs
@@ -0,0 +1,131 @@
+using System.Net;
+using System.Text;
+using System.Text.Json.Nodes;
+using Minio;
+using Minio.DataModel.Args;
+
+namespace CompanyEmployee.EventSink.S3;
+
+///
+/// Cлужба, реализующая интерфейс IS3Service для манипуляции файлами в объектном хранилище
+///
+/// S3 клиент
+/// Конфигурация
+/// Логер
+public class S3MinioService(IMinioClient client, IConfiguration configuration, ILogger logger)
+ : IS3Service
+{
+ private readonly string _bucketName = configuration["AWS:Resources:MinioBucketName"]
+ ?? throw new KeyNotFoundException("S3 bucket name was not found in configuration");
+
+ ///
+ public async Task> GetFileList()
+ {
+ var request = new ListObjectsArgs()
+ .WithBucket(_bucketName)
+ .WithPrefix("")
+ .WithRecursive(true);
+ logger.LogInformation("Began listing files in {bucket}", _bucketName);
+ var responseList = client.ListObjectsEnumAsync(request);
+
+ if (responseList == null)
+ logger.LogWarning("Received null response from {bucket}", _bucketName);
+
+ var list = new List();
+ await foreach (var response in responseList!)
+ list.Add(response.Key);
+ return list;
+ }
+
+ ///
+ public async Task UploadFile(string 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");
+
+ var bytes = Encoding.UTF8.GetBytes(fileData);
+ using var stream = new MemoryStream(bytes);
+ stream.Seek(0, SeekOrigin.Begin);
+
+ logger.LogInformation("Began uploading company employee {file} onto {bucket}", id, _bucketName);
+
+ var request = new PutObjectArgs()
+ .WithBucket(_bucketName)
+ .WithStreamData(stream)
+ .WithObjectSize(bytes.Length)
+ .WithObject($"company_employee_{id}.json");
+
+ var response = await client.PutObjectAsync(request);
+
+ if (response.ResponseStatusCode != HttpStatusCode.OK)
+ {
+ logger.LogError("Failed to upload employee {file}: {code}", id, response.ResponseStatusCode);
+ return false;
+ }
+ logger.LogInformation("Finished uploading employee {file} to {bucket}", id, _bucketName);
+ return true;
+ }
+
+ ///
+ public async Task DownloadFile(string filePath)
+ {
+ logger.LogInformation("Began downloading {file} from {bucket}", filePath, _bucketName);
+ try
+ {
+ var memoryStream = new MemoryStream();
+
+ var request = new GetObjectArgs()
+ .WithBucket(_bucketName)
+ .WithObject(filePath)
+ .WithCallbackStream(async (stream, cancellationToken) =>
+ {
+ await stream.CopyToAsync(memoryStream, cancellationToken);
+ memoryStream.Seek(0, SeekOrigin.Begin);
+ });
+
+ var response = await client.GetObjectAsync(request);
+
+ if (response == null)
+ {
+ logger.LogError("Failed to download {file}", filePath);
+ throw new InvalidOperationException($"Error occurred downloading {filePath} - object is null");
+ }
+
+ using var reader = new StreamReader(memoryStream, Encoding.UTF8);
+
+ return JsonNode.Parse(reader.ReadToEnd()) ??
+ throw new InvalidOperationException($"Downloaded document is not a valid JSON");
+ }
+ catch (Exception ex)
+ {
+ logger.LogError(ex, "Exception occurred during {file} downloading ", filePath);
+ throw;
+ }
+ }
+
+ ///
+ public async Task EnsureBucketExists()
+ {
+ logger.LogInformation("Checking whether {bucket} exists", _bucketName);
+ try
+ {
+ var request = new BucketExistsArgs().WithBucket(_bucketName);
+
+ var exists = await client.BucketExistsAsync(request);
+ if (!exists)
+ {
+ logger.LogInformation("Creating {bucket}", _bucketName);
+ var createRequest = new MakeBucketArgs().WithBucket(_bucketName);
+ await client.MakeBucketAsync(createRequest);
+
+ return;
+ }
+ logger.LogInformation("{bucket} exists", _bucketName);
+ }
+ catch (Exception ex)
+ {
+ logger.LogError(ex, "Unhandled exception occurred during {bucket} check", _bucketName);
+ throw;
+ }
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.EventSink/appsettings.Development.json b/CompanyEmployee.EventSink/appsettings.Development.json
new file mode 100644
index 00000000..0c208ae9
--- /dev/null
+++ b/CompanyEmployee.EventSink/appsettings.Development.json
@@ -0,0 +1,8 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ }
+}
diff --git a/CompanyEmployee.EventSink/appsettings.json b/CompanyEmployee.EventSink/appsettings.json
new file mode 100644
index 00000000..10f68b8c
--- /dev/null
+++ b/CompanyEmployee.EventSink/appsettings.json
@@ -0,0 +1,9 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ },
+ "AllowedHosts": "*"
+}
diff --git a/CompanyEmployee.Generator/CompanyEmployee.Generator.csproj b/CompanyEmployee.Generator/CompanyEmployee.Generator.csproj
new file mode 100644
index 00000000..78ed7057
--- /dev/null
+++ b/CompanyEmployee.Generator/CompanyEmployee.Generator.csproj
@@ -0,0 +1,22 @@
+
+
+
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CompanyEmployee.Generator/Controller/CompanyEmployeeController.cs b/CompanyEmployee.Generator/Controller/CompanyEmployeeController.cs
new file mode 100644
index 00000000..1cf0bee3
--- /dev/null
+++ b/CompanyEmployee.Generator/Controller/CompanyEmployeeController.cs
@@ -0,0 +1,41 @@
+using CompanyEmployee.Generator.Dto;
+using CompanyEmployee.Generator.Service;
+using Microsoft.AspNetCore.Mvc;
+
+namespace CompanyEmployee.Generator.Controller;
+
+///
+/// Контроллер для получения сотрудника компании по id
+///
+/// Сервис получения сотрудника компании
+/// Логгер
+[ApiController]
+[Route("company-employee")]
+public class CompanyEmployeeController(
+ ICompanyEmployeeService service,
+ ILogger logger
+ ) : ControllerBase
+{
+ ///
+ /// Метод для получения сотрудника компании по id
+ ///
+ /// Идентификатор сотрудника
+ /// Токен отмены запроса
+ /// DTO сотрудника компании
+ /// Успешное получение сотрудника
+ /// Некорректный id
+ [HttpGet]
+ [ProducesResponseType(typeof(CompanyEmployeeDto), StatusCodes.Status200OK)]
+ [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
+ public async Task> GetById([FromQuery] int id, CancellationToken cancellationToken)
+ {
+ if (id < 0)
+ {
+ return BadRequest("Id must be greater or equal than 0");
+ }
+ logger.LogInformation("HTTP GET /company-employee, id: {employeeId}", id);
+
+ var employee = await service.GetByIdAsync(id, cancellationToken);
+ return Ok(employee);
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/Dto/CompanyEmployeeDto.cs b/CompanyEmployee.Generator/Dto/CompanyEmployeeDto.cs
new file mode 100644
index 00000000..51192f96
--- /dev/null
+++ b/CompanyEmployee.Generator/Dto/CompanyEmployeeDto.cs
@@ -0,0 +1,57 @@
+namespace CompanyEmployee.Generator.Dto;
+
+///
+/// Информация о сотруднике компании
+///
+public class CompanyEmployeeDto
+{
+ ///
+ /// Идентификатор сотрудника в системе
+ ///
+ public required int Id { get; init; }
+
+ ///
+ /// ФИО
+ ///
+ public required string FullName { get; init; }
+
+ ///
+ /// Должность
+ ///
+ public required string Position { get; init; }
+
+ ///
+ /// Отдел
+ ///
+ public required string Department { get; init; }
+
+ ///
+ /// Дата приема
+ ///
+ public required DateOnly EmploymentDate { get; init; }
+
+ ///
+ /// Оклад
+ ///
+ public required decimal Salary { get; init; }
+
+ ///
+ /// Электронная почта
+ ///
+ public required string Email { get; init; }
+
+ ///
+ /// Номер телефона
+ ///
+ public required string PhoneNumber { get; init; }
+
+ ///
+ /// Индикатор увольнения
+ ///
+ public required bool DismissalFlag { get; init; }
+
+ ///
+ /// Дата увольнения
+ ///
+ public DateOnly? DismissalDate { get; init; }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/Program.cs b/CompanyEmployee.Generator/Program.cs
new file mode 100644
index 00000000..11592601
--- /dev/null
+++ b/CompanyEmployee.Generator/Program.cs
@@ -0,0 +1,37 @@
+using Amazon.SimpleNotificationService;
+using CompanyEmployee.Generator.Messaging;
+using CompanyEmployee.ServiceDefaults;
+using CompanyEmployee.Generator.Service;
+using LocalStack.Client.Extensions;
+
+
+var builder = WebApplication.CreateBuilder(args);
+
+builder.AddServiceDefaults();
+
+builder.Services.AddControllers();
+builder.Services.AddEndpointsApiExplorer();
+builder.Services.AddSwaggerGen();
+
+builder.AddRedisDistributedCache(connectionName: "cache");
+
+builder.Services.AddSingleton();
+builder.Services.AddSingleton();
+builder.Services.AddSingleton();
+
+builder.Services.AddLocalStack(builder.Configuration);
+builder.Services.AddAwsService();
+
+var app = builder.Build();
+
+app.MapDefaultEndpoints();
+
+if (app.Environment.IsDevelopment())
+{
+ app.UseSwagger();
+ app.UseSwaggerUI();
+}
+
+app.MapControllers();
+
+app.Run();
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/Properties/launchSettings.json b/CompanyEmployee.Generator/Properties/launchSettings.json
new file mode 100644
index 00000000..ea64f59f
--- /dev/null
+++ b/CompanyEmployee.Generator/Properties/launchSettings.json
@@ -0,0 +1,41 @@
+{
+ "$schema": "http://json.schemastore.org/launchsettings.json",
+ "iisSettings": {
+ "windowsAuthentication": false,
+ "anonymousAuthentication": true,
+ "iisExpress": {
+ "applicationUrl": "http://localhost:6127",
+ "sslPort": 44325
+ }
+ },
+ "profiles": {
+ "http": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "launchUrl": "swagger",
+ "applicationUrl": "http://localhost:5291",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "https": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "launchUrl": "swagger",
+ "applicationUrl": "https://localhost:7240;http://localhost:5291",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "IIS Express": {
+ "commandName": "IISExpress",
+ "launchBrowser": true,
+ "launchUrl": "swagger",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/Service/CompanyEmployeeGenerator.cs b/CompanyEmployee.Generator/Service/CompanyEmployeeGenerator.cs
new file mode 100644
index 00000000..039b7bcf
--- /dev/null
+++ b/CompanyEmployee.Generator/Service/CompanyEmployeeGenerator.cs
@@ -0,0 +1,58 @@
+using Bogus;
+using Bogus.DataSets;
+using CompanyEmployee.Generator.Dto;
+
+namespace CompanyEmployee.Generator.Service;
+
+///
+/// Генератор сотрудника по идентификатору
+///
+/// Логгер
+public class CompanyEmployeeGenerator(
+ ILogger logger
+ ) : ICompanyEmployeeGenerator
+{
+ private static readonly string[] _position = ["Developer", "Manager", "Analyst", "QA"];
+
+ private static readonly string[] _grade = ["Junior", "Middle", "Senior"];
+
+ ///
+ public CompanyEmployeeDto Generate(int employeeId)
+ {
+ var faker = new Faker("ru")
+ .RuleFor(e => e.Id, _ => employeeId)
+ .RuleFor(e => e.FullName, f =>
+ {
+ var gender = f.PickRandom(Enum.GetValues(typeof(Name.Gender)).Cast().ToArray());
+
+ return $"{f.Name.LastName(gender)} {f.Name.FirstName(gender)} " +
+ $"{f.Name.FirstName(gender)}{(gender == Name.Gender.Male ? "еевич" : "еевна")}";
+ })
+ .RuleFor(e => e.Position, f => $"{f.PickRandom(_position)} {f.PickRandom(_grade)}")
+ .RuleFor(e => e.Department, f => f.Commerce.Department())
+ .RuleFor(e => e.EmploymentDate, f => f.Date.PastDateOnly(10))
+ .RuleFor(e => e.Salary, (f, e) =>
+ {
+ var baseSalary = f.Random.Decimal(100, 200);
+
+ for (var i = 0; i < _grade.Length; ++i)
+ {
+ if (e.Position.Contains(_grade[i]))
+ {
+ baseSalary *= (i + 1);
+ break;
+ }
+ }
+
+ return Math.Round(baseSalary, 2);
+ })
+ .RuleFor(e => e.Email, f => f.Internet.Email())
+ .RuleFor(e => e.PhoneNumber, f => f.Phone.PhoneNumber("+7(###)###-##-##"))
+ .RuleFor(e => e.DismissalFlag, f => f.Random.Bool(0.5f))
+ .RuleFor(e => e.DismissalDate, (f, e) => !e.DismissalFlag ? null :
+ f.Date.BetweenDateOnly(e.EmploymentDate, DateOnly.FromDateTime(DateTime.UtcNow)));
+
+ logger.LogInformation("Generated employee with id {employeeId}", employeeId);
+ return faker.Generate();
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/Service/CompanyEmployeeService.cs b/CompanyEmployee.Generator/Service/CompanyEmployeeService.cs
new file mode 100644
index 00000000..f79ec7b2
--- /dev/null
+++ b/CompanyEmployee.Generator/Service/CompanyEmployeeService.cs
@@ -0,0 +1,76 @@
+using System.Text.Json;
+using CompanyEmployee.Generator.Dto;
+using CompanyEmployee.Generator.Messaging;
+using Microsoft.Extensions.Caching.Distributed;
+
+namespace CompanyEmployee.Generator.Service;
+
+///
+/// Сервис получения сотрудника компании
+///
+/// Генератор сотрудника по идентификатору
+/// Служба отправки сообщений в брокер
+/// Сервис кэширования
+/// Конфигурация приложения
+/// Логгер
+public class CompanyEmployeeService(
+ ICompanyEmployeeGenerator generator,
+ IProducerService producerService,
+ IDistributedCache cache,
+ IConfiguration configuration,
+ ILogger logger
+ ) : ICompanyEmployeeService
+{
+ private static readonly string _companyEmployeeCachePrefix = "company-employee:";
+
+ private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
+
+ ///
+ public async Task GetByIdAsync(int employeeId, CancellationToken token)
+ {
+ var cacheKey = _companyEmployeeCachePrefix + employeeId;
+ var cachedValue = await cache.GetStringAsync(cacheKey, token);
+
+ CompanyEmployeeDto companyEmployee;
+
+ if (!string.IsNullOrEmpty(cachedValue))
+ {
+ logger.LogInformation("Read from cache, key: {cacheKey}", cacheKey);
+ try
+ {
+ companyEmployee = JsonSerializer.Deserialize(cachedValue, _jsonOptions);
+ if (companyEmployee != null)
+ {
+ return companyEmployee;
+ }
+ }
+ catch (Exception ex)
+ {
+ logger.LogWarning(ex, "Error deserializing cached employee, key: {cacheKey}", cacheKey);
+ }
+ }
+
+ companyEmployee = generator.Generate(employeeId);
+
+ await producerService.SendMessage(companyEmployee);
+
+ var ttlSeconds = configuration.GetValue("CompanyEmployeeCache:TtlSeconds", 600);
+ var cacheOpts = new DistributedCacheEntryOptions
+ {
+ AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(ttlSeconds)
+ };
+
+ try
+ {
+ await cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(companyEmployee, _jsonOptions), cacheOpts,
+ token);
+ logger.LogInformation("Write to cache, key: {cacheKey}", cacheKey);
+ }
+ catch (Exception ex)
+ {
+ logger.LogWarning(ex, "Error in caching companyEmployee, key: {cacheKey}", cacheKey);
+ }
+
+ return companyEmployee;
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/Service/ICompanyEmployeeGenerator.cs b/CompanyEmployee.Generator/Service/ICompanyEmployeeGenerator.cs
new file mode 100644
index 00000000..b2332f3e
--- /dev/null
+++ b/CompanyEmployee.Generator/Service/ICompanyEmployeeGenerator.cs
@@ -0,0 +1,17 @@
+using CompanyEmployee.Generator.Dto;
+
+namespace CompanyEmployee.Generator.Service;
+
+///
+/// Интерфейс генератора сотрудника по идентификатору
+///
+public interface ICompanyEmployeeGenerator
+{
+ ///
+ /// Метод для генерации сотрудника по идентификатору
+ ///
+ /// Идентификатор сотрудника
+ /// DTO сотрудника компании
+ public CompanyEmployeeDto Generate(int employeeId);
+
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/Service/ICompanyEmployeeService.cs b/CompanyEmployee.Generator/Service/ICompanyEmployeeService.cs
new file mode 100644
index 00000000..e165687e
--- /dev/null
+++ b/CompanyEmployee.Generator/Service/ICompanyEmployeeService.cs
@@ -0,0 +1,18 @@
+using CompanyEmployee.Generator.Dto;
+
+namespace CompanyEmployee.Generator.Service;
+
+///
+/// Интерфейс сервиса получения сотрудника компании
+///
+public interface ICompanyEmployeeService
+{
+ ///
+ /// Метод получения сотрудника компании по идентификатору
+ /// Сначала пытается найти сотрудника в кэше, если не находит, то генерирует нового и записывает его в кэш
+ ///
+ /// Идентификатор сотрудника
+ /// Токен отмены запроса
+ /// DTO сотрудника компании
+ public Task GetByIdAsync(int employeeId, CancellationToken token);
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/appsettings.Development.json b/CompanyEmployee.Generator/appsettings.Development.json
new file mode 100644
index 00000000..cd7d0bc9
--- /dev/null
+++ b/CompanyEmployee.Generator/appsettings.Development.json
@@ -0,0 +1,8 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Generator/appsettings.json b/CompanyEmployee.Generator/appsettings.json
new file mode 100644
index 00000000..e6d8e7b2
--- /dev/null
+++ b/CompanyEmployee.Generator/appsettings.json
@@ -0,0 +1,13 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ },
+ "CompanyEmployeeCache": {
+ "Enabled": true,
+ "TtlSeconds": 600
+ },
+ "AllowedHosts": "*"
+}
\ No newline at end of file
diff --git a/CompanyEmployee.ServiceDefaults/CompanyEmployee.ServiceDefaults.csproj b/CompanyEmployee.ServiceDefaults/CompanyEmployee.ServiceDefaults.csproj
new file mode 100644
index 00000000..1214e27d
--- /dev/null
+++ b/CompanyEmployee.ServiceDefaults/CompanyEmployee.ServiceDefaults.csproj
@@ -0,0 +1,22 @@
+
+
+
+ net8.0
+ enable
+ enable
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CompanyEmployee.ServiceDefaults/Extensions.cs b/CompanyEmployee.ServiceDefaults/Extensions.cs
new file mode 100644
index 00000000..0c76f9c8
--- /dev/null
+++ b/CompanyEmployee.ServiceDefaults/Extensions.cs
@@ -0,0 +1,130 @@
+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 CompanyEmployee.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;
+ }
+}
\ No newline at end of file
diff --git a/CompanyEmployee.Tests/CompanyEmployee.Tests.csproj b/CompanyEmployee.Tests/CompanyEmployee.Tests.csproj
new file mode 100644
index 00000000..51824867
--- /dev/null
+++ b/CompanyEmployee.Tests/CompanyEmployee.Tests.csproj
@@ -0,0 +1,36 @@
+
+
+
+ net8.0
+ enable
+ enable
+
+ false
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CompanyEmployee.Tests/IntegrationTests.cs b/CompanyEmployee.Tests/IntegrationTests.cs
new file mode 100644
index 00000000..875677cc
--- /dev/null
+++ b/CompanyEmployee.Tests/IntegrationTests.cs
@@ -0,0 +1,137 @@
+using System.Text.Json;
+using Aspire.Hosting;
+using CompanyEmployee.Generator.Dto;
+using Microsoft.Extensions.Logging;
+using Projects;
+using Xunit.Abstractions;
+
+namespace CompanyEmployee.Tests;
+
+///
+/// Интеграционные тесты для проверки микросервисного пайплайна
+///
+/// Служба журналирования юнит-тестов
+public class IntegrationTests(ITestOutputHelper output) : IAsyncLifetime
+{
+ private static readonly JsonSerializerOptions _jsonOptions = new()
+ {
+ PropertyNameCaseInsensitive = true
+ };
+
+ private DistributedApplication? _app;
+ private HttpClient? _gatewayClient;
+ private HttpClient? _s3Client;
+
+ ///
+ public async Task InitializeAsync()
+ {
+ var cancellationToken = CancellationToken.None;
+
+ var builder = await DistributedApplicationTestingBuilder.CreateAsync(cancellationToken);
+ builder.Configuration["DcpPublisher:RandomizePorts"] = "false";
+ builder.Services.AddLogging(logging =>
+ {
+ logging.AddXUnit(output);
+ logging.SetMinimumLevel(LogLevel.Debug);
+ logging.AddFilter("Aspire.Hosting.Dcp", LogLevel.Debug);
+ logging.AddFilter("Aspire.Hosting", LogLevel.Debug);
+ });
+
+ _app = await builder.BuildAsync(cancellationToken);
+ await _app.StartAsync(cancellationToken);
+
+ _gatewayClient = _app.CreateHttpClient("api-gateway", "http");
+ _s3Client = _app.CreateHttpClient("event-sink", "http");
+ }
+
+ ///
+ public async Task DisposeAsync()
+ {
+ await _app!.StopAsync();
+ await _app.DisposeAsync();
+ }
+
+ ///
+ /// Проверка положительного сценария:
+ /// 1. Запрос возвращает сгенерированного сотрудника
+ /// 2. Идентичный сотрудник сохраняется в S3
+ ///
+ [Fact]
+ public async Task SuccessPipelineTest()
+ {
+ var id = 0;
+
+ var response = await _gatewayClient!.GetAsync($"/company-employee?id={id}");
+ var generatedEmployee =
+ JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), _jsonOptions);
+
+ var s3Employee = await GetEmployeeFromS3(id);
+
+ Assert.NotNull(generatedEmployee);
+ Assert.NotNull(s3Employee);
+ Assert.Equivalent(generatedEmployee, s3Employee, strict: true);
+ }
+
+ ///
+ /// Проверка обработки некорректных id
+ ///
+ /// id сотрудника
+ [Theory]
+ [InlineData("-1")]
+ [InlineData("qwe")]
+ public async Task IncorrectEmployeeIdTest(string id)
+ {
+ var request = $"/company-employee?id={id}";
+
+ var response = await _gatewayClient!.GetAsync(request);
+
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ }
+
+ ///
+ /// Проверка кэширования - при запросах с одинаковым id возвращаются одинаковые сотрудники
+ ///
+ [Fact]
+ public async Task CachingTest()
+ {
+ var id = 0;
+
+ var response = await _gatewayClient!.GetAsync($"/company-employee?id={id}");
+ var firstEmployee =
+ JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), _jsonOptions);
+
+ response = await _gatewayClient!.GetAsync($"/company-employee?id={id}");
+ var secondEmployee =
+ JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), _jsonOptions);
+
+ Assert.NotNull(firstEmployee);
+ Assert.NotNull(secondEmployee);
+ Assert.Equivalent(firstEmployee, secondEmployee);
+ }
+
+ ///
+ /// Получение сотрудника из S3
+ ///
+ /// id сотрудника
+ /// DTO сотрудника компании
+ /// Выбрасывается, если сотрудник не найден в S3
+ private async Task GetEmployeeFromS3(int id)
+ {
+ var endTime = DateTime.UtcNow + TimeSpan.FromSeconds(10);
+ var fileName = $"company_employee_{id}.json";
+
+ while (DateTime.UtcNow < endTime)
+ {
+ var file = await _s3Client!.GetAsync($"/api/s3/{fileName}");
+ if (file.IsSuccessStatusCode)
+ {
+ return JsonSerializer.Deserialize(await file.Content.ReadAsStringAsync(),
+ _jsonOptions);
+ }
+
+ await Task.Delay(500);
+ }
+
+ throw new TimeoutException($"File with id {id} not found in S3");
+ }
+}
\ No newline at end of file