-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (47 loc) · 2.72 KB
/
Copy pathProgram.cs
File metadata and controls
57 lines (47 loc) · 2.72 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
using Microsoft.EntityFrameworkCore;
using Rask.Cache;
using Rask.Example.EfCore;
using Rask.Example.EfCore.Features.Catalog.Shared;
using Rask.Mail;
using Rask.Server;
using Rask.SQLite;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRask();
// IDbContextFactory rather than a scoped DbContext: a Rask Server live session is long-lived over
// a WebSocket, so a session-scoped DbContext would outlive any unit of work (and a DbContext is
// neither thread-safe nor meant to be long-lived). Each slice opens a short-lived context per
// operation via the factory. RASK_DB_PATH lets the E2E fixture point at an isolated temp file.
var dbPath = builder.Configuration["RASK_DB_PATH"] ?? "raskExampleCatalog.db";
// UseRaskSqlite rather than UseSqlite: same provider, but every connection opens with the production
// pragmas (WAL journal, foreign_keys on, a busy_timeout) instead of SQLite's bare defaults.
builder.Services.AddDbContextFactory<CatalogDbContext>(options =>
options.UseRaskSqlite($"Data Source={dbPath}"));
// Transactional email on the same SQLite database. No SMTP here: PickupDirectory makes the background
// MailProcessor write each sent message as an .eml file (RASK_MAIL_PICKUP lets the E2E fixture point at an
// isolated temp dir), and a short poll keeps the demo responsive. Set o.Smtp in production to send for real.
builder.Services.AddRaskMail<CatalogDbContext>(o =>
{
o.From = "demo@rask.example";
o.FromName = "Rask EF Core demo";
o.PickupDirectory = builder.Configuration["RASK_MAIL_PICKUP"] ?? "mail-pickup";
o.PollInterval = TimeSpan.FromSeconds(1);
});
// A read-through cache on the same SQLite database — GetOrCreateAsync stores each result as a CacheEntry row
// and the background CachePurger sweeps expired rows (a short interval keeps the demo tidy).
builder.Services.AddRaskCache<CatalogDbContext>(o => o.PurgeInterval = TimeSpan.FromSeconds(30));
// Match what `rask deploy` allows: it sends SIGTERM and SIGKILLs 20s later, so the app budgets under that.
// ServicesStopConcurrently is the other half — stopped one at a time (the .NET default) each hosted
// service's own shutdown grace sums inside this one budget instead of overlapping. See docs/deployment.md.
builder.Services.Configure<HostOptions>(options =>
{
options.ShutdownTimeout = TimeSpan.FromSeconds(15);
options.ServicesStopConcurrently = true;
});
var app = builder.Build();
// Create the schema and seed sample data once at startup.
await CatalogSeeder.SeedAsync(app.Services.GetRequiredService<IDbContextFactory<CatalogDbContext>>());
// MapStaticAssets serves wwwroot + package _content as routed endpoints (outrank the SPA catch-all).
app.MapStaticAssets();
app.UseRouting();
app.UseRask<App>();
app.Run();