From bc4ddb59081936d1a2fef32ce045475c0fc1eb01 Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Fri, 13 Feb 2026 18:33:14 +0400 Subject: [PATCH 01/12] feat: added api service, redis cache service and aspire orchestration --- Aspire/Aspire.AppHost/AppHost.cs | 13 ++ Aspire/Aspire.AppHost/Aspire.AppHost.csproj | 23 ++++ .../Properties/launchSettings.json | 29 ++++ .../appsettings.Development.json | 8 ++ Aspire/Aspire.AppHost/appsettings.json | 9 ++ .../Aspire.ServiceDefaults.csproj | 22 +++ Aspire/Aspire.ServiceDefaults/Extensions.cs | 127 ++++++++++++++++++ Client.Wasm/Components/StudentCard.razor | 6 +- Client.Wasm/wwwroot/appsettings.json | 2 +- CloudDevelopment.sln | 30 +++++ Service.Api/Entity/ProgramProject.cs | 15 +++ Service.Api/Generator/ProgramProjectFaker.cs | 28 ++++ .../ProgramProjectGeneratorService.cs | 16 +++ Service.Api/Program.cs | 64 +++++++++ Service.Api/Properties/launchSettings.json | 41 ++++++ Service.Api/Redis/RedisCacheService.cs | 33 +++++ Service.Api/Service.Api.csproj | 20 +++ Service.Api/Service.Api.http | 6 + Service.Api/appsettings.Development.json | 8 ++ Service.Api/appsettings.json | 9 ++ 20 files changed, 505 insertions(+), 4 deletions(-) create mode 100644 Aspire/Aspire.AppHost/AppHost.cs create mode 100644 Aspire/Aspire.AppHost/Aspire.AppHost.csproj create mode 100644 Aspire/Aspire.AppHost/Properties/launchSettings.json create mode 100644 Aspire/Aspire.AppHost/appsettings.Development.json create mode 100644 Aspire/Aspire.AppHost/appsettings.json create mode 100644 Aspire/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj create mode 100644 Aspire/Aspire.ServiceDefaults/Extensions.cs create mode 100644 Service.Api/Entity/ProgramProject.cs create mode 100644 Service.Api/Generator/ProgramProjectFaker.cs create mode 100644 Service.Api/Generator/ProgramProjectGeneratorService.cs create mode 100644 Service.Api/Program.cs create mode 100644 Service.Api/Properties/launchSettings.json create mode 100644 Service.Api/Redis/RedisCacheService.cs create mode 100644 Service.Api/Service.Api.csproj create mode 100644 Service.Api/Service.Api.http create mode 100644 Service.Api/appsettings.Development.json create mode 100644 Service.Api/appsettings.json diff --git a/Aspire/Aspire.AppHost/AppHost.cs b/Aspire/Aspire.AppHost/AppHost.cs new file mode 100644 index 00000000..aa03cfcf --- /dev/null +++ b/Aspire/Aspire.AppHost/AppHost.cs @@ -0,0 +1,13 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("programproj-cache").WithRedisInsight(containerName: "programproj-insight"); + +var apiService = builder.AddProject("programproj-api") + .WithReference(cache) + .WithHttpHealthCheck("/health") + .WaitFor(cache); + +builder.AddProject("programproj-wasm") + .WaitFor(apiService); + +builder.Build().Run(); diff --git a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj new file mode 100644 index 00000000..0f00fac5 --- /dev/null +++ b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj @@ -0,0 +1,23 @@ + + + + + + Exe + net8.0 + enable + enable + 348a6352-668a-4b0d-833f-bbd1cc51ceb3 + + + + + + + + + + + + + diff --git a/Aspire/Aspire.AppHost/Properties/launchSettings.json b/Aspire/Aspire.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..59a529e3 --- /dev/null +++ b/Aspire/Aspire.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17161;http://localhost:15041", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21204", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22223" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15041", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19104", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20068" + } + } + } +} diff --git a/Aspire/Aspire.AppHost/appsettings.Development.json b/Aspire/Aspire.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Aspire/Aspire.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Aspire/Aspire.AppHost/appsettings.json b/Aspire/Aspire.AppHost/appsettings.json new file mode 100644 index 00000000..31c092aa --- /dev/null +++ b/Aspire/Aspire.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/Aspire/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj b/Aspire/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj new file mode 100644 index 00000000..1b6e209a --- /dev/null +++ b/Aspire/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/Aspire/Aspire.ServiceDefaults/Extensions.cs b/Aspire/Aspire.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..b72c8753 --- /dev/null +++ b/Aspire/Aspire.ServiceDefaults/Extensions.cs @@ -0,0 +1,127 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + private const string HealthEndpointPath = "/health"; + private const string AlivenessEndpointPath = "/alive"; + + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..11b26897 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,9 +4,9 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ + Номер №1 "Кэширование" + Вариант №42 "Название варианта" + Выполнена Кадников Николай 6513 Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index 4dda7c04..118aaa10 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "https://localhost:7170/land-plot" + "BaseAddress": "https://localhost:7262/program-proj" } \ No newline at end of file diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..2288dddf 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -5,6 +5,16 @@ 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}") = "Service.Api", "Service.Api\Service.Api.csproj", "{5003CEA3-9343-45C1-B1E4-6FE43745A8D5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost", "Aspire\Aspire.AppHost\Aspire.AppHost.csproj", "{466A15CD-2DE0-4374-9653-08989D32708A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{AD6B47E3-6C6E-420E-9803-369B1474B73F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ApiService", "Aspire\Aspire.ApiService\Aspire.ApiService.csproj", "{B7E77CED-CABF-402E-9042-11B3EF948903}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Web", "Aspire\Aspire.Web\Aspire.Web.csproj", "{EF33F125-28B4-4C04-B013-EC102109CED9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +25,26 @@ 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 + {5003CEA3-9343-45C1-B1E4-6FE43745A8D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5003CEA3-9343-45C1-B1E4-6FE43745A8D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5003CEA3-9343-45C1-B1E4-6FE43745A8D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5003CEA3-9343-45C1-B1E4-6FE43745A8D5}.Release|Any CPU.Build.0 = Release|Any CPU + {466A15CD-2DE0-4374-9653-08989D32708A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {466A15CD-2DE0-4374-9653-08989D32708A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {466A15CD-2DE0-4374-9653-08989D32708A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {466A15CD-2DE0-4374-9653-08989D32708A}.Release|Any CPU.Build.0 = Release|Any CPU + {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.Build.0 = Release|Any CPU + {B7E77CED-CABF-402E-9042-11B3EF948903}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7E77CED-CABF-402E-9042-11B3EF948903}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7E77CED-CABF-402E-9042-11B3EF948903}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7E77CED-CABF-402E-9042-11B3EF948903}.Release|Any CPU.Build.0 = Release|Any CPU + {EF33F125-28B4-4C04-B013-EC102109CED9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF33F125-28B4-4C04-B013-EC102109CED9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF33F125-28B4-4C04-B013-EC102109CED9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF33F125-28B4-4C04-B013-EC102109CED9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.Api/Entity/ProgramProject.cs b/Service.Api/Entity/ProgramProject.cs new file mode 100644 index 00000000..3c2fdfc5 --- /dev/null +++ b/Service.Api/Entity/ProgramProject.cs @@ -0,0 +1,15 @@ +namespace Service.Api.Entity; + +public class ProgramProject +{ + public int Id { get; set; } + public string Name { get; set; } + public string Customer { get; set; } + public string Manager { get; set; } + public DateOnly StartDate { get; set; } + public DateOnly EndDatePlanned { get; set; } + public DateOnly? EndDateReal { get; set; } + public decimal Budget { get; set; } + public decimal SpentMoney { get; set; } + public int FinishedPerCent { get; set; } +} diff --git a/Service.Api/Generator/ProgramProjectFaker.cs b/Service.Api/Generator/ProgramProjectFaker.cs new file mode 100644 index 00000000..2e9b9457 --- /dev/null +++ b/Service.Api/Generator/ProgramProjectFaker.cs @@ -0,0 +1,28 @@ +using Bogus; +using Service.Api.Entity; + +namespace Service.Api.Generator; + +public class ProgramProjectFaker : Faker +{ + public ProgramProjectFaker() : base("ru") + { + RuleFor(o => o.Name, f => f.Commerce.ProductName()); + RuleFor(o => o.Customer, f => f.Company.CompanyName()); + RuleFor(o => o.Manager, f => f.Name.FullName()); + RuleFor(o => o.StartDate, f => DateOnly.FromDateTime(f.Date.Past(2, DateTime.Now))); + RuleFor(o => o.EndDatePlanned, (f, o) => DateOnly.FromDateTime(f.Date.Future(5, o.StartDate.ToDateTime(TimeOnly.MinValue)))); + RuleFor(o => o.EndDateReal, (f, o) => + { + DateTime end = f.Date.Between(o.StartDate.ToDateTime(TimeOnly.MinValue), o.EndDatePlanned.ToDateTime(TimeOnly.MinValue)); + return end > DateTime.Now ? null : DateOnly.FromDateTime(end); + }); + RuleFor(o => o.Budget, f => Math.Round(f.Finance.Amount(100_000, 1_000_000), 2)); + RuleFor(o => o.FinishedPerCent, (f, o) => o.EndDateReal != null ? 100 : f.Random.Number(1, 100)); + RuleFor(o => o.SpentMoney, (f, o) => + { + var spread = Convert.ToInt32(o.Budget) / 15; + return Math.Round((o.Budget - f.Finance.Amount(-spread, spread)) * o.FinishedPerCent / 100, 2); + }); + } +} diff --git a/Service.Api/Generator/ProgramProjectGeneratorService.cs b/Service.Api/Generator/ProgramProjectGeneratorService.cs new file mode 100644 index 00000000..6c17c0f5 --- /dev/null +++ b/Service.Api/Generator/ProgramProjectGeneratorService.cs @@ -0,0 +1,16 @@ +using Bogus; +using Service.Api.Entity; + +namespace Service.Api.Generator; + +public class ProgramProjectGeneratorService(Faker faker) +{ + private Faker _faker = faker; + + public ProgramProject GetProgramProjectInstance(int id) + { + ProgramProject programProject = _faker.Generate(); + programProject.Id = id; + return programProject; + } +} diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs new file mode 100644 index 00000000..3883a3b5 --- /dev/null +++ b/Service.Api/Program.cs @@ -0,0 +1,64 @@ +using Bogus; +using Service.Api.Entity; +using Service.Api.Generator; +using Service.Api.Redis; +using StackExchange.Redis; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +// Add services to the container. +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + + +builder.Services.AddSingleton, ProgramProjectFaker>(); +builder.Services.AddSingleton(); + +builder.Services.AddSingleton(sp => +{ + var configuration = builder.Configuration.GetConnectionString("programproj-cache"); + if (configuration != null) return ConnectionMultiplexer.Connect(configuration); + else throw new InvalidOperationException("u should fix the redis connection"); +}); + +builder.Services.AddScoped(); + +builder.Services.AddCors(options => +{ + options.AddDefaultPolicy(policy => + policy.AllowAnyOrigin() + .AllowAnyHeader() + .AllowAnyMethod()); +}); + +var app = builder.Build(); + +app.UseCors(); + +app.MapDefaultEndpoints(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.MapGet("/program-proj", async (int id, ProgramProjectGeneratorService generatorService, RedisCacheService cs) => +{ + var key = $"project:{id}"; + var programProject = await cs.GetAsync(key); + if(programProject != null) return Results.Ok(programProject); + var newProject = generatorService.GetProgramProjectInstance(id); + await cs.SetAsync(key, newProject, TimeSpan.FromHours(12)); + return Results.Ok(newProject); +}) +.WithName("GetProgramProject") +.WithOpenApi(); + +app.Run(); diff --git a/Service.Api/Properties/launchSettings.json b/Service.Api/Properties/launchSettings.json new file mode 100644 index 00000000..f7af466f --- /dev/null +++ b/Service.Api/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:45188", + "sslPort": 44359 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5044", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7262;http://localhost:5044", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Service.Api/Redis/RedisCacheService.cs b/Service.Api/Redis/RedisCacheService.cs new file mode 100644 index 00000000..ce478ae5 --- /dev/null +++ b/Service.Api/Redis/RedisCacheService.cs @@ -0,0 +1,33 @@ +using System.Text.Json; +using Service.Api.Entity; +using StackExchange.Redis; + +namespace Service.Api.Redis; + +public class RedisCacheService +{ + private readonly IDatabase _db; + + public RedisCacheService(IConnectionMultiplexer redis) + { + this._db = redis.GetDatabase(); + } + + public async Task SetAsync(string key, T value, TimeSpan? expire) + { + var json = JsonSerializer.Serialize(value); + await this._db.StringSetAsync(key, json, expire); + } + + public async Task GetAsync(string key) + { + var value = await this._db.StringGetAsync(key); + if (value.IsNullOrEmpty) return default; + return JsonSerializer.Deserialize(value!); + } + + public async Task RemoveAsync(string key) + { + await this._db.KeyDeleteAsync(key); + } +} diff --git a/Service.Api/Service.Api.csproj b/Service.Api/Service.Api.csproj new file mode 100644 index 00000000..bc64433b --- /dev/null +++ b/Service.Api/Service.Api.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/Service.Api/Service.Api.http b/Service.Api/Service.Api.http new file mode 100644 index 00000000..2eec3c2d --- /dev/null +++ b/Service.Api/Service.Api.http @@ -0,0 +1,6 @@ +@Service.Api_HostAddress = https://localhost:7262 + +GET {{Service.Api_HostAddress}}/program-proj?id=1 +Accept: application/json + +### diff --git a/Service.Api/appsettings.Development.json b/Service.Api/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Service.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Service.Api/appsettings.json b/Service.Api/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Service.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From 004e4c4190a63ee6b4a95604c5f437ba845c553b Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Sat, 14 Feb 2026 19:03:03 +0400 Subject: [PATCH 02/12] ref: small refactoring --- CloudDevelopment.sln | 12 ------------ Service.Api/Entity/ProgramProject.cs | 4 ++-- .../Generator/ProgramProjectGeneratorService.cs | 4 ++-- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index 2288dddf..4b6a66b0 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -11,10 +11,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost", "Aspire\As EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{AD6B47E3-6C6E-420E-9803-369B1474B73F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ApiService", "Aspire\Aspire.ApiService\Aspire.ApiService.csproj", "{B7E77CED-CABF-402E-9042-11B3EF948903}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Web", "Aspire\Aspire.Web\Aspire.Web.csproj", "{EF33F125-28B4-4C04-B013-EC102109CED9}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,14 +33,6 @@ Global {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Debug|Any CPU.Build.0 = Debug|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.ActiveCfg = Release|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.Build.0 = Release|Any CPU - {B7E77CED-CABF-402E-9042-11B3EF948903}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B7E77CED-CABF-402E-9042-11B3EF948903}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B7E77CED-CABF-402E-9042-11B3EF948903}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B7E77CED-CABF-402E-9042-11B3EF948903}.Release|Any CPU.Build.0 = Release|Any CPU - {EF33F125-28B4-4C04-B013-EC102109CED9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EF33F125-28B4-4C04-B013-EC102109CED9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EF33F125-28B4-4C04-B013-EC102109CED9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EF33F125-28B4-4C04-B013-EC102109CED9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.Api/Entity/ProgramProject.cs b/Service.Api/Entity/ProgramProject.cs index 3c2fdfc5..71594abb 100644 --- a/Service.Api/Entity/ProgramProject.cs +++ b/Service.Api/Entity/ProgramProject.cs @@ -1,8 +1,8 @@ namespace Service.Api.Entity; -public class ProgramProject +public record ProgramProject { - public int Id { get; set; } + public int Id { get; init; } public string Name { get; set; } public string Customer { get; set; } public string Manager { get; set; } diff --git a/Service.Api/Generator/ProgramProjectGeneratorService.cs b/Service.Api/Generator/ProgramProjectGeneratorService.cs index 6c17c0f5..a8432018 100644 --- a/Service.Api/Generator/ProgramProjectGeneratorService.cs +++ b/Service.Api/Generator/ProgramProjectGeneratorService.cs @@ -10,7 +10,7 @@ public class ProgramProjectGeneratorService(Faker faker) public ProgramProject GetProgramProjectInstance(int id) { ProgramProject programProject = _faker.Generate(); - programProject.Id = id; - return programProject; + return programProject with { Id = id }; + } } From af5e41bd41ad319a655e5a7aa508aa29c5dce6cb Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Tue, 17 Feb 2026 14:49:00 +0400 Subject: [PATCH 03/12] fix: small fix --- Client.Wasm/Components/StudentCard.razor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 11b26897..2093dbcf 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -5,7 +5,7 @@ Номер №1 "Кэширование" - Вариант №42 "Название варианта" + Вариант №42 "Программный проект" Выполнена Кадников Николай 6513 Ссылка на форк From 7baab03a10bd201595a432cadd8abacec323a3fa Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Thu, 19 Feb 2026 15:23:50 +0400 Subject: [PATCH 04/12] fix: added fork link, added summaries, added primary constructors instead of usual ones, removed extra methods, added Redis exceptions handling, changed cors configuration, refactored ProgramProjectGeneratorService.cs - moved all the logic into new ProgramProjectCacheService.cs + small refactoring --- Client.Wasm/Components/StudentCard.razor | 2 +- Service.Api/Entity/ProgramProject.cs | 35 ++++++++++++++- Service.Api/Generator/ProgramProjectFaker.cs | 11 ++++- .../ProgramProjectGeneratorService.cs | 16 ------- Service.Api/Program.cs | 43 ++++++++----------- Service.Api/Redis/RedisCacheService.cs | 33 +++++++------- Service.Api/Service.Api.csproj | 2 - .../Services/ProgramProjectCacheService.cs | 39 +++++++++++++++++ 8 files changed, 120 insertions(+), 61 deletions(-) delete mode 100644 Service.Api/Generator/ProgramProjectGeneratorService.cs create mode 100644 Service.Api/Services/ProgramProjectCacheService.cs diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 2093dbcf..8fd30dba 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -7,7 +7,7 @@ Номер №1 "Кэширование" Вариант №42 "Программный проект" Выполнена Кадников Николай 6513 - Ссылка на форк + Ссылка на форк diff --git a/Service.Api/Entity/ProgramProject.cs b/Service.Api/Entity/ProgramProject.cs index 71594abb..21476959 100644 --- a/Service.Api/Entity/ProgramProject.cs +++ b/Service.Api/Entity/ProgramProject.cs @@ -1,15 +1,48 @@ namespace Service.Api.Entity; - +/// +/// This class describes program project with customer, manager, +/// dates of start n end, budged and spent money. +/// public record ProgramProject { + /// + /// The project's ID + /// public int Id { get; init; } + /// + /// The project's name + /// public string Name { get; set; } + /// + /// The project's customer + /// public string Customer { get; set; } + /// + /// The project's manager + /// public string Manager { get; set; } + /// + /// The date of actual start of the project + /// public DateOnly StartDate { get; set; } + /// + /// The planned date of the end of project + /// public DateOnly EndDatePlanned { get; set; } + /// + /// The real date of the end of the project. It may be null if the project is still in progress. + /// public DateOnly? EndDateReal { get; set; } + /// + /// The project's budget + /// public decimal Budget { get; set; } + /// + /// How much money the project actually spent + /// public decimal SpentMoney { get; set; } + /// + /// Shows how complete the project is + /// public int FinishedPerCent { get; set; } } diff --git a/Service.Api/Generator/ProgramProjectFaker.cs b/Service.Api/Generator/ProgramProjectFaker.cs index 2e9b9457..4229e3f9 100644 --- a/Service.Api/Generator/ProgramProjectFaker.cs +++ b/Service.Api/Generator/ProgramProjectFaker.cs @@ -2,9 +2,16 @@ using Service.Api.Entity; namespace Service.Api.Generator; - +/// +/// Generator of test data of Program Project using Bogus. +/// public class ProgramProjectFaker : Faker { + /// + /// Initializes the ProgramProject field generation rules + /// with the "ru" locale and established dependencies between dates, + /// budget, completion percentage, and actual costs. + /// public ProgramProjectFaker() : base("ru") { RuleFor(o => o.Name, f => f.Commerce.ProductName()); @@ -18,7 +25,7 @@ public ProgramProjectFaker() : base("ru") return end > DateTime.Now ? null : DateOnly.FromDateTime(end); }); RuleFor(o => o.Budget, f => Math.Round(f.Finance.Amount(100_000, 1_000_000), 2)); - RuleFor(o => o.FinishedPerCent, (f, o) => o.EndDateReal != null ? 100 : f.Random.Number(1, 100)); + RuleFor(o => o.FinishedPerCent, (f, o) => o.EndDateReal != null ? 100 : f.Random.Number(1, 99)); RuleFor(o => o.SpentMoney, (f, o) => { var spread = Convert.ToInt32(o.Budget) / 15; diff --git a/Service.Api/Generator/ProgramProjectGeneratorService.cs b/Service.Api/Generator/ProgramProjectGeneratorService.cs deleted file mode 100644 index a8432018..00000000 --- a/Service.Api/Generator/ProgramProjectGeneratorService.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Bogus; -using Service.Api.Entity; - -namespace Service.Api.Generator; - -public class ProgramProjectGeneratorService(Faker faker) -{ - private Faker _faker = faker; - - public ProgramProject GetProgramProjectInstance(int id) - { - ProgramProject programProject = _faker.Generate(); - return programProject with { Id = id }; - - } -} diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index 3883a3b5..06a8f95e 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -2,20 +2,16 @@ using Service.Api.Entity; using Service.Api.Generator; using Service.Api.Redis; +using Service.Api.Services; using StackExchange.Redis; var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); -// Add services to the container. -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); - builder.Services.AddSingleton, ProgramProjectFaker>(); -builder.Services.AddSingleton(); builder.Services.AddSingleton(sp => { @@ -25,13 +21,25 @@ }); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddCors(options => { options.AddDefaultPolicy(policy => - policy.AllowAnyOrigin() - .AllowAnyHeader() - .AllowAnyMethod()); + policy.SetIsOriginAllowed(origin => + { + try + { + var uri = new Uri(origin); + return uri.Host == "localhost"; + } + catch + { + return false; + } + }) + .WithMethods("GET") + .AllowAnyHeader()); }); var app = builder.Build(); @@ -40,25 +48,12 @@ app.MapDefaultEndpoints(); -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} - app.UseHttpsRedirection(); -app.MapGet("/program-proj", async (int id, ProgramProjectGeneratorService generatorService, RedisCacheService cs) => +app.MapGet("/program-proj", async (int id, ProgramProjectCacheService cacheService) => { - var key = $"project:{id}"; - var programProject = await cs.GetAsync(key); - if(programProject != null) return Results.Ok(programProject); - var newProject = generatorService.GetProgramProjectInstance(id); - await cs.SetAsync(key, newProject, TimeSpan.FromHours(12)); - return Results.Ok(newProject); + return Results.Ok(await cacheService.GetOrGenerateAsync(id)); }) -.WithName("GetProgramProject") -.WithOpenApi(); +.WithName("GetProgramProject"); app.Run(); diff --git a/Service.Api/Redis/RedisCacheService.cs b/Service.Api/Redis/RedisCacheService.cs index ce478ae5..7dc5937f 100644 --- a/Service.Api/Redis/RedisCacheService.cs +++ b/Service.Api/Redis/RedisCacheService.cs @@ -3,31 +3,34 @@ using StackExchange.Redis; namespace Service.Api.Redis; - -public class RedisCacheService +/// +/// Provides Redis caching for storing and retrieving objects. +/// +public class RedisCacheService(IConnectionMultiplexer redis) { - private readonly IDatabase _db; - - public RedisCacheService(IConnectionMultiplexer redis) - { - this._db = redis.GetDatabase(); - } - + private readonly IDatabase _db = redis.GetDatabase(); + /// + /// Stores a value inn Redis. + /// + /// The type of the value to store. + /// Redis key. + /// Object which will be serialized and stored. + /// Expiration timespan, or null for no expiration. public async Task SetAsync(string key, T value, TimeSpan? expire) { var json = JsonSerializer.Serialize(value); await this._db.StringSetAsync(key, json, expire); } - + /// + /// Retrieves a value from Redis and deserializes it. + /// + /// The expected return typee. + /// Redis key. + /// The deserialized value, or default if the key does'nt exist. public async Task GetAsync(string key) { var value = await this._db.StringGetAsync(key); if (value.IsNullOrEmpty) return default; return JsonSerializer.Deserialize(value!); } - - public async Task RemoveAsync(string key) - { - await this._db.KeyDeleteAsync(key); - } } diff --git a/Service.Api/Service.Api.csproj b/Service.Api/Service.Api.csproj index bc64433b..c3988582 100644 --- a/Service.Api/Service.Api.csproj +++ b/Service.Api/Service.Api.csproj @@ -9,8 +9,6 @@ - - diff --git a/Service.Api/Services/ProgramProjectCacheService.cs b/Service.Api/Services/ProgramProjectCacheService.cs new file mode 100644 index 00000000..2d59d1ca --- /dev/null +++ b/Service.Api/Services/ProgramProjectCacheService.cs @@ -0,0 +1,39 @@ +using Bogus; +using Service.Api.Entity; +using Service.Api.Generator; +using Service.Api.Redis; +using StackExchange.Redis; + +namespace Service.Api.Services; + +/// +/// Provides cached access to instances. +/// Generates a new project if it is not found in Redis. +/// +public class ProgramProjectCacheService(RedisCacheService cs, Faker faker) +{ + private RedisCacheService _cs = cs; + private Faker _faker = faker; + /// + /// Returns a cached by ID, + /// or generates and stores a new one if not found. + /// + /// Id of the project. + /// The existing or newly generated . + public async Task GetOrGenerateAsync(int id) + { + var key = $"project:{id}"; + try + { + var programProject = await _cs.GetAsync(key); + if (programProject != null) return programProject; + } + catch (RedisException ex) + { + Console.WriteLine(ex.Message); + } + ProgramProject newProject = _faker.Generate(); + await _cs.SetAsync(key, newProject, TimeSpan.FromHours(12)); + return newProject with { Id = id }; + } +} From 6fd50c6ac1ca69b29bcbc68fe2571e34ed0b1938 Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Sat, 21 Feb 2026 03:25:33 +0400 Subject: [PATCH 05/12] ref: deleted not needed fields, added try-catch to asynk redis call + small refactoring --- Service.Api/Program.cs | 2 +- .../{RedisCacheService.cs => RedisService.cs} | 2 +- .../Services/ProgramProjectCacheService.cs | 19 ++++++++++++------- 3 files changed, 14 insertions(+), 9 deletions(-) rename Service.Api/Redis/{RedisCacheService.cs => RedisService.cs} (95%) diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index 06a8f95e..7cba7e7f 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -20,7 +20,7 @@ else throw new InvalidOperationException("u should fix the redis connection"); }); -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddCors(options => diff --git a/Service.Api/Redis/RedisCacheService.cs b/Service.Api/Redis/RedisService.cs similarity index 95% rename from Service.Api/Redis/RedisCacheService.cs rename to Service.Api/Redis/RedisService.cs index 7dc5937f..805db955 100644 --- a/Service.Api/Redis/RedisCacheService.cs +++ b/Service.Api/Redis/RedisService.cs @@ -6,7 +6,7 @@ namespace Service.Api.Redis; /// /// Provides Redis caching for storing and retrieving objects. /// -public class RedisCacheService(IConnectionMultiplexer redis) +public class RedisService(IConnectionMultiplexer redis) { private readonly IDatabase _db = redis.GetDatabase(); /// diff --git a/Service.Api/Services/ProgramProjectCacheService.cs b/Service.Api/Services/ProgramProjectCacheService.cs index 2d59d1ca..5a1e5d3e 100644 --- a/Service.Api/Services/ProgramProjectCacheService.cs +++ b/Service.Api/Services/ProgramProjectCacheService.cs @@ -10,10 +10,8 @@ namespace Service.Api.Services; /// Provides cached access to instances. /// Generates a new project if it is not found in Redis. /// -public class ProgramProjectCacheService(RedisCacheService cs, Faker faker) +public class ProgramProjectCacheService(RedisService cacheService, Faker faker) { - private RedisCacheService _cs = cs; - private Faker _faker = faker; /// /// Returns a cached by ID, /// or generates and stores a new one if not found. @@ -25,15 +23,22 @@ public async Task GetOrGenerateAsync(int id) var key = $"project:{id}"; try { - var programProject = await _cs.GetAsync(key); + var programProject = await cacheService.GetAsync(key); if (programProject != null) return programProject; } catch (RedisException ex) { Console.WriteLine(ex.Message); } - ProgramProject newProject = _faker.Generate(); - await _cs.SetAsync(key, newProject, TimeSpan.FromHours(12)); - return newProject with { Id = id }; + var newProject = faker.Generate() with { Id = id }; + try + { + await cacheService.SetAsync(key, newProject, TimeSpan.FromHours(12)); + } + catch (RedisException ex) + { + Console.WriteLine(ex.Message); + } + return newProject; } } From c9650ec385cd6d1b68f6c3c55dc78fc7c36cbbbb Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Sun, 22 Feb 2026 21:41:19 +0400 Subject: [PATCH 06/12] ref: made ProgramProject as class instead of record --- Service.Api/Entity/ProgramProject.cs | 4 ++-- Service.Api/Services/ProgramProjectCacheService.cs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Service.Api/Entity/ProgramProject.cs b/Service.Api/Entity/ProgramProject.cs index 21476959..bbd55586 100644 --- a/Service.Api/Entity/ProgramProject.cs +++ b/Service.Api/Entity/ProgramProject.cs @@ -3,12 +3,12 @@ /// This class describes program project with customer, manager, /// dates of start n end, budged and spent money. /// -public record ProgramProject +public class ProgramProject { /// /// The project's ID /// - public int Id { get; init; } + public int Id { get; set; } /// /// The project's name /// diff --git a/Service.Api/Services/ProgramProjectCacheService.cs b/Service.Api/Services/ProgramProjectCacheService.cs index 5a1e5d3e..64d528ae 100644 --- a/Service.Api/Services/ProgramProjectCacheService.cs +++ b/Service.Api/Services/ProgramProjectCacheService.cs @@ -30,7 +30,8 @@ public async Task GetOrGenerateAsync(int id) { Console.WriteLine(ex.Message); } - var newProject = faker.Generate() with { Id = id }; + var newProject = faker.Generate(); + newProject.Id = id; try { await cacheService.SetAsync(key, newProject, TimeSpan.FromHours(12)); From d1d907cec7988a2aa6339172a1a52997f4e843d5 Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Wed, 11 Mar 2026 03:27:22 +0400 Subject: [PATCH 07/12] add: api gateway Ocelot --- Service.ApiGw/Program.cs | 18 +++++++ Service.ApiGw/Properties/launchSettings.json | 38 +++++++++++++ Service.ApiGw/Service.ApiGw.csproj | 17 ++++++ Service.ApiGw/Service.ApiGw.http | 6 +++ Service.ApiGw/apiGateway.json | 32 +++++++++++ Service.ApiGw/appsettings.Development.json | 8 +++ Service.ApiGw/appsettings.json | 9 ++++ .../balancer/QueryBasedLoadBalancer.cs | 53 +++++++++++++++++++ 8 files changed, 181 insertions(+) create mode 100644 Service.ApiGw/Program.cs create mode 100644 Service.ApiGw/Properties/launchSettings.json create mode 100644 Service.ApiGw/Service.ApiGw.csproj create mode 100644 Service.ApiGw/Service.ApiGw.http create mode 100644 Service.ApiGw/apiGateway.json create mode 100644 Service.ApiGw/appsettings.Development.json create mode 100644 Service.ApiGw/appsettings.json create mode 100644 Service.ApiGw/balancer/QueryBasedLoadBalancer.cs diff --git a/Service.ApiGw/Program.cs b/Service.ApiGw/Program.cs new file mode 100644 index 00000000..41d41769 --- /dev/null +++ b/Service.ApiGw/Program.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.DependencyInjection; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; +using Service.ApiGw.balancer; +var builder = WebApplication.CreateBuilder(args); +builder.AddServiceDefaults(); +builder.Configuration.AddJsonFile("apiGateway.json", optional: false, reloadOnChange: true); +builder.Services.AddOcelot(builder.Configuration).AddCustomLoadBalancer((serviceProvider, route, discoveryProvider) => +{ + return new QueryBasedLoadBalancer(discoveryProvider.GetAsync); +}); +var app = builder.Build(); + +app.MapDefaultEndpoints(); + +app.UseHttpsRedirection(); +await app.UseOcelot(); +app.Run(); diff --git a/Service.ApiGw/Properties/launchSettings.json b/Service.ApiGw/Properties/launchSettings.json new file mode 100644 index 00000000..3d945750 --- /dev/null +++ b/Service.ApiGw/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:32709", + "sslPort": 44352 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5247", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7198;http://localhost:5247", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Service.ApiGw/Service.ApiGw.csproj b/Service.ApiGw/Service.ApiGw.csproj new file mode 100644 index 00000000..163a58fc --- /dev/null +++ b/Service.ApiGw/Service.ApiGw.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/Service.ApiGw/Service.ApiGw.http b/Service.ApiGw/Service.ApiGw.http new file mode 100644 index 00000000..f0e212e1 --- /dev/null +++ b/Service.ApiGw/Service.ApiGw.http @@ -0,0 +1,6 @@ +@Service.ApiGw_HostAddress = http://localhost:5247 + +GET {{Service.ApiGw_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Service.ApiGw/apiGateway.json b/Service.ApiGw/apiGateway.json new file mode 100644 index 00000000..2ac27f12 --- /dev/null +++ b/Service.ApiGw/apiGateway.json @@ -0,0 +1,32 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/api/projects", + "UpstreamHttpMethod": [ "Get" ], + "DownstreamPathTemplate": "/program-proj", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 4441 + }, + { + "Host": "localhost", + "Port": 4442 + }, + { + "Host": "localhost", + "Port": 4443 + }, + { + "Host": "localhost", + "Port": 4444 + } + ], + "LoadBalancerOptions": { + "Type": "QueryBasedLoadBalancer" + } + + } + ], +} \ No newline at end of file diff --git a/Service.ApiGw/appsettings.Development.json b/Service.ApiGw/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Service.ApiGw/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Service.ApiGw/appsettings.json b/Service.ApiGw/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Service.ApiGw/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Service.ApiGw/balancer/QueryBasedLoadBalancer.cs b/Service.ApiGw/balancer/QueryBasedLoadBalancer.cs new file mode 100644 index 00000000..f63216f5 --- /dev/null +++ b/Service.ApiGw/balancer/QueryBasedLoadBalancer.cs @@ -0,0 +1,53 @@ +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Values; +using Ocelot.Responses; +using Ocelot.DownstreamRouteFinder.Finder; +using Ocelot.Errors; + +namespace Service.ApiGw.balancer; +/// +/// Ocelot load balancer that selects a downstream service based on the value of id parametr in query. +/// +public class QueryBasedLoadBalancer : ILoadBalancer +{ + private readonly Func>> _services; + public QueryBasedLoadBalancer() + { + _services = null!; + } + + public QueryBasedLoadBalancer(Func>> services) + { + _services = services; + } + + public string Type => nameof(QueryBasedLoadBalancer); + /// + /// get downstream service selected for the request + /// + /// + /// + /// + public async Task> LeaseAsync(HttpContext httpContext) + { + var services = await _services(); + if (services == null || services.Count == 0) { + throw new InvalidOperationException("no downstreaam services"); + } + var strId = httpContext.Request.Query["id"].ToString(); + int id; + if (!int.TryParse(strId, out id)) + { + return new OkResponse(services[0].HostAndPort); + } + var idxx = Math.Abs(id % services.Count); + return new OkResponse(services[idxx].HostAndPort); + } + /// + /// releases Lease instance + /// + /// + public void Release(ServiceHostAndPort hostAndPort) + { + } +} From 48c1667607fcf521e54dd1e4c455bc8728fe9a94 Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Wed, 11 Mar 2026 03:28:18 +0400 Subject: [PATCH 08/12] add: setting 4 replicas and routing gw to them --- Aspire/Aspire.AppHost/AppHost.cs | 27 ++++++++++++++++----- Aspire/Aspire.AppHost/Aspire.AppHost.csproj | 1 + Aspire/Aspire.AppHost/appsettings.json | 15 +++++++----- Client.Wasm/Components/StudentCard.razor | 2 +- Client.Wasm/wwwroot/appsettings.json | 2 +- CloudDevelopment.sln | 6 +++++ Service.Api/Program.cs | 8 ++++-- 7 files changed, 45 insertions(+), 16 deletions(-) diff --git a/Aspire/Aspire.AppHost/AppHost.cs b/Aspire/Aspire.AppHost/AppHost.cs index aa03cfcf..a7962f33 100644 --- a/Aspire/Aspire.AppHost/AppHost.cs +++ b/Aspire/Aspire.AppHost/AppHost.cs @@ -1,13 +1,28 @@ +using Microsoft.Extensions.Configuration; + var builder = DistributedApplication.CreateBuilder(args); var cache = builder.AddRedis("programproj-cache").WithRedisInsight(containerName: "programproj-insight"); -var apiService = builder.AddProject("programproj-api") - .WithReference(cache) - .WithHttpHealthCheck("/health") - .WaitFor(cache); - +var ports = builder.Configuration.GetSection("ApiGateway:Ports").Get() ?? throw new InvalidOperationException("api gw ports r not configured"); +var apiServices = new List>(); +for (var i = 0; i < ports.Length; i++) +{ + var httspPort = ports[i]; + var httpPort = httspPort - 1000; + apiServices.Add(builder.AddProject($"programproj-api{i+1}", project => { project.ExcludeLaunchProfile = true; }) + .WithReference(cache, "RedisCache") + .WithHttpEndpoint(port: httpPort) + .WithHttpsEndpoint(port: httspPort) + .WithHttpHealthCheck("/health", endpointName: "https") + .WaitFor(cache)); +} +var apiGW = builder.AddProject("api-gw", project => { project.ExcludeLaunchProfile = true; }) + .WithHttpEndpoint(port: 5247) + .WithHttpsEndpoint(port: 7198); +foreach (var service in apiServices) apiGW.WaitFor(service); builder.AddProject("programproj-wasm") - .WaitFor(apiService); + .WithReference(apiGW) + .WaitFor(apiGW); builder.Build().Run(); diff --git a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj index 0f00fac5..eae5df84 100644 --- a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj +++ b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj @@ -12,6 +12,7 @@ + diff --git a/Aspire/Aspire.AppHost/appsettings.json b/Aspire/Aspire.AppHost/appsettings.json index 31c092aa..c7bb059c 100644 --- a/Aspire/Aspire.AppHost/appsettings.json +++ b/Aspire/Aspire.AppHost/appsettings.json @@ -1,9 +1,12 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning", - "Aspire.Hosting.Dcp": "Warning" + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + }, + "ApiGateway": { + "Ports": [4441, 4442, 4443, 4444] } - } } diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 8fd30dba..70dd80dc 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,7 +4,7 @@ - Номер №1 "Кэширование" + Номер №2 "Балансировщик" Вариант №42 "Программный проект" Выполнена Кадников Николай 6513 Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index 5c271a1e..1b733ca6 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "https://localhost:7262/program-proj" + "BaseAddress": "https://localhost:7198/api/projects" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index 4b6a66b0..e71fbb7d 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost", "Aspire\As EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{AD6B47E3-6C6E-420E-9803-369B1474B73F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.ApiGw", "Service.ApiGw\Service.ApiGw.csproj", "{64BBEC30-2681-45FD-B160-C36786DC02BB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Debug|Any CPU.Build.0 = Debug|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.ActiveCfg = Release|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.Build.0 = Release|Any CPU + {64BBEC30-2681-45FD-B160-C36786DC02BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {64BBEC30-2681-45FD-B160-C36786DC02BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {64BBEC30-2681-45FD-B160-C36786DC02BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {64BBEC30-2681-45FD-B160-C36786DC02BB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index 7cba7e7f..f6da51c8 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -11,11 +11,13 @@ builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddHealthChecks(); + builder.Services.AddSingleton, ProgramProjectFaker>(); builder.Services.AddSingleton(sp => { - var configuration = builder.Configuration.GetConnectionString("programproj-cache"); + var configuration = builder.Configuration.GetConnectionString("RedisCache"); if (configuration != null) return ConnectionMultiplexer.Connect(configuration); else throw new InvalidOperationException("u should fix the redis connection"); }); @@ -44,6 +46,8 @@ var app = builder.Build(); +app.MapHealthChecks("health"); + app.UseCors(); app.MapDefaultEndpoints(); @@ -55,5 +59,5 @@ return Results.Ok(await cacheService.GetOrGenerateAsync(id)); }) .WithName("GetProgramProject"); - +app.MapGet("/", () => Results.Ok("ok")); app.Run(); From 1ec5cb724c27692cf0d9416ae415fafc3eddd8a2 Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Fri, 13 Mar 2026 21:12:56 +0400 Subject: [PATCH 09/12] fix: fixed naming + small refactoring --- Aspire/Aspire.AppHost/AppHost.cs | 20 ++++++++++-------- Aspire/Aspire.AppHost/Aspire.AppHost.csproj | 2 +- Aspire/Aspire.AppHost/appsettings.json | 2 +- CloudDevelopment.sln | 10 ++++----- .../Program.cs | 5 ++--- .../Properties/launchSettings.json | 8 +++---- .../Service.ApiGateway.csproj | 2 +- .../apiGateway.json | 4 ++++ .../appsettings.Development.json | 0 .../appsettings.json | 0 .../balancer/QueryBasedLoadBalancer.cs | 21 ++++++------------- Service.ApiGw/Service.ApiGw.http | 6 ------ 12 files changed, 35 insertions(+), 45 deletions(-) rename {Service.ApiGw => Service.ApiGateway}/Program.cs (90%) rename {Service.ApiGw => Service.ApiGateway}/Properties/launchSettings.json (80%) rename Service.ApiGw/Service.ApiGw.csproj => Service.ApiGateway/Service.ApiGateway.csproj (90%) rename {Service.ApiGw => Service.ApiGateway}/apiGateway.json (88%) rename {Service.ApiGw => Service.ApiGateway}/appsettings.Development.json (100%) rename {Service.ApiGw => Service.ApiGateway}/appsettings.json (100%) rename {Service.ApiGw => Service.ApiGateway}/balancer/QueryBasedLoadBalancer.cs (71%) delete mode 100644 Service.ApiGw/Service.ApiGw.http diff --git a/Aspire/Aspire.AppHost/AppHost.cs b/Aspire/Aspire.AppHost/AppHost.cs index a7962f33..c6e6abdd 100644 --- a/Aspire/Aspire.AppHost/AppHost.cs +++ b/Aspire/Aspire.AppHost/AppHost.cs @@ -4,25 +4,27 @@ var cache = builder.AddRedis("programproj-cache").WithRedisInsight(containerName: "programproj-insight"); -var ports = builder.Configuration.GetSection("ApiGateway:Ports").Get() ?? throw new InvalidOperationException("api gw ports r not configured"); -var apiServices = new List>(); +var ports = builder.Configuration.GetSection("ApiGateway:Ports").Get() ?? throw new InvalidOperationException("Configuration section 'ApiGateway:Ports' is missing or empty in appsettings.json file."); +var apiGW = builder.AddProject("api-gw", project => { project.ExcludeLaunchProfile = true; }) + .WithHttpEndpoint(port: 5247) + .WithHttpsEndpoint(port: 7198); for (var i = 0; i < ports.Length; i++) { var httspPort = ports[i]; var httpPort = httspPort - 1000; - apiServices.Add(builder.AddProject($"programproj-api{i+1}", project => { project.ExcludeLaunchProfile = true; }) + var service = builder.AddProject($"programproj-api{i + 1}", project => { project.ExcludeLaunchProfile = true; }) .WithReference(cache, "RedisCache") .WithHttpEndpoint(port: httpPort) .WithHttpsEndpoint(port: httspPort) .WithHttpHealthCheck("/health", endpointName: "https") - .WaitFor(cache)); + .WaitFor(cache); + apiGW.WaitFor(service); } -var apiGW = builder.AddProject("api-gw", project => { project.ExcludeLaunchProfile = true; }) - .WithHttpEndpoint(port: 5247) - .WithHttpsEndpoint(port: 7198); -foreach (var service in apiServices) apiGW.WaitFor(service); builder.AddProject("programproj-wasm") - .WithReference(apiGW) .WaitFor(apiGW); +builder.AddProject("serive-apigateway"); + +builder.AddProject("service-apigateway"); + builder.Build().Run(); diff --git a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj index eae5df84..43e30a2a 100644 --- a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj +++ b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj @@ -12,7 +12,7 @@ - + diff --git a/Aspire/Aspire.AppHost/appsettings.json b/Aspire/Aspire.AppHost/appsettings.json index c7bb059c..1584c608 100644 --- a/Aspire/Aspire.AppHost/appsettings.json +++ b/Aspire/Aspire.AppHost/appsettings.json @@ -7,6 +7,6 @@ } }, "ApiGateway": { - "Ports": [4441, 4442, 4443, 4444] + "Ports": [4441, 4442, 4443, 4444, 4445] } } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index e71fbb7d..fac9672e 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -11,7 +11,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost", "Aspire\As EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{AD6B47E3-6C6E-420E-9803-369B1474B73F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.ApiGw", "Service.ApiGw\Service.ApiGw.csproj", "{64BBEC30-2681-45FD-B160-C36786DC02BB}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.ApiGateway", "Service.ApiGateway\Service.ApiGateway.csproj", "{99D4DF70-7107-4481-A111-EC30DCD07545}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -35,10 +35,10 @@ Global {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Debug|Any CPU.Build.0 = Debug|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.ActiveCfg = Release|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.Build.0 = Release|Any CPU - {64BBEC30-2681-45FD-B160-C36786DC02BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {64BBEC30-2681-45FD-B160-C36786DC02BB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {64BBEC30-2681-45FD-B160-C36786DC02BB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {64BBEC30-2681-45FD-B160-C36786DC02BB}.Release|Any CPU.Build.0 = Release|Any CPU + {99D4DF70-7107-4481-A111-EC30DCD07545}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99D4DF70-7107-4481-A111-EC30DCD07545}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99D4DF70-7107-4481-A111-EC30DCD07545}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99D4DF70-7107-4481-A111-EC30DCD07545}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.ApiGw/Program.cs b/Service.ApiGateway/Program.cs similarity index 90% rename from Service.ApiGw/Program.cs rename to Service.ApiGateway/Program.cs index 41d41769..0884edc7 100644 --- a/Service.ApiGw/Program.cs +++ b/Service.ApiGateway/Program.cs @@ -1,7 +1,8 @@ using Microsoft.Extensions.DependencyInjection; using Ocelot.DependencyInjection; using Ocelot.Middleware; -using Service.ApiGw.balancer; +using Service.ApiGateway.balancer; + var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); builder.Configuration.AddJsonFile("apiGateway.json", optional: false, reloadOnChange: true); @@ -11,8 +12,6 @@ }); var app = builder.Build(); -app.MapDefaultEndpoints(); - app.UseHttpsRedirection(); await app.UseOcelot(); app.Run(); diff --git a/Service.ApiGw/Properties/launchSettings.json b/Service.ApiGateway/Properties/launchSettings.json similarity index 80% rename from Service.ApiGw/Properties/launchSettings.json rename to Service.ApiGateway/Properties/launchSettings.json index 3d945750..0f1941ad 100644 --- a/Service.ApiGw/Properties/launchSettings.json +++ b/Service.ApiGateway/Properties/launchSettings.json @@ -4,8 +4,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:32709", - "sslPort": 44352 + "applicationUrl": "http://localhost:42351", + "sslPort": 44388 } }, "profiles": { @@ -13,7 +13,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5247", + "applicationUrl": "http://localhost:5220", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -22,7 +22,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7198;http://localhost:5247", + "applicationUrl": "https://localhost:7130;http://localhost:5220", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Service.ApiGw/Service.ApiGw.csproj b/Service.ApiGateway/Service.ApiGateway.csproj similarity index 90% rename from Service.ApiGw/Service.ApiGw.csproj rename to Service.ApiGateway/Service.ApiGateway.csproj index 163a58fc..f790f47c 100644 --- a/Service.ApiGw/Service.ApiGw.csproj +++ b/Service.ApiGateway/Service.ApiGateway.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/Service.ApiGw/apiGateway.json b/Service.ApiGateway/apiGateway.json similarity index 88% rename from Service.ApiGw/apiGateway.json rename to Service.ApiGateway/apiGateway.json index 2ac27f12..3090ca44 100644 --- a/Service.ApiGw/apiGateway.json +++ b/Service.ApiGateway/apiGateway.json @@ -21,6 +21,10 @@ { "Host": "localhost", "Port": 4444 + }, + { + "Host": "localhost", + "Port": 4445 } ], "LoadBalancerOptions": { diff --git a/Service.ApiGw/appsettings.Development.json b/Service.ApiGateway/appsettings.Development.json similarity index 100% rename from Service.ApiGw/appsettings.Development.json rename to Service.ApiGateway/appsettings.Development.json diff --git a/Service.ApiGw/appsettings.json b/Service.ApiGateway/appsettings.json similarity index 100% rename from Service.ApiGw/appsettings.json rename to Service.ApiGateway/appsettings.json diff --git a/Service.ApiGw/balancer/QueryBasedLoadBalancer.cs b/Service.ApiGateway/balancer/QueryBasedLoadBalancer.cs similarity index 71% rename from Service.ApiGw/balancer/QueryBasedLoadBalancer.cs rename to Service.ApiGateway/balancer/QueryBasedLoadBalancer.cs index f63216f5..e13ffbb6 100644 --- a/Service.ApiGw/balancer/QueryBasedLoadBalancer.cs +++ b/Service.ApiGateway/balancer/QueryBasedLoadBalancer.cs @@ -4,22 +4,13 @@ using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.Errors; -namespace Service.ApiGw.balancer; +namespace Service.ApiGateway.balancer; /// /// Ocelot load balancer that selects a downstream service based on the value of id parametr in query. /// -public class QueryBasedLoadBalancer : ILoadBalancer +public class QueryBasedLoadBalancer(Func>> services) : ILoadBalancer { - private readonly Func>> _services; - public QueryBasedLoadBalancer() - { - _services = null!; - } - - public QueryBasedLoadBalancer(Func>> services) - { - _services = services; - } + private readonly Func>> _services = services; public string Type => nameof(QueryBasedLoadBalancer); /// @@ -32,7 +23,7 @@ public async Task> LeaseAsync(HttpContext httpConte { var services = await _services(); if (services == null || services.Count == 0) { - throw new InvalidOperationException("no downstreaam services"); + throw new InvalidOperationException("QueryBasedLoadBalancer couldnt select an downstream services cuz no downstream services were available"); } var strId = httpContext.Request.Query["id"].ToString(); int id; @@ -40,8 +31,8 @@ public async Task> LeaseAsync(HttpContext httpConte { return new OkResponse(services[0].HostAndPort); } - var idxx = Math.Abs(id % services.Count); - return new OkResponse(services[idxx].HostAndPort); + var index = Math.Abs(id % services.Count); + return new OkResponse(services[index].HostAndPort); } /// /// releases Lease instance diff --git a/Service.ApiGw/Service.ApiGw.http b/Service.ApiGw/Service.ApiGw.http deleted file mode 100644 index f0e212e1..00000000 --- a/Service.ApiGw/Service.ApiGw.http +++ /dev/null @@ -1,6 +0,0 @@ -@Service.ApiGw_HostAddress = http://localhost:5247 - -GET {{Service.ApiGw_HostAddress}}/weatherforecast/ -Accept: application/json - -### From a3527344c2f5571a3aed68ca4826f0d1e09c623a Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Tue, 17 Mar 2026 11:37:42 +0400 Subject: [PATCH 10/12] fix: renamed bloody folder and moved cors from api into the gw --- Aspire/Aspire.AppHost/AppHost.cs | 6 +-- Aspire/Aspire.AppHost/Aspire.AppHost.csproj | 2 +- Client.Wasm/Properties/launchSettings.json | 40 +++++++++---------- CloudDevelopment.sln | 10 ++--- Service.Api/Program.cs | 21 ---------- .../Balancer}/QueryBasedLoadBalancer.cs | 2 +- .../Program.cs | 24 ++++++++++- .../Properties/launchSettings.json | 8 ++-- .../Service.Gateway.csproj | 0 .../apiGateway.json | 0 .../appsettings.Development.json | 0 .../appsettings.json | 0 12 files changed, 56 insertions(+), 57 deletions(-) rename {Service.ApiGateway/balancer => Service.Gateway/Balancer}/QueryBasedLoadBalancer.cs (97%) rename {Service.ApiGateway => Service.Gateway}/Program.cs (54%) rename {Service.ApiGateway => Service.Gateway}/Properties/launchSettings.json (80%) rename Service.ApiGateway/Service.ApiGateway.csproj => Service.Gateway/Service.Gateway.csproj (100%) rename {Service.ApiGateway => Service.Gateway}/apiGateway.json (100%) rename {Service.ApiGateway => Service.Gateway}/appsettings.Development.json (100%) rename {Service.ApiGateway => Service.Gateway}/appsettings.json (100%) diff --git a/Aspire/Aspire.AppHost/AppHost.cs b/Aspire/Aspire.AppHost/AppHost.cs index c6e6abdd..85eedcff 100644 --- a/Aspire/Aspire.AppHost/AppHost.cs +++ b/Aspire/Aspire.AppHost/AppHost.cs @@ -5,7 +5,7 @@ var cache = builder.AddRedis("programproj-cache").WithRedisInsight(containerName: "programproj-insight"); var ports = builder.Configuration.GetSection("ApiGateway:Ports").Get() ?? throw new InvalidOperationException("Configuration section 'ApiGateway:Ports' is missing or empty in appsettings.json file."); -var apiGW = builder.AddProject("api-gw", project => { project.ExcludeLaunchProfile = true; }) +var apiGW = builder.AddProject("api-gw", project => { project.ExcludeLaunchProfile = true; }) .WithHttpEndpoint(port: 5247) .WithHttpsEndpoint(port: 7198); for (var i = 0; i < ports.Length; i++) @@ -23,8 +23,6 @@ builder.AddProject("programproj-wasm") .WaitFor(apiGW); -builder.AddProject("serive-apigateway"); - -builder.AddProject("service-apigateway"); +builder.AddProject("service-gateway"); builder.Build().Run(); diff --git a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj index 43e30a2a..e693d39b 100644 --- a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj +++ b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj @@ -12,8 +12,8 @@ - + diff --git a/Client.Wasm/Properties/launchSettings.json b/Client.Wasm/Properties/launchSettings.json index 0d824ea7..7e2ee008 100644 --- a/Client.Wasm/Properties/launchSettings.json +++ b/Client.Wasm/Properties/launchSettings.json @@ -1,41 +1,41 @@ { - "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:36545", - "sslPort": 44337 - } - }, "profiles": { "http": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "http://localhost:5127", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", + "applicationUrl": "http://localhost:5127" }, "https": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "https://localhost:7282;http://localhost:5127", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", + "applicationUrl": "https://localhost:7282;http://localhost:5127" }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}" + } + }, + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:58120/", + "sslPort": 44345 } } -} +} \ No newline at end of file diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index fac9672e..ed6f5c17 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -11,7 +11,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost", "Aspire\As EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{AD6B47E3-6C6E-420E-9803-369B1474B73F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.ApiGateway", "Service.ApiGateway\Service.ApiGateway.csproj", "{99D4DF70-7107-4481-A111-EC30DCD07545}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Gateway", "Service.Gateway\Service.Gateway.csproj", "{7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -35,10 +35,10 @@ Global {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Debug|Any CPU.Build.0 = Debug|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.ActiveCfg = Release|Any CPU {AD6B47E3-6C6E-420E-9803-369B1474B73F}.Release|Any CPU.Build.0 = Release|Any CPU - {99D4DF70-7107-4481-A111-EC30DCD07545}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {99D4DF70-7107-4481-A111-EC30DCD07545}.Debug|Any CPU.Build.0 = Debug|Any CPU - {99D4DF70-7107-4481-A111-EC30DCD07545}.Release|Any CPU.ActiveCfg = Release|Any CPU - {99D4DF70-7107-4481-A111-EC30DCD07545}.Release|Any CPU.Build.0 = Release|Any CPU + {7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index f6da51c8..3efb018f 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -25,31 +25,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddCors(options => -{ - options.AddDefaultPolicy(policy => - policy.SetIsOriginAllowed(origin => - { - try - { - var uri = new Uri(origin); - return uri.Host == "localhost"; - } - catch - { - return false; - } - }) - .WithMethods("GET") - .AllowAnyHeader()); -}); - var app = builder.Build(); app.MapHealthChecks("health"); -app.UseCors(); - app.MapDefaultEndpoints(); app.UseHttpsRedirection(); diff --git a/Service.ApiGateway/balancer/QueryBasedLoadBalancer.cs b/Service.Gateway/Balancer/QueryBasedLoadBalancer.cs similarity index 97% rename from Service.ApiGateway/balancer/QueryBasedLoadBalancer.cs rename to Service.Gateway/Balancer/QueryBasedLoadBalancer.cs index e13ffbb6..1b409878 100644 --- a/Service.ApiGateway/balancer/QueryBasedLoadBalancer.cs +++ b/Service.Gateway/Balancer/QueryBasedLoadBalancer.cs @@ -4,7 +4,7 @@ using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.Errors; -namespace Service.ApiGateway.balancer; +namespace Service.Gateway.Balancer; /// /// Ocelot load balancer that selects a downstream service based on the value of id parametr in query. /// diff --git a/Service.ApiGateway/Program.cs b/Service.Gateway/Program.cs similarity index 54% rename from Service.ApiGateway/Program.cs rename to Service.Gateway/Program.cs index 0884edc7..ddd419f7 100644 --- a/Service.ApiGateway/Program.cs +++ b/Service.Gateway/Program.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using Ocelot.DependencyInjection; using Ocelot.Middleware; -using Service.ApiGateway.balancer; +using Service.Gateway.Balancer; var builder = WebApplication.CreateBuilder(args); builder.AddServiceDefaults(); @@ -10,8 +10,30 @@ { return new QueryBasedLoadBalancer(discoveryProvider.GetAsync); }); + +builder.Services.AddCors(options => +{ + options.AddDefaultPolicy(policy => + policy.SetIsOriginAllowed(origin => + { + try + { + var uri = new Uri(origin); + return uri.Host == "localhost"; + } + catch + { + return false; + } + }) + .WithMethods("GET") + .AllowAnyHeader()); +}); + var app = builder.Build(); +app.UseCors(); + app.UseHttpsRedirection(); await app.UseOcelot(); app.Run(); diff --git a/Service.ApiGateway/Properties/launchSettings.json b/Service.Gateway/Properties/launchSettings.json similarity index 80% rename from Service.ApiGateway/Properties/launchSettings.json rename to Service.Gateway/Properties/launchSettings.json index 0f1941ad..e20b864c 100644 --- a/Service.ApiGateway/Properties/launchSettings.json +++ b/Service.Gateway/Properties/launchSettings.json @@ -4,8 +4,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:42351", - "sslPort": 44388 + "applicationUrl": "http://localhost:62668", + "sslPort": 44320 } }, "profiles": { @@ -13,7 +13,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5220", + "applicationUrl": "http://localhost:5163", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -22,7 +22,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7130;http://localhost:5220", + "applicationUrl": "https://localhost:7107;http://localhost:5163", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Service.ApiGateway/Service.ApiGateway.csproj b/Service.Gateway/Service.Gateway.csproj similarity index 100% rename from Service.ApiGateway/Service.ApiGateway.csproj rename to Service.Gateway/Service.Gateway.csproj diff --git a/Service.ApiGateway/apiGateway.json b/Service.Gateway/apiGateway.json similarity index 100% rename from Service.ApiGateway/apiGateway.json rename to Service.Gateway/apiGateway.json diff --git a/Service.ApiGateway/appsettings.Development.json b/Service.Gateway/appsettings.Development.json similarity index 100% rename from Service.ApiGateway/appsettings.Development.json rename to Service.Gateway/appsettings.Development.json diff --git a/Service.ApiGateway/appsettings.json b/Service.Gateway/appsettings.json similarity index 100% rename from Service.ApiGateway/appsettings.json rename to Service.Gateway/appsettings.json From c1f220d695da0803f859b1d9eaaf4ab6deb19e3c Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Sun, 19 Apr 2026 02:37:57 +0400 Subject: [PATCH 11/12] add: broker and s3 --- .../Aspire.AppHost.Tests.csproj | 33 +++++ Aspire.AppHost.Tests/UnitTest1.cs | 86 ++++++++++++ Aspire/Aspire.AppHost/AppHost.cs | 85 ++++++++++-- Aspire/Aspire.AppHost/Aspire.AppHost.csproj | 7 + .../programproj-template-sns.yml | 29 ++++ Aspire/Aspire.AppHost/appsettings.json | 5 +- CloudDevelopment.sln | 12 ++ Service.Api/Broker/IProducerService.cs | 8 ++ Service.Api/Broker/SnsPublisherService.cs | 31 +++++ Service.Api/Program.cs | 7 + Service.Api/Service.Api.csproj | 2 + .../Services/ProgramProjectCacheService.cs | 4 +- .../Broker/SnsSubscriptionService.cs | 36 +++++ .../Controllers/S3StorageController.cs | 63 +++++++++ .../Controllers/SnsSubscriberController.cs | 68 ++++++++++ Service.Storage/Program.cs | 26 ++++ .../Properties/launchSettings.json | 38 ++++++ Service.Storage/Service.Storage.csproj | 19 +++ Service.Storage/Storage/IS3Service.cs | 30 +++++ Service.Storage/Storage/S3MinioService.cs | 124 ++++++++++++++++++ Service.Storage/appsettings.Development.json | 8 ++ Service.Storage/appsettings.json | 13 ++ StorageService/Program.cs | 38 ++++++ StorageService/Properties/launchSettings.json | 41 ++++++ StorageService/Service.Storage.csproj | 13 ++ StorageService/StorageService.http | 6 + StorageService/appsettings.Development.json | 8 ++ StorageService/appsettings.json | 9 ++ 28 files changed, 833 insertions(+), 16 deletions(-) create mode 100644 Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj create mode 100644 Aspire.AppHost.Tests/UnitTest1.cs create mode 100644 Aspire/Aspire.AppHost/Cloudformation/programproj-template-sns.yml create mode 100644 Service.Api/Broker/IProducerService.cs create mode 100644 Service.Api/Broker/SnsPublisherService.cs create mode 100644 Service.Storage/Broker/SnsSubscriptionService.cs create mode 100644 Service.Storage/Controllers/S3StorageController.cs create mode 100644 Service.Storage/Controllers/SnsSubscriberController.cs create mode 100644 Service.Storage/Program.cs create mode 100644 Service.Storage/Properties/launchSettings.json create mode 100644 Service.Storage/Service.Storage.csproj create mode 100644 Service.Storage/Storage/IS3Service.cs create mode 100644 Service.Storage/Storage/S3MinioService.cs create mode 100644 Service.Storage/appsettings.Development.json create mode 100644 Service.Storage/appsettings.json create mode 100644 StorageService/Program.cs create mode 100644 StorageService/Properties/launchSettings.json create mode 100644 StorageService/Service.Storage.csproj create mode 100644 StorageService/StorageService.http create mode 100644 StorageService/appsettings.Development.json create mode 100644 StorageService/appsettings.json diff --git a/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj b/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj new file mode 100644 index 00000000..954c6f1f --- /dev/null +++ b/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj @@ -0,0 +1,33 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + diff --git a/Aspire.AppHost.Tests/UnitTest1.cs b/Aspire.AppHost.Tests/UnitTest1.cs new file mode 100644 index 00000000..5662a8e5 --- /dev/null +++ b/Aspire.AppHost.Tests/UnitTest1.cs @@ -0,0 +1,86 @@ +using System.Text.Json; +using Aspire.Hosting; +using Aspire.Hosting.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Service.Api.Entity; +using Xunit.Abstractions; +using System.Net.Http.Json; + +namespace Aspire.AppHost.Tests; + +public class UnitTest1(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("Development")] + 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("api-gw", "http"); + using var gatewayResponse = await gatewayClient!.GetAsync($"/api/projects?id={id}"); + var api = await gatewayResponse.Content.ReadFromJsonAsync(cancellationToken: cancellationToken); + + await Task.Delay(5000); + using var storageClient = _app.CreateHttpClient("programproj-storage", "http"); + using var listResponse = await storageClient!.GetAsync($"/api/s3"); + var ppList = await listResponse.Content.ReadFromJsonAsync>(cancellationToken: cancellationToken); + using var s3Response = await storageClient!.GetAsync($"/api/s3/programproj_{id}.json"); + var s3 = await s3Response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken); + + Assert.NotNull(ppList); + Assert.Single(ppList); + Assert.NotNull(api); + Assert.NotNull(s3); + Assert.Equal(id, s3.Id); + Assert.Equivalent(api, s3); + } + + /// + public async Task DisposeAsync() + { + if (_app is not null) + { + await _app.StopAsync(); + await _app.DisposeAsync(); + } + + if (_builder is not null) + { + await _builder.DisposeAsync(); + } + } + +} \ No newline at end of file diff --git a/Aspire/Aspire.AppHost/AppHost.cs b/Aspire/Aspire.AppHost/AppHost.cs index 85eedcff..7e1a184d 100644 --- a/Aspire/Aspire.AppHost/AppHost.cs +++ b/Aspire/Aspire.AppHost/AppHost.cs @@ -1,28 +1,85 @@ +using Amazon; using Microsoft.Extensions.Configuration; +using Aspire.Hosting.LocalStack.Container; +using Projects; var builder = DistributedApplication.CreateBuilder(args); var cache = builder.AddRedis("programproj-cache").WithRedisInsight(containerName: "programproj-insight"); -var ports = builder.Configuration.GetSection("ApiGateway:Ports").Get() ?? throw new InvalidOperationException("Configuration section 'ApiGateway:Ports' is missing or empty in appsettings.json file."); -var apiGW = builder.AddProject("api-gw", project => { project.ExcludeLaunchProfile = true; }) - .WithHttpEndpoint(port: 5247) - .WithHttpsEndpoint(port: 7198); +var ports = builder.Configuration.GetSection("ApiGateway:Ports").Get() + ?? throw new InvalidOperationException("Configuration section 'ApiGateway:Ports' is missing or empty in appsettings.json file."); + +var apiGW = builder.AddProject("api-gw", project => +{ + project.ExcludeLaunchProfile = true; +}) +.WithHttpEndpoint(port: 5247) +.WithHttpsEndpoint(port: 7198); + +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +var localstack = builder.AddLocalStack("programproj-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 templatePath = Path.Combine( + builder.AppHostDirectory, + "Cloudformation", + "programproj-template-sns.yml"); + +var awsResources = builder.AddAWSCloudFormationTemplate( + "resources", + templatePath, + "programproj") + .WithReference(awsConfig); + +var storage = builder.AddProject("programproj-storage", project => +{ + project.ExcludeLaunchProfile = true; +}) +.WithReference(awsResources) +.WithHttpEndpoint(port: 5280) +.WithEnvironment("AWS__Resources__SNSUrl", "http://host.docker.internal:5280/api/sns") +.WaitFor(awsResources); + +var minio = builder.AddMinioContainer("programproj-minio"); + +storage.WithEnvironment("AWS__Resources__MinioBucketName", "programproj-bucket") + .WithReference(minio) + .WaitFor(minio); + for (var i = 0; i < ports.Length; i++) { - var httspPort = ports[i]; - var httpPort = httspPort - 1000; - var service = builder.AddProject($"programproj-api{i + 1}", project => { project.ExcludeLaunchProfile = true; }) - .WithReference(cache, "RedisCache") - .WithHttpEndpoint(port: httpPort) - .WithHttpsEndpoint(port: httspPort) - .WithHttpHealthCheck("/health", endpointName: "https") - .WaitFor(cache); + var httpsPort = ports[i]; + var httpPort = httpsPort - 1000; + + var service = builder.AddProject($"programproj-api{i + 1}", project => + { + project.ExcludeLaunchProfile = true; + }) + .WithReference(cache, "RedisCache") + .WithHttpEndpoint(port: httpPort) + .WithHttpsEndpoint(port: httpsPort) + .WithReference(awsResources) + .WithHttpHealthCheck("/health", endpointName: "https") + .WaitFor(cache) + .WaitFor(awsResources); + apiGW.WaitFor(service); } + builder.AddProject("programproj-wasm") .WaitFor(apiGW); -builder.AddProject("service-gateway"); +builder.UseLocalStack(localstack); -builder.Build().Run(); +builder.Build().Run(); \ No newline at end of file diff --git a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj index e693d39b..27b6c4b4 100644 --- a/Aspire/Aspire.AppHost/Aspire.AppHost.csproj +++ b/Aspire/Aspire.AppHost/Aspire.AppHost.csproj @@ -14,11 +14,18 @@ + + + + + + + diff --git a/Aspire/Aspire.AppHost/Cloudformation/programproj-template-sns.yml b/Aspire/Aspire.AppHost/Cloudformation/programproj-template-sns.yml new file mode 100644 index 00000000..16bea66c --- /dev/null +++ b/Aspire/Aspire.AppHost/Cloudformation/programproj-template-sns.yml @@ -0,0 +1,29 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: 'Cloud formation template for program-project project' + +Parameters: + TopicName: + Type: String + Description: Name for the SNS topic + Default: 'programproj-topic' + +Resources: + ProgramProjTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: !Ref TopicName + DisplayName: !Ref TopicName + Tags: + - Key: Name + Value: !Ref TopicName + - Key: Environment + Value: Sample + +Outputs: + SNSTopicName: + Description: Name of the SNS topic + Value: !GetAtt ProgramProjTopic.TopicName + + SNSTopicArn: + Description: ARN of the SNS topic + Value: !Ref ProgramProjTopic \ No newline at end of file diff --git a/Aspire/Aspire.AppHost/appsettings.json b/Aspire/Aspire.AppHost/appsettings.json index 1584c608..5055c494 100644 --- a/Aspire/Aspire.AppHost/appsettings.json +++ b/Aspire/Aspire.AppHost/appsettings.json @@ -7,6 +7,9 @@ } }, "ApiGateway": { - "Ports": [4441, 4442, 4443, 4444, 4445] + "Ports": [ 4441, 4442, 4443, 4444, 4445 ] + }, + "LocalStack": { + "UseLocalStack": true } } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index ed6f5c17..e980f521 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -13,6 +13,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "A EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Gateway", "Service.Gateway\Service.Gateway.csproj", "{7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Storage", "Service.Storage\Service.Storage.csproj", "{38FD6B58-76B3-4EEC-BF2C-F37E0B4D0055}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost.Tests", "Aspire.AppHost.Tests\Aspire.AppHost.Tests.csproj", "{D6D100F4-5267-431E-977D-254192A49518}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +43,14 @@ Global {7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F9E88E0-BDDC-444F-A20D-B312AD79AD6C}.Release|Any CPU.Build.0 = Release|Any CPU + {38FD6B58-76B3-4EEC-BF2C-F37E0B4D0055}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {38FD6B58-76B3-4EEC-BF2C-F37E0B4D0055}.Debug|Any CPU.Build.0 = Debug|Any CPU + {38FD6B58-76B3-4EEC-BF2C-F37E0B4D0055}.Release|Any CPU.ActiveCfg = Release|Any CPU + {38FD6B58-76B3-4EEC-BF2C-F37E0B4D0055}.Release|Any CPU.Build.0 = Release|Any CPU + {D6D100F4-5267-431E-977D-254192A49518}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6D100F4-5267-431E-977D-254192A49518}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D6D100F4-5267-431E-977D-254192A49518}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6D100F4-5267-431E-977D-254192A49518}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.Api/Broker/IProducerService.cs b/Service.Api/Broker/IProducerService.cs new file mode 100644 index 00000000..188d3587 --- /dev/null +++ b/Service.Api/Broker/IProducerService.cs @@ -0,0 +1,8 @@ +using Service.Api.Entity; + +namespace Service.Api.Broker; + +public interface IProducerService +{ + public Task SendMessage(ProgramProject pp); +} diff --git a/Service.Api/Broker/SnsPublisherService.cs b/Service.Api/Broker/SnsPublisherService.cs new file mode 100644 index 00000000..42fe79cc --- /dev/null +++ b/Service.Api/Broker/SnsPublisherService.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; +using Service.Api.Entity; + +namespace Service.Api.Broker; + +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(ProgramProject pp) + { + try + { + var json = JsonSerializer.Serialize(pp); + var request = new PublishRequest + { + Message = json, + TopicArn = _topicArn + }; + var response = await client.PublishAsync(request); + if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) logger.LogInformation($"Programproj {pp.Id} was sent to storage via SNS"); + else throw new Exception($"SNS returned {response.HttpStatusCode}"); + } + catch (Exception e) + { + logger.LogError(e, "Unable to send programproj through sns topic"); + } + } +} diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index 3efb018f..b32df542 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -3,7 +3,10 @@ using Service.Api.Generator; using Service.Api.Redis; using Service.Api.Services; +using Amazon.SimpleNotificationService; +using LocalStack.Client.Extensions; using StackExchange.Redis; +using Service.Api.Broker; var builder = WebApplication.CreateBuilder(args); @@ -25,6 +28,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddScoped(); +builder.Services.AddAwsService(); + var app = builder.Build(); app.MapHealthChecks("health"); diff --git a/Service.Api/Service.Api.csproj b/Service.Api/Service.Api.csproj index c3988582..0ee66d50 100644 --- a/Service.Api/Service.Api.csproj +++ b/Service.Api/Service.Api.csproj @@ -8,7 +8,9 @@ + + diff --git a/Service.Api/Services/ProgramProjectCacheService.cs b/Service.Api/Services/ProgramProjectCacheService.cs index 64d528ae..d42b1679 100644 --- a/Service.Api/Services/ProgramProjectCacheService.cs +++ b/Service.Api/Services/ProgramProjectCacheService.cs @@ -1,4 +1,5 @@ using Bogus; +using Service.Api.Broker; using Service.Api.Entity; using Service.Api.Generator; using Service.Api.Redis; @@ -10,7 +11,7 @@ namespace Service.Api.Services; /// Provides cached access to instances. /// Generates a new project if it is not found in Redis. /// -public class ProgramProjectCacheService(RedisService cacheService, Faker faker) +public class ProgramProjectCacheService(RedisService cacheService, Faker faker, IProducerService producerService) { /// /// Returns a cached by ID, @@ -32,6 +33,7 @@ public async Task GetOrGenerateAsync(int id) } var newProject = faker.Generate(); newProject.Id = id; + await producerService.SendMessage(newProject); try { await cacheService.SetAsync(key, newProject, TimeSpan.FromHours(12)); diff --git a/Service.Storage/Broker/SnsSubscriptionService.cs b/Service.Storage/Broker/SnsSubscriptionService.cs new file mode 100644 index 00000000..6ebf9669 --- /dev/null +++ b/Service.Storage/Broker/SnsSubscriptionService.cs @@ -0,0 +1,36 @@ +using System.Net; +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; + +namespace Service.Storage.Broker; + +public class SnsSubscriptionService(IAmazonSimpleNotificationService snsClient, IConfiguration configuration, ILogger logger) +{ + /// + /// Unique topic sns identifier + /// + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] ?? throw new KeyNotFoundException("SNS topic link was not found in configuration"); + + /// + /// tries to subscribe on sns topic + /// + public async Task SubscribeEndpoint() + { + logger.LogInformation("Sending subscride request for {topic}", _topicArn); + var endpoint = configuration["AWS:Resources:SNSUrl"]; + logger.LogInformation($"SNS Endpoint: {endpoint}"); + + 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); + } +} diff --git a/Service.Storage/Controllers/S3StorageController.cs b/Service.Storage/Controllers/S3StorageController.cs new file mode 100644 index 00000000..cacfcc1d --- /dev/null +++ b/Service.Storage/Controllers/S3StorageController.cs @@ -0,0 +1,63 @@ +using System.Text.Json.Nodes; +using System.Text; +using Microsoft.AspNetCore.Mvc; +using Service.Storage.Storage; + +namespace Service.Storage.Controllers; + +/// +/// Controller to deal with S3 +/// +/// Service to deal witg S3 +/// Logger +[ApiController] +[Route("api/s3")] +public class S3StorageController(IS3Service s3Service, ILogger logger) : ControllerBase +{ + /// + /// Getting files from s3 + /// + /// List with files keys + [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); + } + } + + /// + /// Gets files string + /// + /// Key of the file + /// File string representation + [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); + } + } +} diff --git a/Service.Storage/Controllers/SnsSubscriberController.cs b/Service.Storage/Controllers/SnsSubscriberController.cs new file mode 100644 index 00000000..6c7bab25 --- /dev/null +++ b/Service.Storage/Controllers/SnsSubscriberController.cs @@ -0,0 +1,68 @@ +using Amazon.SimpleNotificationService.Util; +using System.Text; +using Microsoft.AspNetCore.Mvc; +using Service.Storage.Storage; + +namespace Service.Storage.Controllers; + +/// +/// Controller for getting messages from sns +/// +/// Service to work with S3 +/// Logger obviously +[ApiController] +[Route("api/sns")] +public class SnsSubscriberController(ILogger logger, IS3Service s3service) : ControllerBase +{ + /// + /// Webhook that gets notifications from sns topic + /// + /// + /// As well confirms subscription at the messaging beginning. Returns cod 200 anyway + /// + [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); + + 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(); + } +} diff --git a/Service.Storage/Program.cs b/Service.Storage/Program.cs new file mode 100644 index 00000000..125d4f05 --- /dev/null +++ b/Service.Storage/Program.cs @@ -0,0 +1,26 @@ +using Amazon.SimpleNotificationService; +using LocalStack.Client.Extensions; +using Service.Storage.Broker; +using Service.Storage.Storage; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); + +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddScoped(); +builder.Services.AddAwsService(); +builder.AddMinioClient("programproj-minio"); +builder.Services.AddScoped(); + +var app = builder.Build(); +await app.Services.CreateScope().ServiceProvider.GetRequiredService().SubscribeEndpoint(); +await app.Services.CreateScope().ServiceProvider.GetRequiredService().EnsureBucketExists(); + +app.MapDefaultEndpoints(); + +app.MapControllers(); + +app.Run(); diff --git a/Service.Storage/Properties/launchSettings.json b/Service.Storage/Properties/launchSettings.json new file mode 100644 index 00000000..46ea9703 --- /dev/null +++ b/Service.Storage/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:54503", + "sslPort": 44373 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5202", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7160;http://localhost:5202", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Service.Storage/Service.Storage.csproj b/Service.Storage/Service.Storage.csproj new file mode 100644 index 00000000..7f6965f6 --- /dev/null +++ b/Service.Storage/Service.Storage.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + diff --git a/Service.Storage/Storage/IS3Service.cs b/Service.Storage/Storage/IS3Service.cs new file mode 100644 index 00000000..6cb5bdf3 --- /dev/null +++ b/Service.Storage/Storage/IS3Service.cs @@ -0,0 +1,30 @@ +using System.Text.Json.Nodes; + +namespace Service.Storage.Storage; + +public interface IS3Service +{ + /// + /// sends file in storage + /// + /// saving file string representation + public Task UploadFile(string fileData); + + /// + /// getting all files list from the storage + /// + /// Paths list + public Task> GetFileList(); + + /// + /// Getting string representation of a file from the storage + /// + /// Bucket file path + /// Read file string representation + public Task DownloadFile(string filePath); + + /// + /// Creates s3 bucket if needed + /// + public Task EnsureBucketExists(); +} diff --git a/Service.Storage/Storage/S3MinioService.cs b/Service.Storage/Storage/S3MinioService.cs new file mode 100644 index 00000000..fa80ac40 --- /dev/null +++ b/Service.Storage/Storage/S3MinioService.cs @@ -0,0 +1,124 @@ +using System.Net; +using System.Text.Json.Nodes; +using System.Text; +using Minio; +using Minio.DataModel.Args; + +namespace Service.Storage.Storage; + +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 list = new List(); + 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); + + 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 {file} onto {bucket}", id, _bucketName); + var request = new PutObjectArgs() + .WithBucket(_bucketName) + .WithStreamData(stream) + .WithObjectSize(bytes.Length) + .WithObject($"programproj_{id}.json"); + + var response = await client.PutObjectAsync(request); + + if (response.ResponseStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to upload {file}: {code}", id, response.ResponseStatusCode); + return false; + } + logger.LogInformation("Finished uploading {file} to {bucket}", id, _bucketName); + return true; + } + + /// + public async Task DownloadFile(string key) + { + logger.LogInformation("Began downloading {file} from {bucket}", key, _bucketName); + + try + { + var memoryStream = new MemoryStream(); + + var request = new GetObjectArgs() + .WithBucket(_bucketName) + .WithObject(key) + .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}", key); + throw new InvalidOperationException($"Error occurred downloading {key} - 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 ", key); + 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; + } + } +} diff --git a/Service.Storage/appsettings.Development.json b/Service.Storage/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Service.Storage/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Service.Storage/appsettings.json b/Service.Storage/appsettings.json new file mode 100644 index 00000000..d66fe771 --- /dev/null +++ b/Service.Storage/appsettings.json @@ -0,0 +1,13 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Settings": { + "MessageBroker": "", + "S3Hosting": "" + } +} diff --git a/StorageService/Program.cs b/StorageService/Program.cs new file mode 100644 index 00000000..ef24d4dc --- /dev/null +++ b/StorageService/Program.cs @@ -0,0 +1,38 @@ +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +// Add services to the container. + +var app = builder.Build(); + +app.MapDefaultEndpoints(); + +// Configure the HTTP request pipeline. + +app.UseHttpsRedirection(); + +var summaries = new[] +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + +app.MapGet("/weatherforecast", () => +{ + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return forecast; +}); + +app.Run(); + +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} diff --git a/StorageService/Properties/launchSettings.json b/StorageService/Properties/launchSettings.json new file mode 100644 index 00000000..6280f628 --- /dev/null +++ b/StorageService/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49047", + "sslPort": 44355 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5215", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "https://localhost:7009;http://localhost:5215", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/StorageService/Service.Storage.csproj b/StorageService/Service.Storage.csproj new file mode 100644 index 00000000..00f936c9 --- /dev/null +++ b/StorageService/Service.Storage.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/StorageService/StorageService.http b/StorageService/StorageService.http new file mode 100644 index 00000000..660a032c --- /dev/null +++ b/StorageService/StorageService.http @@ -0,0 +1,6 @@ +@StorageService_HostAddress = http://localhost:5215 + +GET {{StorageService_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/StorageService/appsettings.Development.json b/StorageService/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/StorageService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/StorageService/appsettings.json b/StorageService/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/StorageService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From e8263464bcb92b38f574cc686e8bfbdff8eaabe5 Mon Sep 17 00:00:00 2001 From: Airfix982 <25roter52@gmail.com> Date: Sun, 19 Apr 2026 03:32:40 +0400 Subject: [PATCH 12/12] ref: small refactoring --- Aspire.AppHost.Tests/UnitTest1.cs | 9 --------- Client.Wasm/Components/StudentCard.razor | 4 ++-- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Aspire.AppHost.Tests/UnitTest1.cs b/Aspire.AppHost.Tests/UnitTest1.cs index 5662a8e5..ceea9789 100644 --- a/Aspire.AppHost.Tests/UnitTest1.cs +++ b/Aspire.AppHost.Tests/UnitTest1.cs @@ -29,15 +29,6 @@ public async Task InitializeAsync() }); } - /// - /// , : - /// - /// - /// S3 - /// , - /// - /// - /// [Theory] [InlineData("Development")] public async Task TestPipeline(string envName) diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 70dd80dc..fe1204dc 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №2 "Балансировщик" + Номер №3 "брокер+хранилище" Вариант №42 "Программный проект" Выполнена Кадников Николай 6513 - Ссылка на форк + Ссылка на форк