diff --git a/Client.Wasm/Client.Wasm.csproj b/Client.Wasm/Client.Wasm.csproj index 0ba9f90c..1c371bf1 100644 --- a/Client.Wasm/Client.Wasm.csproj +++ b/Client.Wasm/Client.Wasm.csproj @@ -10,6 +10,10 @@ + + + + diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..0e0bce87 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер № 3 + Вариант № 52 + Выполнена Томашайтисом Павлом 6513 + Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index 4dda7c04..d319d30b 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:5000/course-management" } \ No newline at end of file diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln deleted file mode 100644 index cb48241d..00000000 --- a/CloudDevelopment.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -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 -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {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 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {90FE6B04-8381-437E-893A-FEBA1DA10AEE} - EndGlobalSection -EndGlobal diff --git a/CourseManagement.ApiGateway/CourseManagement.ApiGateway.csproj b/CourseManagement.ApiGateway/CourseManagement.ApiGateway.csproj new file mode 100644 index 00000000..e87f0645 --- /dev/null +++ b/CourseManagement.ApiGateway/CourseManagement.ApiGateway.csproj @@ -0,0 +1,18 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + diff --git a/CourseManagement.ApiGateway/LoadBalancers/QueryBased.cs b/CourseManagement.ApiGateway/LoadBalancers/QueryBased.cs new file mode 100644 index 00000000..39f312fd --- /dev/null +++ b/CourseManagement.ApiGateway/LoadBalancers/QueryBased.cs @@ -0,0 +1,44 @@ +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.Values; + +namespace CourseManagement.ApiGateway.LoadBalancers; + +/// +/// Балансировщик нагрузки на основе запроса +/// +/// Логгер +/// Список сервисов +public class QueryBased(ILogger logger, List services) : ILoadBalancer +{ + /// + /// Тип балансировщика нагрузки + /// + public string Type => nameof(QueryBased); + + /// + /// Метод выбора сервиса на основе запроса + /// + /// HTTP запрос + /// Выбранный сервис + public Task> LeaseAsync(HttpContext httpContext) + { + var idString = httpContext.Request.Query["id"].FirstOrDefault(); + + if (!int.TryParse(idString, out var id)) + id = 0; + + var service = services[id % services.Count]; + + logger.LogInformation("Request {ResourceId} sent to service on port {servicePort}", id, service.HostAndPort.DownstreamPort); + + return Task.FromResult>( + new OkResponse(service.HostAndPort)); + } + + /// + /// Метод очистки ресурсов для сервиса + /// + /// Параметры сервиса + public void Release(ServiceHostAndPort hostAndPort) { } +} diff --git a/CourseManagement.ApiGateway/Program.cs b/CourseManagement.ApiGateway/Program.cs new file mode 100644 index 00000000..ae37569a --- /dev/null +++ b/CourseManagement.ApiGateway/Program.cs @@ -0,0 +1,40 @@ +using CourseManagement.ApiGateway.LoadBalancers; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +// Подключение конфига Ocelot +builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); + +// Добавление Ocelot с Query Based балансировщиком нагрузки +builder.Services.AddOcelot() + .AddCustomLoadBalancer((serviceProvider, downstreamRoute, serviceDiscoveryProvider) => + { + var logger = serviceProvider.GetRequiredService>(); + + var services = serviceDiscoveryProvider.GetAsync().GetAwaiter().GetResult().ToList(); + + return new QueryBased(logger, services); + }); + +// CORS +builder.Services.AddCors(options => +{ + options.AddPolicy("AllowClient", policy => + { + policy.AllowAnyOrigin() + .WithMethods("GET") + .WithHeaders("Content-Type"); + }); +}); + +var app = builder.Build(); + +app.UseCors("AllowClient"); + +await app.UseOcelot(); + +app.Run(); \ No newline at end of file diff --git a/CourseManagement.ApiGateway/Properties/launchSettings.json b/CourseManagement.ApiGateway/Properties/launchSettings.json new file mode 100644 index 00000000..48bc6592 --- /dev/null +++ b/CourseManagement.ApiGateway/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5256", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7253;http://localhost:5256", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CourseManagement.ApiGateway/appsettings.Development.json b/CourseManagement.ApiGateway/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CourseManagement.ApiGateway/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CourseManagement.ApiGateway/appsettings.json b/CourseManagement.ApiGateway/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/CourseManagement.ApiGateway/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/CourseManagement.ApiGateway/ocelot.json b/CourseManagement.ApiGateway/ocelot.json new file mode 100644 index 00000000..4d8d8351 --- /dev/null +++ b/CourseManagement.ApiGateway/ocelot.json @@ -0,0 +1,72 @@ +{ + "Routes": [ + { + "DownstreamPathTemplate": "/course-management", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 8081 + }, + { + "Host": "localhost", + "Port": 8082 + }, + { + "Host": "localhost", + "Port": 8083 + }, + { + "Host": "localhost", + "Port": 8084 + }, + { + "Host": "localhost", + "Port": 8085 + } + ], + "UpstreamPathTemplate": "/course-management", + "UpstreamHttpMethod": [ "GET" ], + "LoadBalancerOptions": { + "Type": "QueryBased", + "Key": "course-api" + } + }, + { + "DownstreamPathTemplate": "/health", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 8081 + }, + { + "Host": "localhost", + "Port": 8082 + }, + { + "Host": "localhost", + "Port": 8083 + }, + { + "Host": "localhost", + "Port": 8084 + }, + { + "Host": "localhost", + "Port": 8085 + } + ], + "UpstreamPathTemplate": "/health", + "UpstreamHttpMethod": [ "GET" ], + "Priority": 1, + "LoadBalancerOptions": { + "Type": "QueryBased", + "Key": "course-api-health" + } + } + ], + "GlobalConfiguration": { + "BaseUrl": "https://localhost:5000" + } +} \ No newline at end of file diff --git a/CourseManagement.ApiService/Cache/CacheService.cs b/CourseManagement.ApiService/Cache/CacheService.cs new file mode 100644 index 00000000..8ecf5a57 --- /dev/null +++ b/CourseManagement.ApiService/Cache/CacheService.cs @@ -0,0 +1,66 @@ +using System.Text.Json; +using Microsoft.Extensions.Caching.Distributed; + +namespace CourseManagement.ApiService.Cache; + +/// +/// Универсальный сервис для взаимодействия с кэшем +/// +/// Логгер +/// Кэш +public class CacheService(ILogger> logger, IDistributedCache cache) : ICacheService +{ + /// + /// Время жизни данных в кэше + /// + private static readonly double _cacheDuration = 5; + + /// + public async Task FetchAsync(string key, int id) + { + var cacheKey = $"{key}:{id}"; + + try + { + var cached = await cache.GetStringAsync(cacheKey); + if (cached != null) + { + var obj = JsonSerializer.Deserialize(cached); + + logger.LogInformation("Cache hit for {EntityType} {ResourceId}", typeof(T).Name, id); + + return obj; + } + + logger.LogInformation("Cache miss for {EntityType} {ResourceId}", typeof(T).Name, id); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Сache is unavailable"); + } + + return default; + } + + /// + public async Task StoreAsync(string key, int id, T entity) + { + var cacheKey = $"{key}:{id}"; + + var options = new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(_cacheDuration) + }; + + try + { + var serialized = JsonSerializer.Serialize(entity); + await cache.SetStringAsync(cacheKey, serialized, options); + logger.LogInformation("{EntityType} {ResourceId} cached", typeof(T).Name, id); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Сache is unavailable"); + } + } +} diff --git a/CourseManagement.ApiService/Cache/ICacheService.cs b/CourseManagement.ApiService/Cache/ICacheService.cs new file mode 100644 index 00000000..89f44703 --- /dev/null +++ b/CourseManagement.ApiService/Cache/ICacheService.cs @@ -0,0 +1,23 @@ +namespace CourseManagement.ApiService.Cache; + +/// +/// Универсальный интерфейс для работы с кэшем +/// +public interface ICacheService +{ + /// + /// Асинхронный метод для извлечения данных из кэша + /// + /// Ключ для сущности + /// Идентификатор сущности + /// Объект или null при его отсутствии + public Task FetchAsync(string key, int id); + + /// + /// Асинхронный метод для внесения данных в кэш + /// + /// Ключ для сущности + /// Идентификатор сущности + /// Кешируемая сущность + public Task StoreAsync(string key, int id, T entity); +} diff --git a/CourseManagement.ApiService/Controllers/CourseController.cs b/CourseManagement.ApiService/Controllers/CourseController.cs new file mode 100644 index 00000000..379f259f --- /dev/null +++ b/CourseManagement.ApiService/Controllers/CourseController.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Mvc; +using CourseManagement.ApiService.Entities; +using CourseManagement.ApiService.Services; + +namespace CourseManagement.ApiService.Controllers; + +/// +/// Контроллер для сущности типа Курс +/// +/// Логгер +/// Сервис для сущности типа Курс +[ApiController] +[Route("course-management")] +public class CourseController(ILogger logger, ICourseService courseService) : ControllerBase +{ + /// + /// Обработчик GET-запроса на генерацию курса + /// + /// Идентификатор курса + /// Сгенерированный курс + [HttpGet] + [ProducesResponseType(200)] + public async Task> GetCourse(int? id) + { + logger.LogInformation("Processing request for course {ResourceId}", id); + + var course = await courseService.GetCourse(id ?? 0); + + return Ok(course); + } +} \ No newline at end of file diff --git a/CourseManagement.ApiService/CourseManagement.ApiService.csproj b/CourseManagement.ApiService/CourseManagement.ApiService.csproj new file mode 100644 index 00000000..03c6774b --- /dev/null +++ b/CourseManagement.ApiService/CourseManagement.ApiService.csproj @@ -0,0 +1,23 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + + diff --git a/CourseManagement.ApiService/Entities/Course.cs b/CourseManagement.ApiService/Entities/Course.cs new file mode 100644 index 00000000..fa85bbd5 --- /dev/null +++ b/CourseManagement.ApiService/Entities/Course.cs @@ -0,0 +1,70 @@ +using System.Text.Json.Serialization; + +namespace CourseManagement.ApiService.Entities; + + +/// +/// Сущность типа курс +/// +public class Course +{ + /// + /// Идентификатор курса + /// + [JsonPropertyName("id")] + public int Id { get; set; } + + /// + /// Название курса + /// + [JsonPropertyName("title")] + public string Title { get; set; } = string.Empty; + + /// + /// Лектор + /// + [JsonPropertyName("lector")] + public string Lector { get; set; } = string.Empty; + + /// + /// Дата начала курса + /// + [JsonPropertyName("startDate")] + public DateOnly StartDate { get; set; } + + /// + /// Дата окончания курса + /// + [JsonPropertyName("endDate")] + public DateOnly EndDate { get; set; } + + /// + /// Максимальное число студентов для курса + /// + [JsonPropertyName("maxStudents")] + public int MaxStudents { get; set; } + + /// + /// Текущее число студентов курса + /// + [JsonPropertyName("enrolledStudents")] + public int EnrolledStudents { get; set; } + + /// + /// Выдача сертификата + /// + [JsonPropertyName("hasSertificate")] + public bool HasSertificate { get; set; } + + /// + /// Стоимость курса + /// + [JsonPropertyName("price")] + public decimal Price { get; set; } + + /// + /// Рейтинг + /// + [JsonPropertyName("rating")] + public int Rating { get; set; } +} diff --git a/CourseManagement.ApiService/Generator/CourseGenerator.cs b/CourseManagement.ApiService/Generator/CourseGenerator.cs new file mode 100644 index 00000000..8262d96c --- /dev/null +++ b/CourseManagement.ApiService/Generator/CourseGenerator.cs @@ -0,0 +1,77 @@ +using Bogus; +using CourseManagement.ApiService.Entities; + +namespace CourseManagement.ApiService.Generator; + +/// +/// Генератор для сущности типа Курс +/// +/// Логгер +public class CourseGenerator(ILogger logger) : ICourseGenerator +{ + /// + /// Список названий курсов + /// + private static readonly string[] _courseTitles = { + "Разработка корпоративных приложений", + "Алгоритмы и структуры данных", + "Базы данных", + "Веб-разработка", + "Машинное обучение", + "Основы информационной безопасности", + "Математический анализ", + "Линейная алгебра и геометрия", + "Математическая статистика", + "Теория вероятностей", + "Физика", + "Микроэкономика", + "Макроэкономика", + "Нейронные сети и глубокое обучение", + "Сети и системы передачи информации", + "Философия", + "История России", + "Дискретная математика", + "Прикладное программирование", + "Методы оптимизации", + "Теория графов", + "Технологии программирования", + "Основы веб-приложений", + "Безопасность вычислительных сетей", + "Низкоуровневое программирование", + "Системное программирование", + "Криптография", + "Криптопротоколы", + "Жизненный цикл", + "Цифровая обработка сигналов", + "Цифровая обработка изображений", + "Основы программиования", + "Объектно-ориентированное программирование", + "Форензика", + "Компьютерная алгебра", + }; + + /// + /// Экземпляр генератора данных + /// + private static readonly Faker _courseFaker = new Faker("ru") + .RuleFor(c => c.Title, f => f.PickRandom(_courseTitles)) + .RuleFor(c => c.Lector, f => f.Name.FullName()) + .RuleFor(c => c.StartDate, f => DateOnly.FromDateTime(f.Date.Future(1))) + .RuleFor(c => c.EndDate, (f, c) => c.StartDate.AddMonths(f.Random.Int(1, 6))) + .RuleFor(c => c.MaxStudents, f => f.Random.Int(10, 100)) + .RuleFor(c => c.EnrolledStudents, (f, c) => f.Random.Int(0, c.MaxStudents)) + .RuleFor(c => c.HasSertificate, f => f.Random.Bool()) + .RuleFor(c => c.Price, f => f.Finance.Amount(5000, 100000, 2)) + .RuleFor(c => c.Rating, f => f.Random.Int(1, 5)); + + /// + public Course GenerateOne(int? id) + { + var course = _courseFaker.Generate(); + course.Id = id ?? new Randomizer().Int(1, 100000); + + logger.LogInformation("Course {ResourceId} generated", id); + + return course; + } +} \ No newline at end of file diff --git a/CourseManagement.ApiService/Generator/ICourseGenerator.cs b/CourseManagement.ApiService/Generator/ICourseGenerator.cs new file mode 100644 index 00000000..05a68ebd --- /dev/null +++ b/CourseManagement.ApiService/Generator/ICourseGenerator.cs @@ -0,0 +1,16 @@ +using CourseManagement.ApiService.Entities; + +namespace CourseManagement.ApiService.Generator; + +/// +/// Интерфейс генератора для сущности типа Курс +/// +public interface ICourseGenerator +{ + /// + /// Метод для генерации одного экземпляра сущности типа Курс + /// + /// Идентификатор курса (если не указан, генерируется автоматически) + /// Сгенерированный курс + public Course GenerateOne(int? id = null); +} diff --git a/CourseManagement.ApiService/Messaging/IPublisherService.cs b/CourseManagement.ApiService/Messaging/IPublisherService.cs new file mode 100644 index 00000000..3f21219e --- /dev/null +++ b/CourseManagement.ApiService/Messaging/IPublisherService.cs @@ -0,0 +1,16 @@ +namespace CourseManagement.ApiService.Messaging; + +/// +/// Универсальный интерфейс сервиса для отправки генерируемых сущностей в брокер сообщений +/// +public interface IPublisherService +{ + /// + /// Метод для отправки сообщения в брокер + /// + /// Идентификатор отправляемой сущности + /// Отправляемая сущность + /// Токен для возможности отмены ожидания после отправки + /// Успешность операции отправки + public Task SendMessage(int id, object entity, CancellationToken cancellationToken = default); +} diff --git a/CourseManagement.ApiService/Messaging/SnsPublisherService.cs b/CourseManagement.ApiService/Messaging/SnsPublisherService.cs new file mode 100644 index 00000000..1a4facde --- /dev/null +++ b/CourseManagement.ApiService/Messaging/SnsPublisherService.cs @@ -0,0 +1,51 @@ +using System.Net; +using System.Text.Json; +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; + +namespace CourseManagement.ApiService.Messaging; + +/// +/// Универсальный сервис для отправки в брокер сгенерированных сущностей +/// +/// +/// +/// +public class SnsPublisherService(ILogger logger, IAmazonSimpleNotificationService client, IConfiguration configuration) : IPublisherService +{ + /// + /// Идентификатор топика + /// + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] + ?? throw new KeyNotFoundException("SNS topic link was not found in configuration"); + + /// + public async Task SendMessage(int id, object entity, CancellationToken cancellationToken) + { + try + { + var json = JsonSerializer.Serialize(entity); + + var request = new PublishRequest + { + Message = json, + TopicArn = _topicArn, + }; + var response = await client.PublishAsync(request, cancellationToken); + + if (response.HttpStatusCode == HttpStatusCode.OK) + { + logger.LogInformation("{EntityType} {Id} was sent to sink via SNS", entity.GetType(), id); + return true; + } + + logger.LogWarning("SNS returned {StatusCode} for {EntityType} {Id}", response.HttpStatusCode, entity.GetType(), id); + } + catch (Exception ex) + { + logger.LogError(ex, "Unable to send {EntityType} through SNS topic", entity.GetType()); + } + + return false; + } +} diff --git a/CourseManagement.ApiService/Program.cs b/CourseManagement.ApiService/Program.cs new file mode 100644 index 00000000..8571fdbb --- /dev/null +++ b/CourseManagement.ApiService/Program.cs @@ -0,0 +1,56 @@ +using Amazon.SimpleNotificationService; +using CourseManagement.ApiService.Cache; +using CourseManagement.ApiService.Entities; +using CourseManagement.ApiService.Generator; +using CourseManagement.ApiService.Messaging; +using CourseManagement.ApiService.Services; +using LocalStack.Client.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +// Add service defaults +builder.AddServiceDefaults(); + +// Redis +builder.AddRedisDistributedCache("course-cache"); + +// Add services to the container +builder.Services.AddProblemDetails(); +builder.Services.AddOpenApi(); + +// Регистрация LocalStack +builder.Services.AddLocalStack(builder.Configuration); + +// Регистрация AWS сервисов +builder.Services.AddAwsService(); + +// Контроллеры +builder.Services.AddControllers(); + +// Генератор курсов +builder.Services.AddSingleton (); + +// Сервис для взаимодействия с кэшем +builder.Services.AddSingleton, CacheService>(); + +// Сервис для сущности типа Курс +builder.Services.AddSingleton(); + +// Сервис для публикации данных +builder.Services.AddSingleton(); + +var app = builder.Build(); + +app.UseExceptionHandler(); + +// Mapping +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} + +app.MapControllers(); + +app.MapDefaultEndpoints(); + +app.Run(); \ No newline at end of file diff --git a/CourseManagement.ApiService/Properties/launchSettings.json b/CourseManagement.ApiService/Properties/launchSettings.json new file mode 100644 index 00000000..e2374967 --- /dev/null +++ b/CourseManagement.ApiService/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5329", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7170;http://localhost:5329", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CourseManagement.ApiService/Services/CourseService.cs b/CourseManagement.ApiService/Services/CourseService.cs new file mode 100644 index 00000000..ab2a0699 --- /dev/null +++ b/CourseManagement.ApiService/Services/CourseService.cs @@ -0,0 +1,38 @@ +using CourseManagement.ApiService.Cache; +using CourseManagement.ApiService.Entities; +using CourseManagement.ApiService.Generator; +using CourseManagement.ApiService.Messaging; + +namespace CourseManagement.ApiService.Services; + +/// +/// Сервис для сущности типа Курс +/// +/// Логгер +/// Генератор курсов +/// Сервис для взаимодействия с кэшем +/// Сервис для отправки сообщения в брокер +public class CourseService(ILogger logger, ICourseGenerator generator, ICacheService cacheService, IPublisherService publisherService) : ICourseService +{ + /// + /// Константа для ключа кэша + /// + private const string CacheKeyPrefix = "course"; + + /// + public async Task GetCourse(int id) + { + var course = await cacheService.FetchAsync(CacheKeyPrefix, id); + if (course != null) + return course; + + var newCourse = generator.GenerateOne(id); + + await publisherService.SendMessage(id, newCourse); + await cacheService.StoreAsync(CacheKeyPrefix, id, newCourse); + + logger.LogInformation("Course {ResourceId} processed", id); + + return newCourse; + } +} diff --git a/CourseManagement.ApiService/Services/ICourseService.cs b/CourseManagement.ApiService/Services/ICourseService.cs new file mode 100644 index 00000000..a6080b48 --- /dev/null +++ b/CourseManagement.ApiService/Services/ICourseService.cs @@ -0,0 +1,16 @@ +using CourseManagement.ApiService.Entities; + +namespace CourseManagement.ApiService.Services; + +/// +/// Интерфейс сервиса для работы с сущностью Курс +/// +public interface ICourseService +{ + /// + /// Метод для получения курса + /// + /// Идентификатор курса + /// Курс + public Task GetCourse(int id); +} \ No newline at end of file diff --git a/CourseManagement.ApiService/appsettings.Development.json b/CourseManagement.ApiService/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CourseManagement.ApiService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CourseManagement.ApiService/appsettings.json b/CourseManagement.ApiService/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/CourseManagement.ApiService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/CourseManagement.AppHost.Tests/CourseManagement.AppHost.Tests.csproj b/CourseManagement.AppHost.Tests/CourseManagement.AppHost.Tests.csproj new file mode 100644 index 00000000..1abdd48c --- /dev/null +++ b/CourseManagement.AppHost.Tests/CourseManagement.AppHost.Tests.csproj @@ -0,0 +1,33 @@ + + + + net10.0 + enable + enable + false + true + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CourseManagement.AppHost.Tests/IntegrationTest.cs b/CourseManagement.AppHost.Tests/IntegrationTest.cs new file mode 100644 index 00000000..0b9ebba9 --- /dev/null +++ b/CourseManagement.AppHost.Tests/IntegrationTest.cs @@ -0,0 +1,90 @@ +using Aspire.Hosting; +using System.Text.Json; +using Microsoft.Extensions.Logging; +using Xunit.Abstractions; +using CourseManagement.ApiService.Entities; + +namespace CourseManagement.AppHost.Tests; + +/// +/// Интеграционные тесты для проверки микросервисного пайплайна +/// +/// Служба журналирования unit-тестов +public class IntegrationTests(ITestOutputHelper output) : IAsyncLifetime +{ + private IDistributedApplicationTestingBuilder? _builder; + private DistributedApplication? _app; + + /// + public async Task InitializeAsync() + { + var cancellationToken = CancellationToken.None; + _builder = await DistributedApplicationTestingBuilder.CreateAsync(cancellationToken); + _builder.Configuration["DcpPublisher:RandomizePorts"] = "false"; + _builder.Services.AddLogging(logging => + { + logging.AddXUnit(output); + logging.SetMinimumLevel(LogLevel.Debug); + logging.AddFilter("Aspire.Hosting.Dcp", LogLevel.Debug); + logging.AddFilter("Aspire.Hosting", LogLevel.Debug); + }); + } + + /// + /// Тестирование корректности работы сервиса генерации, api-gateway, сервисов взаимодействия с S3 и SNS + /// + [Fact] + public async Task TestAppServices() + { + Assert.NotNull(_builder); + _app = await _builder.BuildAsync(); + await _app.StartAsync(); + + await Task.Delay(10000); + + var id = new Random().Next(1, 100); + + using var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }; + + using var gatewayClient = _app.CreateHttpClient("course-gateway", "http"); + + using var gatewayResponse = await gatewayClient.GetAsync($"/course-management?id={id}"); + + var content = await gatewayResponse.Content.ReadAsStringAsync(); + + Assert.True(gatewayResponse.IsSuccessStatusCode, $"Gateway failed: {gatewayResponse.StatusCode} - {content}"); + + var apiCourse = JsonSerializer.Deserialize(content); + Assert.NotNull(apiCourse); + + await Task.Delay(5000); + + using var storageClient = _app.CreateHttpClient("course-storage", "http"); + + using var listResponse = await storageClient.GetAsync("/api/s3"); + var listContent = await listResponse.Content.ReadAsStringAsync(); + + var courseList = JsonSerializer.Deserialize>(listContent); + Assert.NotNull(courseList); + Assert.NotEmpty(courseList); + + var matchingFile = courseList.FirstOrDefault(f => f.Contains($"course_{id}")); + Assert.NotNull(matchingFile); + + using var s3Response = await storageClient.GetAsync($"/api/s3/{matchingFile}"); + var s3Content = await s3Response.Content.ReadAsStringAsync(); + var s3Course = JsonSerializer.Deserialize(s3Content); + + Assert.NotNull(s3Course); + Assert.Equal(id, s3Course.Id); + Assert.Equivalent(apiCourse, s3Course); + } + + /// + public async Task DisposeAsync() + { + await _app!.StopAsync(); + await _app.DisposeAsync(); + await _builder!.DisposeAsync(); + } +} \ No newline at end of file diff --git a/CourseManagement.AppHost/AppHost.cs b/CourseManagement.AppHost/AppHost.cs new file mode 100644 index 00000000..78c25591 --- /dev/null +++ b/CourseManagement.AppHost/AppHost.cs @@ -0,0 +1,84 @@ +using Amazon; +using Aspire.Hosting.LocalStack.Container; +using Microsoft.Extensions.Configuration; + +var builder = DistributedApplication.CreateBuilder(args); + +// Read configuration for Api Service +var apiServiceConfig = builder.Configuration.GetSection("ApiService"); +var ports = apiServiceConfig.GetSection("Ports").Get>() ?? []; + +// Read configuration for Api Gateway +var apiGatewayConfig = builder.Configuration.GetSection("ApiGateway"); +var gatewayPort = apiGatewayConfig.GetValue("Port"); + +// Read configuration for Localstack, SNS +var localStackPort = builder.Configuration.GetSection("LocalStack").GetValue("Port"); +var cloudFormationTemplate = builder.Configuration.GetSection("LocalStack").GetValue("CloudFormationTemplate") ?? ""; +var snsEndpointUrl = builder.Configuration.GetSection("SNS").GetValue("EndpointURL") ?? ""; + +// Cache (Redis) +var redis = builder.AddRedis("course-cache") + .WithRedisInsight(containerName: "course-insight") + .WithDataVolume(); + +// API Gateway (Ocelot) +var apiGateway = builder.AddProject("course-gateway") + .WithHttpsEndpoint(port: gatewayPort, name: "course-gateway-endpoint", isProxied: false) + .WithExternalHttpEndpoints(); + +// AWS +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +// LocalStack (SNS + S3) +var localStack = builder + .AddLocalStack("course-localstack", awsConfig: awsConfig, configureContainer: container => + { + container.Lifetime = ContainerLifetime.Session; + container.DebugLevel = 1; + container.LogLevel = LocalStackLogLevel.Debug; + container.Port = localStackPort; + container.AdditionalEnvironmentVariables + .Add("DEBUG", "1"); + container.AdditionalEnvironmentVariables + .Add("SNS_CERT_URL_HOST", "sns.eu-central-1.amazonaws.com"); + }); + +// LocalStack Initialization +var awsResources = builder.AddAWSCloudFormationTemplate("course-resources", cloudFormationTemplate, "landplot") + .WithReference(awsConfig); + +// Storage Service (S3 + SNS) +var storage = builder.AddProject("course-storage") + .WithReference(awsResources) + .WithEnvironment("SNS__EndpointURL", snsEndpointUrl) + .WaitFor(awsResources); + + +// API services (Backend) +var serviceId = 1; +foreach (var port in ports) +{ + var apiService = builder.AddProject($"course-api-{serviceId++}") + .WithReference(redis) + .WithHttpsEndpoint(port: port, name: "course-api-endpoint", isProxied: false) + .WithReference(awsResources) + .WaitFor(redis) + .WaitFor(storage); + + apiGateway.WaitFor(apiService); +} +apiGateway.WithHttpHealthCheck("/health"); + +// Client (Frontend) +builder.AddProject("course-wasm") + .WithExternalHttpEndpoints() + .WithHttpHealthCheck("/health") + .WithReference(apiGateway) + .WaitFor(apiGateway); + +builder.UseLocalStack(localStack); + +builder.Build().Run(); \ No newline at end of file diff --git a/CourseManagement.AppHost/CloudFormation/course-template-sns-s3.yaml b/CourseManagement.AppHost/CloudFormation/course-template-sns-s3.yaml new file mode 100644 index 00000000..de98a84f --- /dev/null +++ b/CourseManagement.AppHost/CloudFormation/course-template-sns-s3.yaml @@ -0,0 +1,59 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: 'Cloud formation template for course management project' + +Parameters: + BucketName: + Type: String + Description: Name for the S3 bucket + Default: 'course-bucket' + + TopicName: + Type: String + Description: Name for the SNS topic + Default: 'course-topic' + +Resources: + CourseBucket: + Type: AWS::S3::Bucket + Properties: + BucketName: !Ref BucketName + VersioningConfiguration: + Status: Suspended + Tags: + - Key: Name + Value: !Ref BucketName + - Key: Environment + Value: Sample + PublicAccessBlockConfiguration: + BlockPublicAcls: true + BlockPublicPolicy: true + IgnorePublicAcls: true + RestrictPublicBuckets: true + + CourseTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: !Ref TopicName + DisplayName: !Ref TopicName + Tags: + - Key: Name + Value: !Ref TopicName + - Key: Environment + Value: Sample + +Outputs: + S3BucketName: + Description: Name of the S3 bucket + Value: !Ref CourseBucket + + S3BucketArn: + Description: ARN of the S3 bucket + Value: !GetAtt CourseBucket.Arn + + SNSTopicName: + Description: Name of the SNS topic + Value: !GetAtt CourseTopic.TopicName + + SNSTopicArn: + Description: ARN of the SNS topic + Value: !Ref CourseTopic \ No newline at end of file diff --git a/CourseManagement.AppHost/CourseManagement.AppHost.csproj b/CourseManagement.AppHost/CourseManagement.AppHost.csproj new file mode 100644 index 00000000..b45917a5 --- /dev/null +++ b/CourseManagement.AppHost/CourseManagement.AppHost.csproj @@ -0,0 +1,30 @@ + + + + Exe + net10.0 + enable + enable + 9bbc0bf3-e529-4a0f-833b-49b80c01c009 + + + + + + + + + + + + + + + + + + PreserveNewest + + + + diff --git a/CourseManagement.AppHost/Properties/launchSettings.json b/CourseManagement.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..8bba3b86 --- /dev/null +++ b/CourseManagement.AppHost/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17286;http://localhost:15040", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21283", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23150", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22100" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15040", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19011", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18011", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20240" + } + } + } +} diff --git a/CourseManagement.AppHost/appsettings.Development.json b/CourseManagement.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CourseManagement.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CourseManagement.AppHost/appsettings.json b/CourseManagement.AppHost/appsettings.json new file mode 100644 index 00000000..f7c357a8 --- /dev/null +++ b/CourseManagement.AppHost/appsettings.json @@ -0,0 +1,32 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + }, + "ApiService": { + "Ports": [ + 8081, + 8082, + 8083, + 8084, + 8085 + ] + }, + "ApiGateway": { + "Port": 5000 + }, + "Redis": { + "ConnectionString": "localhost:6379" + }, + "LocalStack": { + "UseLocalStack": true, + "Port": 4566, + "CloudFormationTemplate": "CloudFormation/course-template-sns-s3.yaml" + }, + "SNS": { + "EndpointURL": "http://host.docker.internal:5280/api/sns" + } +} diff --git a/CourseManagement.ServiceDefaults/CourseManagement.ServiceDefaults.csproj b/CourseManagement.ServiceDefaults/CourseManagement.ServiceDefaults.csproj new file mode 100644 index 00000000..eeb71e38 --- /dev/null +++ b/CourseManagement.ServiceDefaults/CourseManagement.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net10.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/CourseManagement.ServiceDefaults/Extensions.cs b/CourseManagement.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..b72c8753 --- /dev/null +++ b/CourseManagement.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/CourseManagement.Storage/Controllers/S3StorageController.cs b/CourseManagement.Storage/Controllers/S3StorageController.cs new file mode 100644 index 00000000..220be3a2 --- /dev/null +++ b/CourseManagement.Storage/Controllers/S3StorageController.cs @@ -0,0 +1,63 @@ +using System.Text; +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Mvc; +using CourseManagement.Storage.Services; + +namespace CourseManagement.Storage.Controllers; + +/// +/// Контроллер для взаимодейсвия с S3 +/// +/// Логгер +/// Сервис для работы с S3 +[ApiController] +[Route("api/s3")] +public class S3StorageController(ILogger logger, IS3Service s3Service) : ControllerBase +{ + /// + /// Обработчик GET-запроса на получение списка хранящихся в S3 файлов + /// + /// Список с ключами файлов + [HttpGet] + [ProducesResponseType(200)] + [ProducesResponseType(500)] + public async Task>> ListFiles() + { + logger.LogInformation("Method {Method} of {Controller} was called", nameof(ListFiles), nameof(S3StorageController)); + try + { + var list = await s3Service.GetFileList(); + logger.LogInformation("Got a list of {Count} files from bucket", list.Count); + return Ok(list); + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occured during {Method} of {Controller}", nameof(ListFiles), nameof(S3StorageController)); + return BadRequest(ex); + } + } + + /// + /// Обработчик GET-запроса на получение строкового представления хранящегося в S3 файла + /// + /// Ключ файла + /// Строковое представление файла + [HttpGet("{key}")] + [ProducesResponseType(200)] + [ProducesResponseType(500)] + public async Task> GetFile(string key) + { + logger.LogInformation("Method {Method} of {Controller} was called", nameof(GetFile), nameof(S3StorageController)); + try + { + var node = await s3Service.DownloadFile(key); + logger.LogInformation("Received json of {Size} bytes", Encoding.UTF8.GetByteCount(node.ToJsonString())); + return Ok(node); + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occured during {Method} of {Controller}", nameof(GetFile), nameof(S3StorageController)); + return BadRequest(ex); + } + } +} \ No newline at end of file diff --git a/CourseManagement.Storage/Controllers/SnsSubscriberController.cs b/CourseManagement.Storage/Controllers/SnsSubscriberController.cs new file mode 100644 index 00000000..832e35e7 --- /dev/null +++ b/CourseManagement.Storage/Controllers/SnsSubscriberController.cs @@ -0,0 +1,65 @@ +using System.Text; +using Microsoft.AspNetCore.Mvc; +using Amazon.SimpleNotificationService.Util; +using CourseManagement.Storage.Services; + +namespace CourseManagement.Storage.Controllers; + +/// +/// Контроллер для приема сообщений от SNS +/// +/// Сервис для работы с S3 +/// Логгер +[ApiController] +[Route("api/sns")] +public class SnsSubscriberController(IS3Service s3Service, ILogger logger) : ControllerBase +{ + /// + /// Обработчик POST-запроса оповещения из SNS топика и подтверждения подписки на топик + /// + [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/CourseManagement.Storage/CourseManagement.Storage.csproj b/CourseManagement.Storage/CourseManagement.Storage.csproj new file mode 100644 index 00000000..e485c854 --- /dev/null +++ b/CourseManagement.Storage/CourseManagement.Storage.csproj @@ -0,0 +1,22 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + diff --git a/CourseManagement.Storage/Messaging/ISubscriberService.cs b/CourseManagement.Storage/Messaging/ISubscriberService.cs new file mode 100644 index 00000000..0316a98c --- /dev/null +++ b/CourseManagement.Storage/Messaging/ISubscriberService.cs @@ -0,0 +1,13 @@ +namespace CourseManagement.Storage.Messaging; + +/// +/// Интерфейс сервиса для подписки на топик +/// +public interface ISubscriberService +{ + /// + /// Метод для подписки на топик + /// + /// Успешность операции подписки + public Task SubscribeEndpoint(); +} diff --git a/CourseManagement.Storage/Messaging/SnsSubscriberService.cs b/CourseManagement.Storage/Messaging/SnsSubscriberService.cs new file mode 100644 index 00000000..b585b048 --- /dev/null +++ b/CourseManagement.Storage/Messaging/SnsSubscriberService.cs @@ -0,0 +1,47 @@ +using System.Net; +using Amazon.SimpleNotificationService; +using Amazon.SimpleNotificationService.Model; + +namespace CourseManagement.Storage.Messaging; + +/// +/// Сервис для подписки на SNS на старте приложения +/// +/// Логгер +/// Клиент SNS +/// Конфигурация +public class SnsSubscriberService(ILogger logger, IAmazonSimpleNotificationService snsClient, IConfiguration configuration) : ISubscriberService +{ + /// + /// Уникальный идентификатор топика SNS + /// + private readonly string _topicArn = configuration["AWS:Resources:SNSTopicArn"] ?? throw new KeyNotFoundException("SNS topic link was not found in configuration"); + + /// + /// URL сервиса SNS + /// + private readonly string _snsEndpointUrl = configuration["SNS:EndpointURL"] ?? throw new KeyNotFoundException("SNS endpoint URL was not found in configuration"); + + /// + public async Task SubscribeEndpoint() + { + logger.LogInformation("Sending subscride request for {topic}", _topicArn); + + var request = new SubscribeRequest + { + TopicArn = _topicArn, + Protocol = "http", + Endpoint = _snsEndpointUrl, + 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); + + return response.HttpStatusCode == HttpStatusCode.OK; + } +} \ No newline at end of file diff --git a/CourseManagement.Storage/Program.cs b/CourseManagement.Storage/Program.cs new file mode 100644 index 00000000..42303d93 --- /dev/null +++ b/CourseManagement.Storage/Program.cs @@ -0,0 +1,47 @@ +using Amazon.S3; +using Amazon.SimpleNotificationService; +using LocalStack.Client.Extensions; +using CourseManagement.Storage.Messaging; +using CourseManagement.Storage.Services; + +var builder = WebApplication.CreateBuilder(args); +builder.AddServiceDefaults(); + +// Контроллеры и Swagger +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +// Настройка LocalStack +builder.Services.AddLocalStack(builder.Configuration); + +// Регистрация AWS сервисов +builder.Services.AddAwsService(); +builder.Services.AddAwsService(); + +// Регистрация SNS и S3 сервисов +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +var app = builder.Build(); + +// Инициализация S3 bucket при старте +using (var scope = app.Services.CreateScope()) +{ + var s3Service = scope.ServiceProvider.GetRequiredService(); + await s3Service.EnsureBucketExists(); + + var subscriptionService = scope.ServiceProvider.GetRequiredService(); + await subscriptionService.SubscribeEndpoint(); +} + +// Swagger +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +// App +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/CourseManagement.Storage/Properties/launchSettings.json b/CourseManagement.Storage/Properties/launchSettings.json new file mode 100644 index 00000000..fe0d6e38 --- /dev/null +++ b/CourseManagement.Storage/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5280", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7210;http://localhost:5280", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CourseManagement.Storage/Services/IS3Service.cs b/CourseManagement.Storage/Services/IS3Service.cs new file mode 100644 index 00000000..6a5d789f --- /dev/null +++ b/CourseManagement.Storage/Services/IS3Service.cs @@ -0,0 +1,34 @@ +using System.Text.Json.Nodes; + +namespace CourseManagement.Storage.Services; + +/// +/// Интерфейс сервиса для управления файлами в объектном хранилище +/// +public interface IS3Service +{ + /// + /// Отправляет файл в хранилище + /// + /// Строковая репрезентация сохраняемого файла + /// Успешность операции отправки файла + public Task UploadFile(string fileData); + + /// + /// Получает список всех файлов из хранилища + /// + /// Список путей к файлам + public Task> GetFileList(); + + /// + /// Получает строковую репрезентацию файла из хранилища + /// + /// Путь к файлу в бакете + /// Строковая репрезентация прочтенного файла + public Task DownloadFile(string filePath); + + /// + /// Создает S3 бакет при необходимости + /// + public Task EnsureBucketExists(); +} diff --git a/CourseManagement.Storage/Services/S3AwsService.cs b/CourseManagement.Storage/Services/S3AwsService.cs new file mode 100644 index 00000000..41ecf4bd --- /dev/null +++ b/CourseManagement.Storage/Services/S3AwsService.cs @@ -0,0 +1,138 @@ +using Amazon.S3; +using Amazon.S3.Model; +using System.Net; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace CourseManagement.Storage.Services; + +/// +/// Сервис для манипуляции файлами в объектном хранилище +/// +/// /// Логер +/// S3 клиент +/// Конфигурация +public class S3AwsService(ILogger logger, IAmazonS3 client, IConfiguration configuration) : IS3Service +{ + /// + /// Идентификатор бакета + /// + private readonly string _bucketName = configuration["AWS:Resources:S3BucketName"] + ?? throw new KeyNotFoundException("S3 bucket name was not found in configuration"); + + /// + public async Task UploadFile(string fileData) + { + var rootNode = JsonNode.Parse(fileData) ?? throw new ArgumentException("Passed string is not a valid JSON"); + var id = rootNode["id"]?.GetValue() ?? throw new ArgumentException("Passed JSON has invalid structure"); + + using var stream = new MemoryStream(); + JsonSerializer.Serialize(stream, rootNode); + stream.Seek(0, SeekOrigin.Begin); + + logger.LogInformation("Began uploading {file} onto {Bucket}", id, _bucketName); + + var request = new PutObjectRequest + { + BucketName = _bucketName, + Key = $"course_{id}.json", + InputStream = stream + }; + + var response = await client.PutObjectAsync(request); + + if (response.HttpStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to upload {File}: {Code}", id, response.HttpStatusCode); + return false; + } + + logger.LogInformation("Finished uploading {File} to {Bucket}", id, _bucketName); + + return true; + } + + /// + public async Task> GetFileList() + { + var list = new List(); + + var request = new ListObjectsV2Request + { + BucketName = _bucketName, + Prefix = "", + Delimiter = ",", + }; + var paginator = client.Paginators.ListObjectsV2(request); + + logger.LogInformation("Began listing files in {Bucket}", _bucketName); + + await foreach (var response in paginator.Responses) + if (response != null && response.S3Objects != null) + foreach (var obj in response.S3Objects) + { + if (obj != null) + list.Add(obj.Key); + else + logger.LogWarning("Received null object from {Bucket}", _bucketName); + } + else + logger.LogWarning("Received null response from {Bucket}", _bucketName); + + return list; + } + + /// + public async Task DownloadFile(string key) + { + logger.LogInformation("Began downloading {File} from {Bucket}", key, _bucketName); + + try + { + var request = new GetObjectRequest + { + BucketName = _bucketName, + Key = key + }; + using var response = await client.GetObjectAsync(request); + + if (response.HttpStatusCode != HttpStatusCode.OK) + { + logger.LogError("Failed to download {File}: {Code}", key, response.HttpStatusCode); + throw new InvalidOperationException($"Error occurred downloading {key} - {response.HttpStatusCode}"); + } + using var reader = new StreamReader(response.ResponseStream, Encoding.UTF8); + + return JsonNode.Parse(reader.ReadToEnd()) ?? throw new InvalidOperationException($"Downloaded document is not a valid JSON"); + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occurred during {File} downloading ", key); + throw; + } + } + + /// + public async Task EnsureBucketExists() + { + logger.LogInformation("Checking whether {Bucket} exists", _bucketName); + + try + { + await client.EnsureBucketExistsAsync(_bucketName); + logger.LogInformation("{Bucket} existence ensured", _bucketName); + } + catch (AmazonS3Exception ex) when (ex.ErrorCode == "BucketAlreadyOwnedByYou" || + ex.ErrorCode == "BucketAlreadyExists" || + ex.StatusCode == HttpStatusCode.Conflict) + { + logger.LogInformation("{Bucket} already exists", _bucketName); + } + catch (Exception ex) + { + logger.LogError(ex, "Unhandled exception occurred during {Bucket} check", _bucketName); + throw; + } + } +} diff --git a/CourseManagement.Storage/appsettings.Development.json b/CourseManagement.Storage/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CourseManagement.Storage/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CourseManagement.Storage/appsettings.json b/CourseManagement.Storage/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/CourseManagement.Storage/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/CourseManagement.slnx b/CourseManagement.slnx new file mode 100644 index 00000000..62bae4f7 --- /dev/null +++ b/CourseManagement.slnx @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/README.md b/README.md index dcaa5eb7..d47f98b4 100644 --- a/README.md +++ b/README.md @@ -1,128 +1,21 @@ -# Современные технологии разработки программного обеспечения -[Таблица с успеваемостью](https://docs.google.com/spreadsheets/d/1an43o-iqlq4V_kDtkr_y7DC221hY9qdhGPrpII27sH8/edit?usp=sharing) - -## Задание -### Цель -Реализация проекта микросервисного бекенда. - -### Задачи -* Реализация межсервисной коммуникации, -* Изучение работы с брокерами сообщений, -* Изучение архитектурных паттернов, -* Изучение работы со средствами оркестрации на примере .NET Aspire, -* Повторение основ работы с системами контроля версий, -* Интеграционное тестирование. - -### Лабораторные работы -
-1. «Кэширование» - Реализация сервиса генерации контрактов, кэширование его ответов -
- -В рамках первой лабораторной работы необходимо: -* Реализовать сервис генерации контрактов на основе Bogus, -* Реализовать кеширование при помощи IDistributedCache и Redis, -* Реализовать структурное логирование сервиса генерации, -* Настроить оркестрацию Aspire. - -
-
-2. «Балансировка нагрузки» - Реализация апи гейтвея, настройка его работы -
- -В рамках второй лабораторной работы необходимо: -* Настроить оркестрацию на запуск нескольких реплик сервиса генерации, -* Реализовать апи гейтвей на основе Ocelot, -* Имплементировать алгоритм балансировки нагрузки согласно варианту. - -
-
-
-3. «Интеграционное тестирование» - Реализация файлового сервиса и объектного хранилища, интеграционное тестирование бекенда -
- -В рамках третьей лабораторной работы необходимо: -* Добавить в оркестрацию объектное хранилище, -* Реализовать файловый сервис, сериализующий сгенерированные данные в файлы и сохраняющий их в объектном хранилище, -* Реализовать отправку генерируемых данных в файловый сервис посредством брокера, -* Реализовать интеграционные тесты, проверяющие корректность работы всех сервисов бекенда вместе. - -
-
-
-4. (Опционально) «Переход на облачную инфраструктуру» - Перенос бекенда в Yandex Cloud -
- -В рамках четвертой лабораторной работы необходимо перенестиервисы на облако все ранее разработанные сервисы: -* Клиент - в хостинг через отдельный бакет Object Storage, -* Сервис генерации - в Cloud Function, -* Апи гейтвей - в Serverless Integration как API Gateway, -* Брокер сообщений - в Message Queue, -* Файловый сервис - в Cloud Function, -* Объектное хранилище - в отдельный бакет Object Storage, - -
-
- -## Задание. Общая часть -**Обязательно**: -* Реализация серверной части на [.NET 8](https://learn.microsoft.com/ru-ru/dotnet/core/whats-new/dotnet-8/overview). -* Оркестрация проектов при помощи [.NET Aspire](https://learn.microsoft.com/ru-ru/dotnet/aspire/get-started/aspire-overview). -* Реализация сервиса генерации данных при помощи [Bogus](https://github.com/bchavez/Bogus). -* Реализация тестов с использованием [xUnit](https://xunit.net/?tabs=cs). -* Создание минимальной документации к проекту: страница на GitHub с информацией о задании, скриншоты приложения и прочая информация. - -**Факультативно**: -* Перенос бекенда на облачную инфраструктуру Yandex Cloud - -Внимательно прочитайте [дискуссии](https://github.com/itsecd/cloud-development/discussions/1) о том, как работает автоматическое распределение на ревью. -Сразу корректно называйте свои pr, чтобы они попали на ревью нужному преподавателю. - -По итогу работы в семестре должна получиться следующая информационная система: -
-C4 диаграмма -Современные_технологии_разработки_ПО_drawio -
- -## Варианты заданий -Номер варианта задания присваивается в начале семестра. Изменить его нельзя. Каждый вариант имеет уникальную комбинацию из предметной области, базы данных и технологии для общения сервиса генерации данных и сервера апи. - -[Список вариантов](https://docs.google.com/document/d/1WGmLYwffTTaAj4TgFCk5bUyW3XKbFMiBm-DHZrfFWr4/edit?usp=sharing) -[Список предметных областей и алгоритмов балансировки](https://docs.google.com/document/d/1PLn2lKe4swIdJDZhwBYzxqFSu0AbY2MFY1SUPkIKOM4/edit?usp=sharing) - -## Схема сдачи - -На каждую из лабораторных работ необходимо сделать отдельный [Pull Request (PR)](https://docs.github.com/en/pull-requests). - -Общая схема: -1. Сделать форк данного репозитория -2. Выполнить задание -3. Сделать PR в данный репозиторий -4. Исправить замечания после code review -5. Получить approve - -## Критерии оценивания - -Конкурентный принцип. -Так как задания в первой лабораторной будут повторяться между студентами, то выделяются следующие показатели для оценки: -1. Скорость разработки -2. Качество разработки -3. Полнота выполнения задания - -Быстрее делаете PR - у вас преимущество. -Быстрее получаете Approve - у вас преимущество. -Выполните нечто немного выходящее за рамки проекта - у вас преимущество. -Не укладываетесь в дедлайн - получаете минимально возможный балл. - -### Шкала оценивания - -- **3 балла** за качество кода, из них: - - 2 балла - базовая оценка - - 1 балл (но не более) можно получить за выполнение любого из следующих пунктов: - - Реализация факультативного функционала - - Выполнение работы раньше других: первые 5 человек из каждой группы, которые сделали PR и получили approve, получают дополнительный балл - -## Вопросы и обратная связь по курсу - -Чтобы задать вопрос по лабораторной, воспользуйтесь [соответствующим разделом дискуссий](https://github.com/itsecd/cloud-development/discussions/categories/questions) или заведите [ишью](https://github.com/itsecd/cloud-development/issues/new). -Если у вас появились идеи/пожелания/прочие полезные мысли по преподаваемой дисциплине, их можно оставить [здесь](https://github.com/itsecd/cloud-development/discussions/categories/ideas). - +# Томашайтис Павел 6513 (вариант 52) +--- +## Учебный курс + +**Внесённые изменения:** +- [x] Cервис генерации контрактов на основе Bogus +- [x] Кеширование при помощи IDistributedCache и Redis +- [x] Сервис для взаимодействия с кешем +- [x] Структурное логирование сервисов +- [x] Оркестрация Aspire +- [x] Поддержка работы с несколькими репликами сервиса генерации +- [x] Api Gateway Ocelot +- [x] QueryBased балансировщик нагрузки +- [x] Брокер сообщений SNS схема (Publish-Subscribe) +- [x] Объектное хранилище S3 +- [x] Файловый сервис для сохранения сгенерированных контрактов в объектном хранилище +- [x] Интеграционные тесты + +image + +image