diff --git a/labs/.DS_Store b/labs/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/labs/.DS_Store differ diff --git a/labs/lab_3/example_1.cs b/labs/lab_3/example_1.cs new file mode 100644 index 0000000..5e35792 --- /dev/null +++ b/labs/lab_3/example_1.cs @@ -0,0 +1,76 @@ +List tasks = new() +{ + new TaskEntity() { Uuid= Guid.NewGuid(), Subject = "Сдать третью лабу", IsCompleted = false }, +}; + +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +// Массив сущностей. +app.MapGet("/", () => +{ + return tasks.Select(t => new TaskEntityDto() + { + Uuid = t.Uuid, + Subject = t.Subject, + IsCompleted = t.IsCompleted, + }); +}); + +// Подробности сущности. +app.MapGet("/{uuid}", (Guid uuid) => +{ + var task = tasks.FirstOrDefault(t => t.Uuid == uuid); + if (task == null) + return Results.NotFound(); + return Results.Json(new TaskEntityDto() + { + Uuid = task.Uuid, + Subject = task.Subject, + IsCompleted = task.IsCompleted, + }); +}); + +// Создание сущности. +app.MapPost("/", () => { throw new NotImplementedException(); }); + +// Изменение сущности. +app.MapPut("/{uuid}", () => { throw new NotImplementedException(); }); + +// Удаление сущности. +app.MapPost("/", () => { throw new NotImplementedException(); }); + +app.Run(); + +/// +/// Сама сущность. +/// +public class TaskEntity +{ + public Guid Uuid { get; set; } + public string Subject { get; set; } = string.Empty; + public bool IsCompleted { get; set; } +} + +/// +/// DTO-класс для описания сущности в API. +/// +public class TaskEntityDto : TaskEntity { } + +/// +/// Запрос на создание сущности. +/// +public class TaskCreateRequest +{ + public string Subject { get; set; } = string.Empty; + public bool IsCompleted { get; set; } +} + +/// +/// Запрос на изменение сущности. +/// +public class TaskEditRequest +{ + public string Subject { get; set; } = string.Empty; + public bool IsCompleted { get; set; } +} diff --git a/labs/lab_3/example_2.cs b/labs/lab_3/example_2.cs new file mode 100644 index 0000000..1de87e7 --- /dev/null +++ b/labs/lab_3/example_2.cs @@ -0,0 +1,26 @@ +var builder = WebApplication.CreateBuilder(args); +builder.Services.AddHttpClient(); // Добавляем библиотеку по работе с HTTP. +var app = builder.Build(); + +// Да, по заданию надо будет запрашивать метод /{uuid}. Тут запрашивается список. +app.MapGet("/", async (IHttpClientFactory httpClientFactory) => +{ + var remoteHost = "http://localhost:5158"; // Адрес, по которому развёрнут микросервис с задачами. В docker compose тут будет имя service. + var client = httpClientFactory.CreateClient(); + var response = await client.GetFromJsonAsync>(remoteHost); + if (response == null) + return Results.BadRequest(); + return Results.Ok($"Соседний микросервис отдал следующие задачи: {string.Join(", ", response.Select(i => i.Subject))}"); +}); + +app.Run(); + +/// +/// DTO-класс для описания сущности из другого микросервиса. +/// +public class TaskEntityDto +{ + public Guid Uuid { get; set; } + public string Subject { get; set; } = string.Empty; + public bool IsCompleted { get; set; } +} diff --git a/labs/lab_3/example_nginx.conf b/labs/lab_3/example_nginx.conf new file mode 100644 index 0000000..9d3e2b3 --- /dev/null +++ b/labs/lab_3/example_nginx.conf @@ -0,0 +1,21 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + location /test/ { + proxy_pass http://test:80/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-Prefix /test; + } + + location /admin/ { + proxy_pass http://admin:80/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-Prefix /admin; + } +}