-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (92 loc) · 3.5 KB
/
Copy pathProgram.cs
File metadata and controls
117 lines (92 loc) · 3.5 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using CarpentryShop.Services;
using CarpentryShop.Areas.Identity.Data;
using Microsoft.AspNetCore.HttpOverrides;
using System.Net;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
// Add service defaults (Aspire)
builder.AddServiceDefaults();
// Configure PostgreSQL database connection
// When running with Aspire, the connection string is automatically configured
// When running standalone, it falls back to environment variable or configuration
var connectionString = builder.Configuration.GetConnectionString("carpentrydb");
if (string.IsNullOrEmpty(connectionString))
{
// Fallback to legacy environment variable for backward compatibility
connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION");
if (string.IsNullOrEmpty(connectionString))
{
throw new Exception(
"Database connection not configured. Either:\n" +
"1. Run with Aspire: install Aspire CLI (curl -sSL https://aspire.dev/install.sh | bash) and use 'aspire run' or 'just run'\n" +
"2. Set DB_CONNECTION environment variable with PostgreSQL connection string\n" +
"3. Configure ConnectionStrings:carpentrydb in appsettings.json"
);
}
// Register DbContext manually when not using Aspire
builder.Services.AddDbContext<CarpentryShopIdentityDbContext>(options =>
options.UseNpgsql(connectionString));
}
else
{
// Use Aspire PostgreSQL integration when connection string is provided by Aspire
builder.AddNpgsqlDbContext<CarpentryShopIdentityDbContext>("carpentrydb");
}
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<CarpentryShopIdentityDbContext>();
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var emailSection = builder.Configuration.GetSection("EmailSettings");
builder.Services.Configure<EmailSettings>(emailSection);
builder.Services.AddSingleton<IEmailSender, EmailSender>();
builder.Services.AddRazorPages(options =>
{
options.Conventions.AuthorizePage("/Admin/Index");
});
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.KnownProxies.Add(IPAddress.Parse("127.0.0.1"));
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<CarpentryShopIdentityDbContext>();
context.Database.EnsureCreated();
// DbInitializer.Initialize(context);
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// using Microsoft.AspNetCore.HttpOverrides;
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
app.UseAuthorization();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
await SampleData.InitializeAsync(services);
}
app.MapRazorPages();
// Map Aspire default endpoints
app.MapDefaultEndpoints();
app.Run();