-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathProgram.cs
More file actions
62 lines (49 loc) · 1.45 KB
/
Program.cs
File metadata and controls
62 lines (49 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using PatientApp.Generator.Services;
using PatientApp.ServiceDefaults;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.AddRedisDistributedCache("redis");
builder.Services.AddSingleton<PatientGenerator>();
builder.Services.AddScoped<PatientService>();
var allowedOrigins = builder.Configuration
.GetSection("Cors:AllowedOrigins")
.Get<string[]>();
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins(allowedOrigins!)
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
app.UseCors();
app.UseSerilogRequestLogging();
app.MapDefaultEndpoints();
app.MapGet("/patient", async (
int id,
PatientService service,
ILogger<Program> logger,
CancellationToken cancellationToken) =>
{
logger.LogInformation("Received request for patient with ID: {id}", id);
if (id <= 0)
{
logger.LogWarning("Received invalid ID: {id}", id);
return Results.BadRequest(new { error = "ID must be a positive number"});
}
try
{
var application = await service.GetByIdAsync(id, cancellationToken);
return Results.Ok(application);
}
catch (Exception ex)
{
logger.LogError(ex, "Error while getting patient {id}", id);
return Results.Problem("An error occurred while processing the request");
}
})
.WithName("GetPatient");
app.Run();