-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
75 lines (56 loc) · 2.22 KB
/
Program.cs
File metadata and controls
75 lines (56 loc) · 2.22 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
using Microsoft.EntityFrameworkCore;
using StepUp.Data;
var builder = WebApplication.CreateBuilder(args);
// Connection string holen (appsettings oder ENV ConnectionStrings__DefaultConnection)
var cs = builder.Configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrWhiteSpace(cs))
throw new InvalidOperationException("Missing ConnectionStrings:DefaultConnection");
// Render liefert oft postgres:// oder postgresql:// -> in Npgsql Format umwandeln
cs = NormalizePostgresConnectionString(cs);
builder.Services.AddDbContext<AppDbContext>(o => o.UseNpgsql(cs));
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Migration beim Start anwenden
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.Migrate();
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// Auf Render ist HTTPS-Termination vorne dran -> optional auskommentieren, wenn Warnung nervt
// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
static string NormalizePostgresConnectionString(string input)
{
input = input.Trim();
// Schon Key-Value? Dann passt es.
if (input.Contains("Host=", StringComparison.OrdinalIgnoreCase))
return input;
// URL-Form: postgres://user:pass@host:port/db
if (input.StartsWith("postgres://", StringComparison.OrdinalIgnoreCase) ||
input.StartsWith("postgresql://", StringComparison.OrdinalIgnoreCase))
{
var uri = new Uri(input);
var userInfo = uri.UserInfo.Split(':', 2);
var user = Uri.UnescapeDataString(userInfo[0]);
var pass = userInfo.Length > 1 ? Uri.UnescapeDataString(userInfo[1]) : "";
var db = uri.AbsolutePath.TrimStart('/');
// Render Postgres -> SSL meist required
return
$"Host={uri.Host};" +
(uri.Port > 0 ? $"Port={uri.Port};" : "") +
$"Database={db};Username={user};Password={pass};" +
"Ssl Mode=Require;Trust Server Certificate=true";
}
return input;
}