-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (40 loc) · 1.16 KB
/
Copy pathProgram.cs
File metadata and controls
55 lines (40 loc) · 1.16 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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Prometheus;
using TaskApi.Data;
var builder = WebApplication.CreateBuilder(args);
const string corsPolicyName = "AllowFrontend";
builder.Services.AddDbContext<AppDbContext>(options =>
{
options.UseNpgsql(
builder.Configuration.GetConnectionString("DefaultConnection")
);
});
builder.Services.AddControllers();
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy("OK"));
builder.Services.AddCors(options =>
{
options.AddPolicy(corsPolicyName, policy =>
{
policy
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Auto-apply EF migrations at startup
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.Migrate();
}
// Use CORS (ORDER MATTERS)
app.UseCors(corsPolicyName);
app.UseRouting();
app.UseHttpMetrics(); // request duration, count, etc.
app.MapControllers();
app.MapHealthChecks("/health");
app.MapMetrics(); // exposes /metrics
app.Run();