22using System . Globalization ;
33using Microsoft . AspNetCore . Builder ;
44using Serilog ;
5+ using NetAPI . Features . Posts ;
6+ using Microsoft . OpenApi . Models ;
7+ using NetAPI . Common . Api ;
58
69[ ExcludeFromCodeCoverage ]
710public static class WebAppExtensions
@@ -18,8 +21,6 @@ public static WebApplication ConfigureApplication(this WebApplication app)
1821 app . UseAuthentication ( ) ;
1922 app . UseAuthorization ( ) ;
2023
21- // -----------------------------------------------------------------------------------------
22-
2324 if ( IsDevelopment )
2425 {
2526 app . UseSwagger ( ) ;
@@ -40,46 +41,68 @@ public static WebApplication ConfigureApplication(this WebApplication app)
4041 // use rate limiter
4142 app . UseRateLimiter ( ) ;
4243
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 ( ) ;
4945
46+ app . AppendHeaders ( ) ;
5047
51- // Prevent Cross-Site Scripting (XSS) & Clickjacking
52- // Use Content Security Policy (CSP) and X-Frame-Options:
48+ app . AddEndpoints ( ) ;
5349
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+ }
6152
6253
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+ {
6364 app . MapGet ( "/" , ( ) => "Hello, World!" ) ;
64- app . MapGet ( "/health" , ( ) => "Healthy" ) ;
65+ // app.MapGet("/health", () => "Healthy");
6566
66- app . MapGet ( "/secure" , ( ) => "You are authenticated!" )
67- . RequireAuthorization ( ) ; // Protect this endpoint
67+ // app.MapGet("/secure", () => "You are authenticated!")
68+ // .RequireAuthorization(); // Protect this endpoint
6869
69- app . MapGet ( "/admin" , ( ) => "Welcome Admin!" )
70- . RequireAuthorization ( policy => policy . RequireRole ( "admin" ) ) ;
70+ // app.MapGet("/admin", () => "Welcome Admin!")
71+ // .RequireAuthorization(policy => policy.RequireRole("admin"));
7172
73+ app . MapPostEndpoints ( ) ;
7274
75+ }
7376
74- #region MinimalApi
77+ private static void MapPostEndpoints ( this IEndpointRouteBuilder app )
78+ {
79+ var endpoint = app . MapPublicGroup ( "/tasks" ) ;
80+ endpoint . MapEndpoint < GetPosts > ( ) ;
81+ }
7582
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+ }
8088
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+ }
8294
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+ } ) ;
84107 }
85108}
0 commit comments