From 5d6e90ed322810c3b72bb64218165bb5de2bdb9d Mon Sep 17 00:00:00 2001 From: born Date: Sat, 28 Oct 2023 11:44:28 +0400 Subject: [PATCH] Added 3rd lab. --- labs/.DS_Store | Bin 0 -> 6148 bytes labs/lab_3/example_1.cs | 76 ++++++++++++++++++++++++++++++++++ labs/lab_3/example_2.cs | 26 ++++++++++++ labs/lab_3/example_nginx.conf | 21 ++++++++++ 4 files changed, 123 insertions(+) create mode 100644 labs/.DS_Store create mode 100644 labs/lab_3/example_1.cs create mode 100644 labs/lab_3/example_2.cs create mode 100644 labs/lab_3/example_nginx.conf diff --git a/labs/.DS_Store b/labs/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 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; + } +}