Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
62 lines (52 loc) · 2.09 KB
/
Copy pathProgram.cs
File metadata and controls
62 lines (52 loc) · 2.09 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 Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using QUEUEDUP.Data;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: false);
// Configurable DB path — set DbPath env var in Azure to "Data Source=/home/data/queuedup.db"
var dbPath = builder.Configuration["DbPath"] ?? "Data Source=queuedup.db";
var dbFile = dbPath.Replace("Data Source=", "").Trim();
var dbDir = Path.GetDirectoryName(Path.GetFullPath(dbFile));
if (!string.IsNullOrEmpty(dbDir)) Directory.CreateDirectory(dbDir);
builder.Services.AddRazorPages();
builder.Services.AddHttpClient();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlite(dbPath));
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromDays(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
});
var app = builder.Build();
// Trust Azure's reverse proxy so HTTPS cookies work correctly
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseRouting();
app.UseSession();
app.UseAuthorization();
app.MapStaticAssets();
app.MapRazorPages().WithStaticAssets();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureCreated();
db.Database.ExecuteSqlRaw(@"
CREATE TABLE IF NOT EXISTS ""EmailSignups"" (
""Id"" INTEGER NOT NULL CONSTRAINT ""PK_EmailSignups"" PRIMARY KEY AUTOINCREMENT,
""Email"" TEXT NOT NULL,
""SignedUpAt"" TEXT NOT NULL
)");
db.Database.ExecuteSqlRaw(@"
CREATE UNIQUE INDEX IF NOT EXISTS ""IX_EmailSignups_Email"" ON ""EmailSignups"" (""Email"")");
}
app.Run();