Skip to content

Commit ee80c7c

Browse files
committed
refactor: To top level statement
1 parent 3b9e102 commit ee80c7c

File tree

1 file changed

+70
-81
lines changed

1 file changed

+70
-81
lines changed

src/LinkDotNet.Blog.Web/Program.cs

Lines changed: 70 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Threading.Tasks;
21
using Blazored.Toast;
32
using HealthChecks.UI.Client;
3+
using LinkDotNet.Blog.Web;
44
using LinkDotNet.Blog.Web.Authentication.OpenIdConnect;
55
using LinkDotNet.Blog.Web.Authentication.Dummy;
66
using LinkDotNet.Blog.Web.RegistrationExtensions;
@@ -9,98 +9,87 @@
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Hosting;
1111

12-
namespace LinkDotNet.Blog.Web;
12+
var builder = WebApplication.CreateBuilder(args);
13+
RegisterServices(builder);
1314

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)
1520
{
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());
2032

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();
2344

24-
await app.RunAsync();
25-
}
45+
builder.Services.AddAntiforgery();
2646

27-
private static void RegisterServices(WebApplicationBuilder builder)
47+
if (builder.Environment.IsDevelopment())
48+
{
49+
builder.Services.UseDummyAuthentication();
50+
}
51+
else
2852
{
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();
6354
}
55+
}
56+
57+
static void ConfigureApp(WebApplication app)
58+
{
59+
app.UseSecurityHeaders();
6460

65-
private static void ConfigureApp(WebApplication app)
61+
if (app.Environment.IsDevelopment())
6662
{
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, })
8777
.RequireAuthorization();
8878

89-
app.UseStatusCodePagesWithReExecute("/NotFound");
90-
91-
app.UseRouting();
79+
app.UseStatusCodePagesWithReExecute("/NotFound");
9280

93-
app.UseUserCulture();
81+
app.UseRouting();
9482

95-
app.UseAuthentication();
96-
app.UseAuthorization();
97-
98-
app.UseAntiforgery();
83+
app.UseUserCulture();
9984

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();
10695
}

0 commit comments

Comments
 (0)