-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleModuleHostExtensions.cs
More file actions
336 lines (296 loc) · 13.8 KB
/
SimpleModuleHostExtensions.cs
File metadata and controls
336 lines (296 loc) · 13.8 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using SimpleModule.Core.Authorization;
using SimpleModule.Core.Constants;
using SimpleModule.Core.Exceptions;
using SimpleModule.Core.Health;
using SimpleModule.Core.Inertia;
using SimpleModule.Core.Menu;
using SimpleModule.Core.RateLimiting;
using SimpleModule.Core.Security;
using SimpleModule.Database;
using SimpleModule.Database.Health;
using SimpleModule.Database.Interceptors;
using SimpleModule.DevTools;
using SimpleModule.Hosting.Inertia;
using SimpleModule.Hosting.Middleware;
using SimpleModule.Hosting.RateLimiting;
using Wolverine;
using ZiggyCreatures.Caching.Fusion;
namespace SimpleModule.Hosting;
public static partial class SimpleModuleHostExtensions
{
/// <summary>
/// Registers all non-generated SimpleModule infrastructure services.
/// Called by the source-generated <c>AddSimpleModule()</c> method.
/// </summary>
public static WebApplicationBuilder AddSimpleModuleInfrastructure(
this WebApplicationBuilder builder,
Action<SimpleModuleOptions>? configure = null
)
{
var options = new SimpleModuleOptions();
configure?.Invoke(options);
builder.Services.AddSingleton(options);
builder.Services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(5));
BridgeAspireConnectionString(builder.Configuration);
options.DatabaseProvider = ValidateDatabaseConfiguration(builder.Configuration);
builder.Services.Configure<ForwardedHeadersOptions>(fhOptions =>
{
fhOptions.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// Allow any proxy in containerized/cloud environments
fhOptions.KnownIPNetworks.Clear();
fhOptions.KnownProxies.Clear();
});
builder.Services.AddProblemDetails();
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
if (options.EnableSwagger)
{
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
}
builder.Services.AddSingleton<IInertiaPageRenderer, HtmlFileInertiaPageRenderer>();
// Unified caching abstraction (IFusionCache) shared across all modules.
// Stampede-safe GetOrSetAsync built in; five-minute default entry duration.
builder
.Services.AddFusionCache()
.WithDefaultEntryOptions(o => o.Duration = TimeSpan.FromMinutes(5));
// Wolverine: in-process messaging only. Handlers are auto-discovered
// from loaded assemblies. No external transports, no message persistence.
builder.Host.UseWolverine(_ => { });
// Lazy<IMessageBus> lets services break factory-lambda cycles
// (e.g. SettingsService ↔ AuditingMessageBus via ISettingsContracts).
builder.Services.AddScoped(sp => new Lazy<IMessageBus>(() =>
sp.GetRequiredService<IMessageBus>()
));
builder.Services.AddScoped<InertiaSharedData>();
// Required by EntityInterceptor to access the current HTTP context
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<ICurrentUser, HttpContextCurrentUser>();
// Entity framework interceptors for automatic entity field population
builder.Services.AddScoped<ISaveChangesInterceptor, EntityInterceptor>();
builder.Services.AddScoped<ISaveChangesInterceptor, DomainEventInterceptor>();
builder.Services.AddScoped<ISaveChangesInterceptor, EntityChangeInterceptor>();
// Authentication is configured by modules via their ConfigureServices
// (e.g., OpenIddict registers SmartAuth policy scheme).
// Register a baseline so the middleware pipeline works even without an auth module.
builder.Services.AddAuthentication();
// Authenticated-by-default. Endpoints that genuinely need to be public
// (login, health probes, error/404 fallbacks) opt out with .AllowAnonymous().
// Without a fallback policy, plain app.MapGet(...) outside a module group
// is silently public — the wrong default for a business app.
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
builder.Services.AddAntiforgery();
// Register default IPublicMenuProvider if no module provides one
builder.Services.TryAddScoped<IPublicMenuProvider, DefaultPublicMenuProvider>();
builder.Services.AddScoped<ICspNonce, CspNonce>();
if (options.EnableHealthChecks)
{
builder
.Services.AddHealthChecks()
.AddCheck<DatabaseHealthCheck>(
HealthCheckConstants.DatabaseCheckName,
tags: [HealthCheckConstants.ReadyTag]
)
.AddCheck<ModuleHealthCheck>(
HealthCheckConstants.ModulesCheckName,
tags: [HealthCheckConstants.ReadyTag]
);
}
if (options.EnableDevTools && builder.Environment.IsDevelopment())
{
builder.Services.AddDevTools();
}
if (options.ValidateModuleGraph)
{
builder.Services.AddHostedService<ModuleGraphValidator>();
}
return builder;
}
/// <summary>
/// Configures all non-generated SimpleModule middleware.
/// Called by the source-generated <c>UseSimpleModule()</c> method.
/// </summary>
public static async Task UseSimpleModuleInfrastructure(this WebApplication app)
{
// Database initialization
// SQLite (file-based) always needs auto-initialization since the DB file may not exist.
// Managed databases (PostgreSQL, SQL Server) skip this in production — apply migrations externally.
var smOptions = app.Services.GetRequiredService<SimpleModuleOptions>();
if (
!app.Environment.IsProduction()
|| smOptions.DatabaseProvider == DatabaseProvider.Sqlite
)
{
using var scope = app.Services.CreateScope();
var infos = scope.ServiceProvider.GetServices<ModuleDbContextInfo>();
foreach (var info in infos)
{
if (scope.ServiceProvider.GetService(info.DbContextType) is not DbContext db)
continue;
// DbContexts with EF migrations use MigrateAsync; those without (e.g. scaffolded
// module contexts that ship no migrations) fall back to EnsureCreatedAsync so
// their tables exist on first run. Only the Host context typically ships
// migrations; module contexts rely on the unified HostDbContext for schema.
if (info.ModuleName == DatabaseConstants.HostModuleName)
{
if (db.Database.GetMigrations().Any())
{
await db.Database.MigrateAsync();
}
else
{
await db.Database.EnsureCreatedAsync();
}
}
}
}
app.UseForwardedHeaders();
var errorHtmlPath = Path.Combine(app.Environment.WebRootPath, "error.html");
var errorHtmlBytes = File.Exists(errorHtmlPath)
? await File.ReadAllBytesAsync(errorHtmlPath)
: null;
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
if (context.Response.HasStarted)
return;
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "text/html";
if (errorHtmlBytes is not null)
{
await context.Response.Body.WriteAsync(errorHtmlBytes);
}
else
{
await context.Response.WriteAsync("<h1>500 Internal Server Error</h1>");
}
});
});
var options = app.Services.GetRequiredService<SimpleModuleOptions>();
if (options.EnableSwagger && app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var isDevelopment = app.Environment.IsDevelopment();
var cspOptions = options.Csp;
// Directives never change after startup, so build everything except the
// per-request nonce once. Per request we only do a single concat.
var connectSrc = isDevelopment
? $"'self' ws: wss: https: {string.Join(' ', cspOptions.ConnectSources)}"
: $"'self' https: {string.Join(' ', cspOptions.ConnectSources)}";
var cspPrefix = "default-src 'none'; script-src 'self' 'nonce-";
var cspSuffix =
$"'; style-src 'self' 'unsafe-inline' fonts.googleapis.com rsms.me {string.Join(' ', cspOptions.StyleSources)}; "
+ $"font-src 'self' fonts.gstatic.com rsms.me {string.Join(' ', cspOptions.FontSources)}; "
+ $"worker-src 'self' blob: {string.Join(' ', cspOptions.WorkerSources)}; "
+ $"connect-src {connectSrc}; "
+ $"img-src 'self' data: https: {string.Join(' ', cspOptions.ImgSources)}; "
+ "object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none';";
var cspSuffixHttps = cspSuffix + " upgrade-insecure-requests;";
app.Use(
async (context, next) =>
{
var nonce = context.RequestServices.GetRequiredService<ICspNonce>().Value;
var isHttps = context.Request.IsHttps;
context.Response.OnStarting(() =>
{
var headers = context.Response.Headers;
headers["X-Content-Type-Options"] = "nosniff";
headers["X-Frame-Options"] = "SAMEORIGIN";
headers["Referrer-Policy"] = "strict-origin-when-cross-origin";
headers["X-Permitted-Cross-Domain-Policies"] = "none";
headers["Content-Security-Policy"] = string.Concat(
cspPrefix,
nonce,
isHttps ? cspSuffixHttps : cspSuffix
);
return Task.CompletedTask;
});
await next();
}
);
// Vite dev server proxy — intercepts /@vite/, /@fs/, .tsx requests and
// proxies them to the Vite dev server. Also sets HttpContext.Items["ViteDevServer"]
// so downstream middleware (Inertia renderer) can adapt the HTML.
if (options.EnableDevTools && isDevelopment)
{
app.UseMiddleware<ViteDevMiddleware>();
}
app.UseInertia();
UseStaticFileCaching(app);
// MapStaticAssets registers endpoints, which would otherwise inherit the
// RequireAuthenticatedUser fallback policy — that breaks JS bundle / CSS /
// favicon loads on anonymous pages like /Identity/Account/Login. Static
// files are intentionally public.
app.MapStaticAssets().AllowAnonymous();
app.UseAuthentication();
app.UseAuthorization();
app.UseSimpleModuleRateLimiting();
app.UseMiddleware<InertiaLayoutDataMiddleware>();
if (options.EnableDevTools && app.Environment.IsDevelopment())
{
app.MapLiveReload();
}
// Module middleware is added by the source-generated UseSimpleModule()
// via IModule.ConfigureMiddleware() calls.
UseHomePageRewrite(app);
app.UseAntiforgery();
if (options.EnableHealthChecks)
{
// Health probes are intentionally anonymous so kubelet / load balancers
// can hit them without credentials. The framework owns these — do not
// remove the .AllowAnonymous() calls when sweeping anonymous routes
// out of an application.
app.MapHealthChecks(
RouteConstants.HealthLive,
new HealthCheckOptions { Predicate = _ => false }
)
.AllowAnonymous();
app.MapHealthChecks(
RouteConstants.HealthReady,
new HealthCheckOptions
{
Predicate = check => check.Tags.Contains(HealthCheckConstants.ReadyTag),
ResponseWriter = WriteHealthCheckResponse,
}
)
.AllowAnonymous();
}
app.MapGet("/error/{statusCode:int}", (int statusCode) => RenderErrorPage(statusCode))
.AllowAnonymous()
.ExcludeFromDescription();
// Catch-all for unmatched GET requests — renders a 404 Inertia page
// for browser navigation to non-existent URLs. Does NOT fire on
// matched endpoints that return bare 401/403 from auth, so API tests
// that verify bare status codes remain unaffected.
app.MapFallback(
"{**catchAll}",
(HttpContext context) =>
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return RenderErrorPage(404);
}
)
.AllowAnonymous()
.ExcludeFromDescription();
}
}