-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathProgram.cs
34 lines (26 loc) · 1.3 KB
/
Program.cs
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
using Microsoft.EntityFrameworkCore;
using AspireShop.CatalogDb;
using AspireShop.CatalogDbManager;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.AddNpgsqlDbContext<CatalogDbContext>("catalogdb", null,
optionsBuilder => optionsBuilder.UseNpgsql(npgsqlBuilder =>
npgsqlBuilder.MigrationsAssembly(typeof(Program).Assembly.GetName().Name)));
builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing.AddSource(CatalogDbInitializer.ActivitySourceName));
builder.Services.AddSingleton<CatalogDbInitializer>();
builder.Services.AddHostedService(sp => sp.GetRequiredService<CatalogDbInitializer>());
builder.Services.AddHealthChecks()
.AddCheck<CatalogDbInitializerHealthCheck>("DbInitializer", null);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapPost("/reset-db", async (CatalogDbContext dbContext, CatalogDbInitializer dbInitializer, CancellationToken cancellationToken) =>
{
// Delete and recreate the database. This is useful for development scenarios to reset the database to its initial state.
await dbContext.Database.EnsureDeletedAsync(cancellationToken);
await dbInitializer.InitializeDatabaseAsync(dbContext, cancellationToken);
});
}
app.MapDefaultEndpoints();
await app.RunAsync();