2
2
using System . Globalization ;
3
3
using Microsoft . AspNetCore . Builder ;
4
4
using Serilog ;
5
+ using NetAPI . Features . Posts ;
6
+ using Microsoft . OpenApi . Models ;
7
+ using NetAPI . Common . Api ;
5
8
6
9
[ ExcludeFromCodeCoverage ]
7
10
public static class WebAppExtensions
@@ -18,8 +21,6 @@ public static WebApplication ConfigureApplication(this WebApplication app)
18
21
app . UseAuthentication ( ) ;
19
22
app . UseAuthorization ( ) ;
20
23
21
- // -----------------------------------------------------------------------------------------
22
-
23
24
if ( IsDevelopment )
24
25
{
25
26
app . UseSwagger ( ) ;
@@ -40,46 +41,68 @@ public static WebApplication ConfigureApplication(this WebApplication app)
40
41
// use rate limiter
41
42
app . UseRateLimiter ( ) ;
42
43
43
- // Ensure Database is Created
44
- // using (var scope = app.Services.CreateScope())
45
- // {
46
- // var dbContext = scope.ServiceProvider.GetRequiredService<ExpenseDbContext>();
47
- // dbContext.Database.Migrate();
48
- // }
44
+ app . EnsureDatabaseCreated ( ) . Wait ( ) ;
49
45
46
+ app . AppendHeaders ( ) ;
50
47
51
- // Prevent Cross-Site Scripting (XSS) & Clickjacking
52
- // Use Content Security Policy (CSP) and X-Frame-Options:
48
+ app . AddEndpoints ( ) ;
53
49
54
- app . Use ( async ( context , next ) =>
55
- {
56
- context . Response . Headers . Append ( "X-Content-Type-Options" , "nosniff" ) ;
57
- context . Response . Headers . Append ( "X-Frame-Options" , "DENY" ) ;
58
- context . Response . Headers . Append ( "Content-Security-Policy" , "default-src 'self'" ) ;
59
- await next ( ) ;
60
- } ) ;
50
+ return app ;
51
+ }
61
52
62
53
54
+ private static async Task EnsureDatabaseCreated ( this WebApplication app )
55
+ {
56
+ // using var scope = app.Services.CreateScope();
57
+ // var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
58
+ // await db.Database.MigrateAsync();
59
+ await Task . CompletedTask ;
60
+ }
61
+
62
+ private static void AddEndpoints ( this WebApplication app )
63
+ {
63
64
app . MapGet ( "/" , ( ) => "Hello, World!" ) ;
64
- app . MapGet ( "/health" , ( ) => "Healthy" ) ;
65
+ // app.MapGet("/health", () => "Healthy");
65
66
66
- app . MapGet ( "/secure" , ( ) => "You are authenticated!" )
67
- . RequireAuthorization ( ) ; // Protect this endpoint
67
+ // app.MapGet("/secure", () => "You are authenticated!")
68
+ // .RequireAuthorization(); // Protect this endpoint
68
69
69
- app . MapGet ( "/admin" , ( ) => "Welcome Admin!" )
70
- . RequireAuthorization ( policy => policy . RequireRole ( "admin" ) ) ;
70
+ // app.MapGet("/admin", () => "Welcome Admin!")
71
+ // .RequireAuthorization(policy => policy.RequireRole("admin"));
71
72
73
+ app . MapPostEndpoints ( ) ;
72
74
75
+ }
73
76
74
- #region MinimalApi
77
+ private static void MapPostEndpoints ( this IEndpointRouteBuilder app )
78
+ {
79
+ var endpoint = app . MapPublicGroup ( "/tasks" ) ;
80
+ endpoint . MapEndpoint < GetPosts > ( ) ;
81
+ }
75
82
76
- // _ = app.MapVersionEndpoints();
77
- // _ = app.MapAuthorEndpoints();
78
- // _ = app.MapMovieEndpoints();
79
- // _ = app.MapReviewEndpoints();
83
+ private static RouteGroupBuilder MapPublicGroup ( this IEndpointRouteBuilder app , string ? prefix = null )
84
+ {
85
+ return app . MapGroup ( prefix ?? string . Empty )
86
+ . AllowAnonymous ( ) ;
87
+ }
80
88
81
- #endregion MinimalApi
89
+ private static RouteGroupBuilder MapPrivateGroup ( this IEndpointRouteBuilder app , string ? prefix = null )
90
+ {
91
+ return app . MapGroup ( prefix ?? string . Empty )
92
+ . RequireAuthorization ( ) ;
93
+ }
82
94
83
- return app ;
95
+ private static void AppendHeaders ( this WebApplication app )
96
+ {
97
+ // Prevent Cross-Site Scripting (XSS) & Clickjacking
98
+ // Use Content Security Policy (CSP) and X-Frame-Options:
99
+
100
+ app . Use ( async ( context , next ) =>
101
+ {
102
+ context . Response . Headers . Append ( "X-Content-Type-Options" , "nosniff" ) ;
103
+ context . Response . Headers . Append ( "X-Frame-Options" , "DENY" ) ;
104
+ context . Response . Headers . Append ( "Content-Security-Policy" , "default-src 'self'" ) ;
105
+ await next ( ) ;
106
+ } ) ;
84
107
}
85
108
}
0 commit comments