|
1 | | -using System.Threading.Tasks; |
2 | 1 | using Blazored.Toast; |
3 | 2 | using HealthChecks.UI.Client; |
| 3 | +using LinkDotNet.Blog.Web; |
4 | 4 | using LinkDotNet.Blog.Web.Authentication.OpenIdConnect; |
5 | 5 | using LinkDotNet.Blog.Web.Authentication.Dummy; |
6 | 6 | using LinkDotNet.Blog.Web.RegistrationExtensions; |
|
9 | 9 | using Microsoft.Extensions.DependencyInjection; |
10 | 10 | using Microsoft.Extensions.Hosting; |
11 | 11 |
|
12 | | -namespace LinkDotNet.Blog.Web; |
| 12 | +var builder = WebApplication.CreateBuilder(args); |
| 13 | +RegisterServices(builder); |
13 | 14 |
|
14 | | -public class Program |
| 15 | +await using var app = builder.Build(); |
| 16 | +ConfigureApp(app); |
| 17 | + |
| 18 | +await app.RunAsync(); |
| 19 | +static void RegisterServices(WebApplicationBuilder builder) |
15 | 20 | { |
16 | | - public static async Task Main(string[] args) |
17 | | - { |
18 | | - var builder = WebApplication.CreateBuilder(args); |
19 | | - RegisterServices(builder); |
| 21 | + builder.Services.AddSecurityHeaderPolicies() |
| 22 | + .SetDefaultPolicy(p => |
| 23 | + p.AddDefaultSecurityHeaders() |
| 24 | + .AddCrossOriginEmbedderPolicy(policy => policy.UnsafeNone()) |
| 25 | + .AddPermissionsPolicy(policy => |
| 26 | + { |
| 27 | + policy.AddCamera().None(); |
| 28 | + policy.AddMicrophone().None(); |
| 29 | + policy.AddGeolocation().None(); |
| 30 | + })) |
| 31 | + .AddPolicy("API", p => p.AddDefaultApiSecurityHeaders()); |
20 | 32 |
|
21 | | - await using var app = builder.Build(); |
22 | | - ConfigureApp(app); |
| 33 | + builder.Services |
| 34 | + .AddHostingServices() |
| 35 | + .AddConfiguration() |
| 36 | + .AddRateLimiting() |
| 37 | + .AddApplicationServices() |
| 38 | + .AddStorageProvider(builder.Configuration) |
| 39 | + .AddImageUploadProvider(builder.Configuration) |
| 40 | + .AddBlazoredToast() |
| 41 | + .AddBlazoriseWithBootstrap() |
| 42 | + .AddResponseCompression() |
| 43 | + .AddHealthCheckSetup(); |
23 | 44 |
|
24 | | - await app.RunAsync(); |
25 | | - } |
| 45 | + builder.Services.AddAntiforgery(); |
26 | 46 |
|
27 | | - private static void RegisterServices(WebApplicationBuilder builder) |
| 47 | + if (builder.Environment.IsDevelopment()) |
| 48 | + { |
| 49 | + builder.Services.UseDummyAuthentication(); |
| 50 | + } |
| 51 | + else |
28 | 52 | { |
29 | | - builder.Services.AddSecurityHeaderPolicies() |
30 | | - .SetDefaultPolicy(p => |
31 | | - p.AddDefaultSecurityHeaders() |
32 | | - .AddCrossOriginEmbedderPolicy(policy => policy.UnsafeNone()) |
33 | | - .AddPermissionsPolicy(policy => |
34 | | - { |
35 | | - policy.AddCamera().None(); |
36 | | - policy.AddMicrophone().None(); |
37 | | - policy.AddGeolocation().None(); |
38 | | - })) |
39 | | - .AddPolicy("API", p => p.AddDefaultApiSecurityHeaders()); |
40 | | - |
41 | | - builder.Services |
42 | | - .AddHostingServices() |
43 | | - .AddConfiguration() |
44 | | - .AddRateLimiting() |
45 | | - .AddApplicationServices() |
46 | | - .AddStorageProvider(builder.Configuration) |
47 | | - .AddImageUploadProvider(builder.Configuration) |
48 | | - .AddBlazoredToast() |
49 | | - .AddBlazoriseWithBootstrap() |
50 | | - .AddResponseCompression() |
51 | | - .AddHealthCheckSetup(); |
52 | | - |
53 | | - builder.Services.AddAntiforgery(); |
54 | | - |
55 | | - if (builder.Environment.IsDevelopment()) |
56 | | - { |
57 | | - builder.Services.UseDummyAuthentication(); |
58 | | - } |
59 | | - else |
60 | | - { |
61 | | - builder.Services.UseAuthentication(); |
62 | | - } |
| 53 | + builder.Services.UseAuthentication(); |
63 | 54 | } |
| 55 | +} |
| 56 | + |
| 57 | +static void ConfigureApp(WebApplication app) |
| 58 | +{ |
| 59 | + app.UseSecurityHeaders(); |
64 | 60 |
|
65 | | - private static void ConfigureApp(WebApplication app) |
| 61 | + if (app.Environment.IsDevelopment()) |
66 | 62 | { |
67 | | - app.UseSecurityHeaders(); |
68 | | - |
69 | | - if (app.Environment.IsDevelopment()) |
70 | | - { |
71 | | - app.UseDeveloperExceptionPage(); |
72 | | - } |
73 | | - else |
74 | | - { |
75 | | - app.UseExceptionHandler("/Error"); |
76 | | - app.UseHsts(); |
77 | | - } |
78 | | - |
79 | | - app.UseResponseCompression(); |
80 | | - app.UseHttpsRedirection(); |
81 | | - app.MapStaticAssets(); |
82 | | - |
83 | | - app.MapHealthChecks("/health", new HealthCheckOptions |
84 | | - { |
85 | | - ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, |
86 | | - }) |
| 63 | + app.UseDeveloperExceptionPage(); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + app.UseExceptionHandler("/Error"); |
| 68 | + app.UseHsts(); |
| 69 | + } |
| 70 | + |
| 71 | + app.UseResponseCompression(); |
| 72 | + app.UseHttpsRedirection(); |
| 73 | + app.MapStaticAssets(); |
| 74 | + |
| 75 | + app.MapHealthChecks("/health", |
| 76 | + new HealthCheckOptions { ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, }) |
87 | 77 | .RequireAuthorization(); |
88 | 78 |
|
89 | | - app.UseStatusCodePagesWithReExecute("/NotFound"); |
90 | | - |
91 | | - app.UseRouting(); |
| 79 | + app.UseStatusCodePagesWithReExecute("/NotFound"); |
92 | 80 |
|
93 | | - app.UseUserCulture(); |
| 81 | + app.UseRouting(); |
94 | 82 |
|
95 | | - app.UseAuthentication(); |
96 | | - app.UseAuthorization(); |
97 | | - |
98 | | - app.UseAntiforgery(); |
| 83 | + app.UseUserCulture(); |
99 | 84 |
|
100 | | - app.UseRateLimiter(); |
101 | | - app.MapControllers(); |
102 | | - app.MapRazorPages(); |
103 | | - app.MapRazorComponents<App>() |
104 | | - .AddInteractiveServerRenderMode(); |
105 | | - } |
| 85 | + app.UseAuthentication(); |
| 86 | + app.UseAuthorization(); |
| 87 | + |
| 88 | + app.UseAntiforgery(); |
| 89 | + |
| 90 | + app.UseRateLimiter(); |
| 91 | + app.MapControllers(); |
| 92 | + app.MapRazorPages(); |
| 93 | + app.MapRazorComponents<App>() |
| 94 | + .AddInteractiveServerRenderMode(); |
106 | 95 | } |
0 commit comments