-
Notifications
You must be signed in to change notification settings - Fork 52
Степанов Дмитрий Лаб. 3 Группа 6511 #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4af3e53
8a78fe1
2365002
4d0a399
a728f2d
9fd7735
026d492
a815d79
7a51625
6af949e
575e22c
a36c952
2d5bd92
d36a28e
e4724c1
983a873
27fe306
f5b05a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,5 @@ | |
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "" | ||
| } | ||
| "BaseAddress": "http://localhost:5000/patient" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using System.Text.Json; | ||
| using MassTransit; | ||
| using PatientApp.Generator.Models; | ||
| using PatientApp.EventSink.Storage; | ||
|
|
||
| namespace PatientApp.EventSink.Consumers; | ||
|
|
||
| public class PatientCreatedConsumer( | ||
| IStorageService storageService, | ||
| ILogger<PatientCreatedConsumer> logger) | ||
| : IConsumer<Patient> | ||
| { | ||
| public async Task Consume( | ||
| ConsumeContext<Patient> context) | ||
| { | ||
| var patient = context.Message; | ||
|
|
||
| logger.LogInformation( | ||
| "Received patient {Id}", | ||
| patient.Id); | ||
|
|
||
| var json = | ||
| JsonSerializer.Serialize(patient); | ||
|
|
||
| await storageService.SaveAsync( | ||
| $"{patient.Id}.json", | ||
| json); | ||
|
|
||
| logger.LogInformation( | ||
| "Patient {Id} saved to S3", | ||
| patient.Id); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.3.37" /> | ||
| <PackageReference Include="AWSSDK.S3" Version="4.0.23" /> | ||
| <PackageReference Include="AWSSDK.SimpleNotificationService" Version="4.0.2.30" /> | ||
| <PackageReference Include="LocalStack.Client.Extensions" Version="2.0.0" /> | ||
| <PackageReference Include="MassTransit" Version="9.2.0-develop.138" /> | ||
| <PackageReference Include="MassTransit.AmazonSQS" Version="9.2.0-develop.138" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\GeneratorService\PatientApp.Generator.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| using Amazon; | ||
| using Amazon.Runtime; | ||
| using Amazon.S3; | ||
| using Amazon.S3.Model; | ||
| using Amazon.SimpleNotificationService; | ||
| using Amazon.SQS; | ||
| using MassTransit; | ||
| using PatientApp.EventSink.Consumers; | ||
| using PatientApp.EventSink.Storage; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.AddControllers(); | ||
|
|
||
|
Comment on lines
+11
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь не хватает |
||
| builder.Services.AddSingleton<IAmazonS3>(_ => | ||
| { | ||
| var config = new AmazonS3Config | ||
| { | ||
| ServiceURL = "http://localhost:4566", | ||
| ForcePathStyle = true, | ||
| UseHttp = true, | ||
| AuthenticationRegion = "us-east-1" | ||
| }; | ||
|
|
||
| return new AmazonS3Client( | ||
| new BasicAWSCredentials("test", "test"), | ||
| config); | ||
| }); | ||
|
|
||
| builder.Services.AddScoped<IStorageService, StorageService>(); | ||
|
|
||
| builder.Services.AddMassTransit(x => | ||
| { | ||
| x.AddConsumer<PatientCreatedConsumer>(); | ||
|
|
||
| x.UsingAmazonSqs((context, cfg) => | ||
| { | ||
| cfg.Host( | ||
| "us-east-1", | ||
| h => | ||
| { | ||
| h.AccessKey("test"); | ||
|
|
||
| h.SecretKey("test"); | ||
|
|
||
| h.Config(new AmazonSQSConfig | ||
| { | ||
| ServiceURL = "http://localhost:4566", | ||
| AuthenticationRegion = "us-east-1", | ||
| UseHttp = true | ||
| }); | ||
|
|
||
| h.Config( | ||
| new AmazonSimpleNotificationServiceConfig | ||
| { | ||
| ServiceURL = | ||
| "http://localhost:4566", | ||
|
|
||
| AuthenticationRegion = | ||
| "us-east-1", | ||
|
|
||
| UseHttp = true | ||
| }); | ||
| }); | ||
|
|
||
| cfg.ReceiveEndpoint( | ||
| "patients-queue", | ||
| e => | ||
| { | ||
| e.ConfigureConsumeTopology = true; | ||
|
|
||
| e.ConfigureConsumer< | ||
| PatientCreatedConsumer>( | ||
| context); | ||
| }); | ||
| }); | ||
| }); | ||
|
Comment on lines
+32
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если не ошибаюсь, на лекциях должно было говорится, что пользоваться |
||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.MapControllers(); | ||
|
|
||
| app.MapGet("/test-s3", async ( | ||
| IAmazonS3 s3) => | ||
| { | ||
| var buckets = | ||
| await s3.ListBucketsAsync(); | ||
|
|
||
| return Results.Ok( | ||
| buckets.Buckets | ||
| .Select(x => x.BucketName) | ||
| .ToList()); | ||
| }); | ||
|
|
||
| app.MapGet("/test-files", async ( | ||
| IAmazonS3 s3) => | ||
| { | ||
| try | ||
| { | ||
| var response = | ||
| await s3.ListObjectsV2Async( | ||
| new ListObjectsV2Request | ||
| { | ||
| BucketName = "patients" | ||
| }); | ||
|
|
||
| return Results.Ok( | ||
| response.S3Objects | ||
| .Select(x => x.Key) | ||
| .ToList()); | ||
| } | ||
| catch | ||
| { | ||
| return Results.Ok( | ||
| new List<string>()); | ||
| } | ||
| }); | ||
|
|
||
| app.Run(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "EventSink": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "applicationUrl": "http://0.0.0.0:20829", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace PatientApp.EventSink.Storage; | ||
|
|
||
| public interface IStorageService | ||
| { | ||
| public Task SaveAsync(string fileName, string content); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using Amazon.S3; | ||
| using Amazon.S3.Model; | ||
| using Amazon.S3.Util; | ||
|
|
||
| namespace PatientApp.EventSink.Storage; | ||
|
|
||
| public class StorageService( | ||
| IAmazonS3 s3Client) : IStorageService | ||
| { | ||
| private const string BucketName = "patients"; | ||
|
|
||
| public async Task SaveAsync( | ||
| string fileName, | ||
| string content) | ||
| { | ||
| var exists = | ||
| await AmazonS3Util | ||
| .DoesS3BucketExistV2Async( | ||
| s3Client, | ||
| BucketName); | ||
|
|
||
| if (!exists) | ||
| { | ||
| await s3Client.PutBucketAsync( | ||
| new PutBucketRequest | ||
| { | ||
| BucketName = BucketName | ||
| }); | ||
| } | ||
|
|
||
| var request = new PutObjectRequest | ||
| { | ||
| BucketName = BucketName, | ||
| Key = fileName, | ||
| ContentBody = content | ||
| }; | ||
|
|
||
| await s3Client.PutObjectAsync(request); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "EventSink": { | ||
| "Endpoint": "http://host.docker.internal:20829/subscription" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleanup