diff --git a/Api.Gateway/Api.Gateway.csproj b/Api.Gateway/Api.Gateway.csproj new file mode 100644 index 00000000..921dca54 --- /dev/null +++ b/Api.Gateway/Api.Gateway.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + + + + + + + + + diff --git a/Api.Gateway/LoadBalancer/WeightedRandom.cs b/Api.Gateway/LoadBalancer/WeightedRandom.cs new file mode 100644 index 00000000..cb12b404 --- /dev/null +++ b/Api.Gateway/LoadBalancer/WeightedRandom.cs @@ -0,0 +1,36 @@ +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.Values; + +namespace Api.Gateway.LoadBalancer; + +/// +/// Балансировка случайным образом с весами +/// +public class WeightedRandom : ILoadBalancer +{ + private readonly Func>> _services = null!; + private static readonly object _locker = new(); + + private readonly int[] _values = null!; + + public string Type => nameof(WeightedRandom); + public WeightedRandom(Func>> services, IConfiguration configuration) + { + _services = services; + var frequencies = configuration.GetSection("LoadBalancer:WeightedRandom:Weights").Get(); + if (frequencies == null || frequencies.Length == 0) + throw new InvalidOperationException("Weights is empty or null. Add weights to configuration"); + _values = [.. Enumerable.Range(0, frequencies.Length).Zip(frequencies, (val, freq) => Enumerable.Repeat(val, freq)).SelectMany(x => x)]; + } + public async Task> LeaseAsync(HttpContext httpContext) + { + var services = await _services.Invoke(); + lock (_locker) + { + Random.Shared.Shuffle(_values); + return new OkResponse(services[_values.First()].HostAndPort); + } + } + public void Release(ServiceHostAndPort hostAndPort) { } +} \ No newline at end of file diff --git a/Api.Gateway/Program.cs b/Api.Gateway/Program.cs new file mode 100644 index 00000000..21889099 --- /dev/null +++ b/Api.Gateway/Program.cs @@ -0,0 +1,29 @@ +using Api.Gateway.LoadBalancer; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +builder.Services.AddServiceDiscovery(); +builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); +builder.Services.AddOcelot() + .AddCustomLoadBalancer((serviceProvider, _, discoveryProvider) => + { + var configuration = serviceProvider.GetRequiredService(); + return new WeightedRandom(discoveryProvider.GetAsync, configuration); + }); + + +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.WithOrigins("https://localhost:5127", "http://localhost:5127", "https://localhost:7282") + .WithMethods("GET") + .AllowAnyHeader(); +})); + +var app = builder.Build(); +app.UseCors(); +await app.UseOcelot(); +app.Run(); diff --git a/Api.Gateway/Properties/launchSettings.json b/Api.Gateway/Properties/launchSettings.json new file mode 100644 index 00000000..2de3e6c5 --- /dev/null +++ b/Api.Gateway/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:42987", + "sslPort": 44379 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5086", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7099;http://localhost:5086", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Api.Gateway/appsettings.Development.json b/Api.Gateway/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Api.Gateway/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Api.Gateway/appsettings.json b/Api.Gateway/appsettings.json new file mode 100644 index 00000000..9699729e --- /dev/null +++ b/Api.Gateway/appsettings.json @@ -0,0 +1,14 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "LoadBalancer": { + "WeightedRandom": { + "Weights": [ 1, 2, 3, 2, 1 ] + } + } +} diff --git a/Api.Gateway/ocelot.json b/Api.Gateway/ocelot.json new file mode 100644 index 00000000..72647816 --- /dev/null +++ b/Api.Gateway/ocelot.json @@ -0,0 +1,23 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/employee", + "UpstreamHttpMethod": [ "GET" ], + "DownstreamPathTemplate": "/employee", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { "Host": "localhost", "Port": 7170 }, + { "Host": "localhost", "Port": 7171 }, + { "Host": "localhost", "Port": 7172 }, + { "Host": "localhost", "Port": 7173 }, + { "Host": "localhost", "Port": 7174 } + ], + "LoadBalancerOptions": { + "Type": "WeightedRandom" + } + } + ], + "GlobalConfiguration": { + "BaseUrl": "https://localhost:7099" + } +} \ No newline at end of file diff --git a/AspireApp.AppHost.Test/AspireApp.AppHost.Test.csproj b/AspireApp.AppHost.Test/AspireApp.AppHost.Test.csproj new file mode 100644 index 00000000..9c070666 --- /dev/null +++ b/AspireApp.AppHost.Test/AspireApp.AppHost.Test.csproj @@ -0,0 +1,40 @@ + + + + net8.0 + enable + enable + false + true + + + + + + + + + + + + + + + + + + + CloudFormation\%(Filename)%(Extension) + PreserveNewest + + + + + + + + + + + + diff --git a/AspireApp.AppHost.Test/IntegrationTest.cs b/AspireApp.AppHost.Test/IntegrationTest.cs new file mode 100644 index 00000000..dbc87c4a --- /dev/null +++ b/AspireApp.AppHost.Test/IntegrationTest.cs @@ -0,0 +1,79 @@ +using Aspire.Hosting; +using Microsoft.Extensions.Logging; +using Service.Api.Entities; +using System.Text.Json; +using Xunit.Abstractions; + +namespace AspireApp.AppHost.Test; + +/// +/// +/// +/// - +public class IntegrationTests(ITestOutputHelper output) : IAsyncLifetime +{ + private IDistributedApplicationTestingBuilder? _builder; + private DistributedApplication? _app; + + /// + public async Task InitializeAsync() + { + var cancellationToken = CancellationToken.None; + _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); + }); + } + + /// + /// , : + /// + /// + /// S3 + /// , + /// + /// + /// + [Theory] + [InlineData("SNS+LocalstackS3")] + public async Task TestPipeline(string envName) + { + var cancellationToken = CancellationToken.None; + _builder!.Environment.EnvironmentName = envName; + _app = await _builder.BuildAsync(cancellationToken); + await _app.StartAsync(cancellationToken); + + var random = new Random(); + var id = random.Next(1, 100); + using var gatewayClient = _app.CreateHttpClient("employee-api-gateway", "http"); + using var gatewayResponse = await gatewayClient!.GetAsync($"/employee?id={id}"); + var apiEmployee = JsonSerializer.Deserialize(await gatewayResponse.Content.ReadAsStringAsync()); + + await Task.Delay(5000); + using var sinkClient = _app.CreateHttpClient("employee-sink", "http"); + using var listResponse = await sinkClient!.GetAsync($"/api/s3"); + var employeeList = JsonSerializer.Deserialize>(await listResponse.Content.ReadAsStringAsync()); + using var s3Response = await sinkClient!.GetAsync($"/api/s3/employee_{id}.json"); + var s3Employee = JsonSerializer.Deserialize(await s3Response.Content.ReadAsStringAsync()); + + Assert.NotNull(employeeList); + Assert.Single(employeeList); + Assert.NotNull(apiEmployee); + Assert.NotNull(s3Employee); + Assert.Equal(id, s3Employee.Id); + Assert.Equivalent(apiEmployee, s3Employee); + } + + /// + public async Task DisposeAsync() + { + await _app!.StopAsync(); + await _app.DisposeAsync(); + await _builder!.DisposeAsync(); + } +} \ No newline at end of file diff --git a/AspireApp/AspireApp.AppHost/AppHost.cs b/AspireApp/AspireApp.AppHost/AppHost.cs new file mode 100644 index 00000000..6bbefcc7 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/AppHost.cs @@ -0,0 +1,57 @@ +using Amazon; +using Aspire.Hosting.LocalStack.Container; + +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("employee-cache") + .WithRedisInsight(containerName: "employee-insight"); + +var gateway = builder.AddProject("employee-api-gateway"); + +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +var localstack = builder + .AddLocalStack("employee-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 cloudFormationTemplate = "CloudFormation/employee-template-sns-s3.yaml"; +var awsResources = builder.AddAWSCloudFormationTemplate("resources", cloudFormationTemplate, "employee") + .WithReference(awsConfig); + +for (var i = 0; i < 5; i++) +{ + var service = builder.AddProject($"employee-api-{i + 1}", launchProfileName: null) + .WithHttpsEndpoint(7170 + i) + .WithReference(cache, "RedisCache") + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SNS") + .WaitFor(cache) + .WaitFor(awsResources); + gateway.WaitFor(service); +} + +builder.AddProject("employee-wasm") + .WaitFor(gateway); + +var sink = builder.AddProject("employee-sink") + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SNS") + .WithEnvironment("Settings__S3Hosting", "Localstack") + .WaitFor(awsResources); + +sink.WithEnvironment("AWS__Resources__SNSUrl", "http://host.docker.internal:5134/api/sns"); + +builder.UseLocalStack(localstack); + +builder.Build().Run(); diff --git a/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj new file mode 100644 index 00000000..37d3eb17 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj @@ -0,0 +1,33 @@ + + + + + + Exe + net8.0 + enable + enable + true + 8acee786-1688-40c1-943e-5186ce386476 + + + + + + + + + + + + + + + + + + Always + + + + diff --git a/AspireApp/AspireApp.AppHost/CloudFormation/employee-template-sns-s3.yaml b/AspireApp/AspireApp.AppHost/CloudFormation/employee-template-sns-s3.yaml new file mode 100644 index 00000000..042e3486 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/CloudFormation/employee-template-sns-s3.yaml @@ -0,0 +1,59 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: 'Cloud formation template for employee project' + +Parameters: + BucketName: + Type: String + Description: Name for the S3 bucket + Default: 'employee-bucket' + + TopicName: + Type: String + Description: Name for the SNS topic + Default: 'employee-topic' + +Resources: + EmployeeBucket: + 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 + + EmployeeTopic: + 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 EmployeeBucket + + S3BucketArn: + Description: ARN of the S3 bucket + Value: !GetAtt EmployeeBucket.Arn + + SNSTopicName: + Description: Name of the SNS topic + Value: !GetAtt EmployeeTopic.TopicName + + SNSTopicArn: + Description: ARN of the SNS topic + Value: !Ref EmployeeTopic \ No newline at end of file diff --git a/AspireApp/AspireApp.AppHost/Properties/launchSettings.json b/AspireApp/AspireApp.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..cafbf414 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "SNS+LocalstackS3": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17289;http://localhost:15028", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "SNS+LocalstackS3", + "DOTNET_ENVIRONMENT": "SNS+LocalstackS3", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21076", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22018" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17096;http://localhost:15155", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21139", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22017" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15155", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19197", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20116" + } + } + } +} diff --git a/AspireApp/AspireApp.AppHost/appsettings.Development.json b/AspireApp/AspireApp.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/AspireApp/AspireApp.AppHost/appsettings.json b/AspireApp/AspireApp.AppHost/appsettings.json new file mode 100644 index 00000000..a6b256bb --- /dev/null +++ b/AspireApp/AspireApp.AppHost/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + }, + "LocalStack": { + "UseLocalStack": true + } +} diff --git a/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj b/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj new file mode 100644 index 00000000..6c036a13 --- /dev/null +++ b/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/AspireApp/AspireApp.ServiceDefaults/Extensions.cs b/AspireApp/AspireApp.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..13151bf4 --- /dev/null +++ b/AspireApp/AspireApp.ServiceDefaults/Extensions.cs @@ -0,0 +1,119 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common .NET 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 +{ + 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() + // 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("/health"); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..a3c88f0f 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №3 "Интеграционное тестирование" + Вариант №32 "Сотрудник компании" + Выполнена Рыженковой Дианой 6512 + Ссылка на форк diff --git a/Client.Wasm/Properties/launchSettings.json b/Client.Wasm/Properties/launchSettings.json index 0d824ea7..60120ec3 100644 --- a/Client.Wasm/Properties/launchSettings.json +++ b/Client.Wasm/Properties/launchSettings.json @@ -12,7 +12,7 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "applicationUrl": "http://localhost:5127", "environmentVariables": { @@ -22,7 +22,7 @@ "https": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "applicationUrl": "https://localhost:7282;http://localhost:5127", "environmentVariables": { @@ -31,7 +31,7 @@ }, "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..4f4cc3c9 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "http://localhost:5086/employee" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..3d56ccb1 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}") = "AspireApp.AppHost", "AspireApp\AspireApp.AppHost\AspireApp.AppHost.csproj", "{2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.ServiceDefaults", "AspireApp\AspireApp.ServiceDefaults\AspireApp.ServiceDefaults.csproj", "{A0BC68FF-8BE6-4035-BF3B-561CB4631B57}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Api", "ServiceApi\Service.Api.csproj", "{888B9F35-3A72-42E9-A176-BA7A19ED878C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.Gateway", "Api.Gateway\Api.Gateway.csproj", "{FE44C324-0DA5-4434-8963-E9A17DC65ED8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Event.Sink", "Event.Sink\Event.Sink.csproj", "{2A278C6D-6FE6-47AE-902A-A3983F081C79}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.AppHost.Test", "AspireApp.AppHost.Test\AspireApp.AppHost.Test.csproj", "{59B4FBEE-512C-429C-9DF0-745A8B55B650}" +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 + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Release|Any CPU.Build.0 = Release|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Release|Any CPU.Build.0 = Release|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Release|Any CPU.Build.0 = Release|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Release|Any CPU.Build.0 = Release|Any CPU + {2A278C6D-6FE6-47AE-902A-A3983F081C79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2A278C6D-6FE6-47AE-902A-A3983F081C79}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2A278C6D-6FE6-47AE-902A-A3983F081C79}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2A278C6D-6FE6-47AE-902A-A3983F081C79}.Release|Any CPU.Build.0 = Release|Any CPU + {59B4FBEE-512C-429C-9DF0-745A8B55B650}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {59B4FBEE-512C-429C-9DF0-745A8B55B650}.Debug|Any CPU.Build.0 = Debug|Any CPU + {59B4FBEE-512C-429C-9DF0-745A8B55B650}.Release|Any CPU.ActiveCfg = Release|Any CPU + {59B4FBEE-512C-429C-9DF0-745A8B55B650}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Event.Sink/Controllers/S3StorageController.cs b/Event.Sink/Controllers/S3StorageController.cs new file mode 100644 index 00000000..d097fe1a --- /dev/null +++ b/Event.Sink/Controllers/S3StorageController.cs @@ -0,0 +1,63 @@ +using Event.Sink.Storage; +using Microsoft.AspNetCore.Mvc; +using System.Text; +using System.Text.Json.Nodes; + +namespace Event.Sink.Controllers; + +/// +/// Контроллер для взаимодейсвия с 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/Event.Sink/Controllers/SnsSubscriberController.cs b/Event.Sink/Controllers/SnsSubscriberController.cs new file mode 100644 index 00000000..5fd08eb6 --- /dev/null +++ b/Event.Sink/Controllers/SnsSubscriberController.cs @@ -0,0 +1,71 @@ +using Amazon.SimpleNotificationService.Util; +using Event.Sink.Storage; +using Microsoft.AspNetCore.Mvc; +using System.Text; + +namespace Event.Sink.Controllers; + +/// +/// Контроллер для приема сообщений от SNS +/// +/// Служба для работы с S3 +/// Логгер +[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); + string jsonContent = await reader.ReadToEndAsync(); + + var snsMessage = Message.ParseMessage(jsonContent); + + if (snsMessage.Type == "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"); + return Ok(); + } + + if (snsMessage.Type == "Notification") + { + await s3Service.UploadFile(snsMessage.MessageText); + logger.LogInformation("Notification was successfully processed"); + } + + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occurred while processing SNS notifications"); + } + return Ok(); + } +} \ No newline at end of file diff --git a/Event.Sink/Event.Sink.csproj b/Event.Sink/Event.Sink.csproj new file mode 100644 index 00000000..875b29aa --- /dev/null +++ b/Event.Sink/Event.Sink.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + + + + diff --git a/Event.Sink/Messaging/SnsSubscriptionService.cs b/Event.Sink/Messaging/SnsSubscriptionService.cs new file mode 100644 index 00000000..f8480330 --- /dev/null +++ b/Event.Sink/Messaging/SnsSubscriptionService.cs @@ -0,0 +1,41 @@ +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; +using System.Net; + +namespace Event.Sink.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("Subscripltion request for {topic} is seccessfull, waiting for confirmation", _topicArn); + } +} \ No newline at end of file diff --git a/Event.Sink/Program.cs b/Event.Sink/Program.cs new file mode 100644 index 00000000..6c83ff7b --- /dev/null +++ b/Event.Sink/Program.cs @@ -0,0 +1,28 @@ +using Event.Sink; +using LocalStack.Client.Extensions; +using System.Reflection; + +var builder = WebApplication.CreateBuilder(args); +builder.AddServiceDefaults(); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(options => +{ + var assembly = Assembly.GetExecutingAssembly(); + options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{assembly.GetName().Name}.xml")); +}); +builder.Services.AddLocalStack(builder.Configuration); +builder.AddConsumer(); +builder.AddS3(); + +var app = builder.Build(); +await app.UseConsumer(); +await app.UseS3(); +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/Event.Sink/Properties/launchSettings.json b/Event.Sink/Properties/launchSettings.json new file mode 100644 index 00000000..fbdcd317 --- /dev/null +++ b/Event.Sink/Properties/launchSettings.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51854", + "sslPort": 44310 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5134", + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7192;http://localhost:5134", + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Event.Sink/Storage/IS3Service.cs b/Event.Sink/Storage/IS3Service.cs new file mode 100644 index 00000000..4385a2b8 --- /dev/null +++ b/Event.Sink/Storage/IS3Service.cs @@ -0,0 +1,33 @@ +using System.Text.Json.Nodes; + +namespace Event.Sink.Storage; + +/// +/// Интерфейс службы для манипуляции файлами в объектном хранилище +/// +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/Event.Sink/Storage/S3AwsService.cs b/Event.Sink/Storage/S3AwsService.cs new file mode 100644 index 00000000..fe26dd51 --- /dev/null +++ b/Event.Sink/Storage/S3AwsService.cs @@ -0,0 +1,121 @@ +using Amazon.S3; +using Amazon.S3.Model; +using System.Net; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace Event.Sink.Storage; + +/// +/// Cлужба для манипуляции файлами в объектном хранилище +/// +/// S3 клиент +/// Конфигурация +/// Логер +public class S3AwsService(IAmazonS3 client, IConfiguration configuration, ILogger logger) : IS3Service +{ + private readonly string _bucketName = configuration["AWS:Resources:S3BucketName"] + ?? throw new KeyNotFoundException("S3 bucket name was not found in configuration"); + + /// + public async Task> GetFileList() + { + 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}", _bucketName); + await foreach (var response in paginator.Responses) + if (response != null && response.S3Objects != null) + foreach (var obj in response.S3Objects) + { + if (obj != null) + list.Add(obj.Key); + else + logger.LogWarning("Received null object from {bucket}", _bucketName); + } + else + logger.LogWarning("Received null response from {bucket}", _bucketName); + 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"); + + using var stream = new MemoryStream(); + JsonSerializer.Serialize(stream, rootNode); + stream.Seek(0, SeekOrigin.Begin); + + logger.LogInformation("Began uploading employee {file} onto {bucket}", id, _bucketName); + var request = new PutObjectRequest + { + BucketName = _bucketName, + Key = $"employee_{id}.json", + InputStream = stream + }; + + var response = await client.PutObjectAsync(request); + + if (response.HttpStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to upload employee {file}: {code}", id, response.HttpStatusCode); + return false; + } + logger.LogInformation("Finished uploading employee {file} to {bucket}", id, _bucketName); + return true; + } + + /// + public async Task DownloadFile(string key) + { + logger.LogInformation("Began downloading {file} from {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}: {code}", key, response.HttpStatusCode); + throw new InvalidOperationException($"Error occurred downloading {key} - {response.HttpStatusCode}"); + } + using var reader = new StreamReader(response.ResponseStream, 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 ", key); + throw; + } + } + + /// + public async Task EnsureBucketExists() + { + logger.LogInformation("Checking whether {bucket} exists", _bucketName); + try + { + await client.EnsureBucketExistsAsync(_bucketName); + logger.LogInformation("{bucket} existence ensured", _bucketName); + } + catch (Exception ex) + { + logger.LogError(ex, "Unhandled exception occurred during {bucket} check", _bucketName); + throw; + } + } +} \ No newline at end of file diff --git a/Event.Sink/WebApplicationBuilderExtensions.cs b/Event.Sink/WebApplicationBuilderExtensions.cs new file mode 100644 index 00000000..1632dbd1 --- /dev/null +++ b/Event.Sink/WebApplicationBuilderExtensions.cs @@ -0,0 +1,60 @@ +using Amazon.S3; +using Amazon.SimpleNotificationService; +using Event.Sink.Messaging; +using Event.Sink.Storage; +using LocalStack.Client.Extensions; + +namespace Event.Sink; + +/// +/// Экстеншен для добавления различных служб в DI +/// +internal static class WebApplicationBuilderExtensions +{ + /// + /// Регистрирует клиентские службы для работы с брокером сообщений + /// + /// Билдер + /// Билдер + /// Если настройки не найдены + public static WebApplicationBuilder AddConsumer(this WebApplicationBuilder builder) + { + builder.Services.AddLocalStack(builder.Configuration); + return builder.AddSnsSubscriber(); + } + + /// + /// Регистрирует службы для работы с SNS + /// + /// Билдер + /// Билдер + private static WebApplicationBuilder AddSnsSubscriber(this WebApplicationBuilder builder) + { + builder.Services.AddScoped(); + builder.Services.AddAwsService(); + return builder; + } + + /// + /// Регистрирует клиентские службы для работы с объектным хранилищем + /// + /// Билдер + /// Билдер + /// Если настройки не найдены + public static WebApplicationBuilder AddS3(this WebApplicationBuilder builder) + { + return builder.AddLocalstack(); + } + + /// + /// Регистрирует службы для работы с S3 по классическому AWS API + /// + /// Билдер + /// Билдер + private static WebApplicationBuilder AddLocalstack(this WebApplicationBuilder builder) + { + builder.Services.AddAwsService(); + builder.Services.AddScoped(); + return builder; + } +} \ No newline at end of file diff --git a/Event.Sink/WebApplicationExtensions.cs b/Event.Sink/WebApplicationExtensions.cs new file mode 100644 index 00000000..d5cf4aab --- /dev/null +++ b/Event.Sink/WebApplicationExtensions.cs @@ -0,0 +1,47 @@ +using Event.Sink.Messaging; +using Event.Sink.Storage; + +namespace Event.Sink; + +/// +/// Экстеншен для добавления брокера +/// +internal static class WebApplicationExtensions +{ + /// + /// Конфигурирует клиенские службы для взаимодействия с брокером сообщений + /// + /// Билдер + /// Билдер + /// Если настройки не найдены + public static async Task UseConsumer(this WebApplication app) + { + return await app.UseSnsSubscriber(); + } + + /// + /// Запускает службу для подписки на SNS + /// + /// Билдер + /// Билдер + private static async Task UseSnsSubscriber(this WebApplication app) + { + using var scope = app.Services.CreateScope(); + var subscriptionService = scope.ServiceProvider.GetRequiredService(); + await subscriptionService.SubscribeEndpoint(); + return app; + } + + /// + /// Конфигурирует клиенские службы для взаимодействия с S3 + /// + /// Билдер + /// Билдер + public static async Task UseS3(this WebApplication app) + { + using var scope = app.Services.CreateScope(); + var s3Service = scope.ServiceProvider.GetRequiredService(); + await s3Service.EnsureBucketExists(); + return app; + } +} \ No newline at end of file diff --git a/Event.Sink/appsettings.Development.json b/Event.Sink/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Event.Sink/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Event.Sink/appsettings.json b/Event.Sink/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Event.Sink/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/ServiceApi/Entities/Employee.cs b/ServiceApi/Entities/Employee.cs new file mode 100644 index 00000000..6bac3f41 --- /dev/null +++ b/ServiceApi/Entities/Employee.cs @@ -0,0 +1,69 @@ +using System.Text.Json.Serialization; + +namespace Service.Api.Entities; + +/// +/// Сотрудник компании +/// +public class Employee +{ + /// + /// Идентификатор сотрудника в системе + /// + [JsonPropertyName("id")] + public int Id { get; set; } + + /// + /// ФИО + /// + [JsonPropertyName("fullName")] + public string? FullName { get; set; } + + /// + /// Должность + /// + [JsonPropertyName("position")] + public string? Position { get; set; } + + /// + /// Отдел + /// + [JsonPropertyName("department")] + public string? Department { get; set; } + + /// + /// Дата приема + /// + [JsonPropertyName("hireDate")] + public DateOnly HireDate { get; set; } + + /// + /// Оклад + /// + [JsonPropertyName("salary")] + public decimal Salary { get; set; } + + /// + /// Электронная почта + /// + [JsonPropertyName("email")] + public string? Email { get; set; } + + /// + /// Номер телефона + /// + [JsonPropertyName("phone")] + public string? Phone { get; set; } + + /// + /// Индикатор увольнения + /// + [JsonPropertyName("isFired")] + public bool IsFired { get; set; } + + /// + /// Дата увольнения + /// + [JsonPropertyName("fireDate")] + public DateOnly? FireDate { get; set; } +} \ No newline at end of file diff --git a/ServiceApi/Generator/EmployeeGenerator.cs b/ServiceApi/Generator/EmployeeGenerator.cs new file mode 100644 index 00000000..2f344ec8 --- /dev/null +++ b/ServiceApi/Generator/EmployeeGenerator.cs @@ -0,0 +1,114 @@ +using Bogus; +using Bogus.DataSets; +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Генератор случайных сотрудников компании +/// +public static class EmployeeGenerator +{ + private static readonly Faker _faker; + + /// + /// Справочник профессий + /// + private static readonly string[] _professions = + { + "Developer", + "Manager", + "Analyst", + "Tester", + "Administrator", + "Designer" + }; + + /// + /// Справочник суффиксов + /// + private static readonly string[] _levels = + { + "Junior", + "Middle", + "Senior" + }; + + static EmployeeGenerator() + { + _faker = new Faker("ru") + + + // ФИО + .RuleFor(e => e.FullName, f => + { + var gender = f.PickRandom(); + + var firstName = f.Name.FirstName(gender); + var lastName = f.Name.LastName(gender); + + var patronymicSuffix = gender == Name.Gender.Male ? "ович" : "овна"; + var patronymic = f.Name.FirstName(Name.Gender.Male) + patronymicSuffix; + + return $"{lastName} {firstName} {patronymic}"; + }) + + // Должность + .RuleFor(e => e.Position, f => + { + var level = f.PickRandom(_levels); + var profession = f.PickRandom(_professions); + + return $"{level} {profession}"; + }) + + // Отдел + .RuleFor(e => e.Department, f => f.Commerce.Department()) + + // Дата приема (не более 10 лет назад) + .RuleFor(e => e.HireDate, + f => DateOnly.FromDateTime(f.Date.Past(10))) + + // Оклад (зависит от уровня) + .RuleFor(e => e.Salary, (f, e) => + { + if (e.Position!.Contains("Junior")) + return Math.Round(f.Random.Decimal(50000, 90000), 2); + + if (e.Position.Contains("Middle")) + return Math.Round(f.Random.Decimal(90000, 150000), 2); + + return Math.Round(f.Random.Decimal(150000, 250000), 2); + }) + + // Email + .RuleFor(e => e.Email, f => f.Internet.Email()) + + // Телефон + .RuleFor(e => e.Phone, + f => f.Phone.PhoneNumber("+7(###)###-##-##")) + + // Индикатор увольнения + .RuleFor(e => e.IsFired, + f => f.Random.Bool(0.2f)) // 20% сотрудников уволены + + // Дата увольнения + .RuleFor(e => e.FireDate, (f, e) => + { + if (!e.IsFired) + return null; + + return f.Date.BetweenDateOnly(e.HireDate, DateOnly.FromDateTime(DateTime.Now)); + }); + } + + /// + /// Генерирует одного случайного сотрудника + /// + public static Employee Generate(int id) + { + var employee = _faker.Generate(); + employee.Id = id; + return employee; + } +} \ No newline at end of file diff --git a/ServiceApi/Generator/EmployeeGeneratorService.cs b/ServiceApi/Generator/EmployeeGeneratorService.cs new file mode 100644 index 00000000..b8b9f451 --- /dev/null +++ b/ServiceApi/Generator/EmployeeGeneratorService.cs @@ -0,0 +1,67 @@ +using Microsoft.Extensions.Caching.Distributed; +using Service.Api.Entities; +using Service.Api.Messaging; +using System.Text.Json; +namespace Service.Api.Generator; + +public class EmployeeGeneratorService(IDistributedCache cache, IProducerService messagingService, ILogger logger, IConfiguration configuration) : IEmployeeGeneratorService +{ + /// + /// Время инициализации кэша + /// + private readonly TimeSpan _cacheExpiration = int.TryParse(configuration["CacheExpiration"], out var sec) + ? TimeSpan.FromSeconds(sec) + : TimeSpan.FromSeconds(3600); + + public async Task ProcessEmployee(int id) + { + logger.LogInformation("Обработка сотрудника {id} ", id); + try + { + logger.LogInformation("Trying to get employee {id} from cache", id); + var employee = await RetrieveFromCache(id); + if (employee != null) + { + logger.LogInformation("Employee {id} was found in cache", id); + return employee; + } + logger.LogInformation("No employee {id} in cache. Generating employee", id); + employee = EmployeeGenerator.Generate(id); + logger.LogInformation("Populating the cache with employee {id}", id); + await messagingService.SendMessage(employee); + await PopulateCache(employee); + return employee; + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occurred during employee {id} processing", id); + throw; + } + + } + + /// + /// Пытается достать сотрудника из кэша + /// + private async Task RetrieveFromCache(int id) + { + var json = await cache.GetStringAsync(id.ToString()); + if (string.IsNullOrEmpty(json)) + return null; + return JsonSerializer.Deserialize(json); + } + + /// + /// Кладет сотрудника в кэш + /// + private async Task PopulateCache(Employee employee) + { + var json = JsonSerializer.Serialize(employee); + await cache.SetStringAsync(employee.Id.ToString(), json, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheExpiration + }); + } + +} diff --git a/ServiceApi/Generator/IEmployeeGeneratorService.cs b/ServiceApi/Generator/IEmployeeGeneratorService.cs new file mode 100644 index 00000000..8ae6fda8 --- /dev/null +++ b/ServiceApi/Generator/IEmployeeGeneratorService.cs @@ -0,0 +1,12 @@ +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Интерфейс для записи юзкейса по обработке сотрудников компании +/// +public interface IEmployeeGeneratorService +{ + Task ProcessEmployee(int id); + +} diff --git a/ServiceApi/Messaging/IProducerService.cs b/ServiceApi/Messaging/IProducerService.cs new file mode 100644 index 00000000..36aa5a59 --- /dev/null +++ b/ServiceApi/Messaging/IProducerService.cs @@ -0,0 +1,15 @@ +using Service.Api.Entities; + +namespace Service.Api.Messaging; + +/// +/// Интерфейс службы для отправки генерируемых сотрудников в брокер сообщений +/// +public interface IProducerService +{ + /// + /// Отправляет сообщение в брокер + /// + /// Сотрудник + public Task SendMessage(Employee employee); +} diff --git a/ServiceApi/Messaging/SnsPublisherService.cs b/ServiceApi/Messaging/SnsPublisherService.cs new file mode 100644 index 00000000..3648a08a --- /dev/null +++ b/ServiceApi/Messaging/SnsPublisherService.cs @@ -0,0 +1,43 @@ +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; +using Service.Api.Entities; +using System.Net; +using System.Text.Json; + +namespace Service.Api.Messaging; + +/// +/// Служба для отправки сообщений в SNS +/// +/// Клиент SNS +/// Конфигурация +/// Логгер +public class SnsPublisherService(IAmazonSimpleNotificationService client, IConfiguration configuration, ILogger logger) : IProducerService +{ + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] + ?? throw new KeyNotFoundException("SNS topic link was not found in configuration"); + + /// + public async Task SendMessage(Employee employee) + { + try + { + var json = JsonSerializer.Serialize(employee); + var request = new PublishRequest + { + Message = json, + TopicArn = _topicArn + }; + var response = await client.PublishAsync(request); + if (response.HttpStatusCode == HttpStatusCode.OK) + logger.LogInformation("Employee {id} was sent to sink via SNS", employee.Id); + else + throw new Exception($"SNS returned {response.HttpStatusCode}"); + } + catch (Exception ex) + { + logger.LogError(ex, "Unable to send employee through SNS topic"); + } + + } +} \ No newline at end of file diff --git a/ServiceApi/Program.cs b/ServiceApi/Program.cs new file mode 100644 index 00000000..23ce7996 --- /dev/null +++ b/ServiceApi/Program.cs @@ -0,0 +1,16 @@ +using Service.Api; +using Service.Api.Generator; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("RedisCache"); + +builder.Services.AddScoped(); +builder.AddProducer(); + +var app = builder.Build(); +app.MapDefaultEndpoints(); +app.MapGet("/employee", (IEmployeeGeneratorService service, int id) => service.ProcessEmployee(id)); + +app.Run(); diff --git a/ServiceApi/Properties/launchSettings.json b/ServiceApi/Properties/launchSettings.json new file mode 100644 index 00000000..1fb877b8 --- /dev/null +++ b/ServiceApi/Properties/launchSettings.json @@ -0,0 +1,13 @@ +{ + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:7099", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ServiceApi/Service.Api.csproj b/ServiceApi/Service.Api.csproj new file mode 100644 index 00000000..3a4b19a6 --- /dev/null +++ b/ServiceApi/Service.Api.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/ServiceApi/WebApplicationBuilderExtensions.cs b/ServiceApi/WebApplicationBuilderExtensions.cs new file mode 100644 index 00000000..2adad362 --- /dev/null +++ b/ServiceApi/WebApplicationBuilderExtensions.cs @@ -0,0 +1,36 @@ +using Amazon.SimpleNotificationService; +using LocalStack.Client.Extensions; +using Service.Api.Messaging; + +namespace Service.Api; + +/// +/// Экстеншен для добавления различных служб в DI +/// +internal static class WebApplicationBuilderExtensions +{ + /// + /// Регистрирует клиентские службы для работы с брокером сообщений + /// + /// Билдер + /// Билдер + /// Если настройки не найдены + public static WebApplicationBuilder AddProducer(this WebApplicationBuilder builder) + { + builder.Services.AddLocalStack(builder.Configuration); + return builder.AddSnsPublisher(); + } + + /// + /// Регистрирует службы для работы с SNS + /// + /// Билдер + /// Билдер + private static WebApplicationBuilder AddSnsPublisher(this WebApplicationBuilder builder) + { + builder.Services.AddScoped(); + builder.Services.AddAwsService(); + return builder; + } + +} \ No newline at end of file diff --git a/ServiceApi/appsettings.Development.json b/ServiceApi/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/ServiceApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ServiceApi/appsettings.json b/ServiceApi/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/ServiceApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}