Added 3rd lab.

This commit is contained in:
born 2023-10-28 11:44:28 +04:00
parent 20210a36a6
commit 5d6e90ed32
4 changed files with 123 additions and 0 deletions

BIN
labs/.DS_Store vendored Normal file

Binary file not shown.

76
labs/lab_3/example_1.cs Normal file
View File

@ -0,0 +1,76 @@
List<TaskEntity> 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();
/// <summary>
/// Сама сущность.
/// </summary>
public class TaskEntity
{
public Guid Uuid { get; set; }
public string Subject { get; set; } = string.Empty;
public bool IsCompleted { get; set; }
}
/// <summary>
/// DTO-класс для описания сущности в API.
/// </summary>
public class TaskEntityDto : TaskEntity { }
/// <summary>
/// Запрос на создание сущности.
/// </summary>
public class TaskCreateRequest
{
public string Subject { get; set; } = string.Empty;
public bool IsCompleted { get; set; }
}
/// <summary>
/// Запрос на изменение сущности.
/// </summary>
public class TaskEditRequest
{
public string Subject { get; set; } = string.Empty;
public bool IsCompleted { get; set; }
}

26
labs/lab_3/example_2.cs Normal file
View File

@ -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<List<TaskEntityDto>>(remoteHost);
if (response == null)
return Results.BadRequest();
return Results.Ok($"Соседний микросервис отдал следующие задачи: {string.Join(", ", response.Select(i => i.Subject))}");
});
app.Run();
/// <summary>
/// DTO-класс для описания сущности из другого микросервиса.
/// </summary>
public class TaskEntityDto
{
public Guid Uuid { get; set; }
public string Subject { get; set; } = string.Empty;
public bool IsCompleted { get; set; }
}

View File

@ -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;
}
}