Skip to content

Commit 5874667

Browse files
committed
endpoint structure added
1 parent dfbcace commit 5874667

File tree

6 files changed

+108
-30
lines changed

6 files changed

+108
-30
lines changed

src/Common/Api/IEndpoint.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NetAPI.Common.Api;
2+
3+
public interface IEndpoint
4+
{
5+
static abstract void Map(IEndpointRouteBuilder app);
6+
}

src/Extensions/MapEndpoint.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using NetAPI.Common.Api;
2+
public static class MapEndpointExtention
3+
{
4+
5+
public static IEndpointRouteBuilder MapEndpoint<TEndpoint>(this IEndpointRouteBuilder app) where TEndpoint : IEndpoint
6+
{
7+
TEndpoint.Map(app);
8+
return app;
9+
}
10+
}

src/Extensions/WebAppBuilderExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Microsoft.Extensions.Configuration;
2222
using Microsoft.AspNetCore.Authentication.JwtBearer;
2323
using Microsoft.IdentityModel.Tokens;
24+
using NetAPI.Infrastructure;
2425

2526
[ExcludeFromCodeCoverage]
2627
public static class WebAppBuilderExtension
@@ -177,7 +178,7 @@ public static WebApplicationBuilder ConfigureApplicationBuilder(this WebApplicat
177178

178179
#endregion
179180

180-
181+
builder.Services.AddInfra();
181182

182183
builder.Services.AddControllers()
183184
.AddJsonOptions(options =>

src/Extensions/WebAppExtensions.cs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Globalization;
33
using Microsoft.AspNetCore.Builder;
44
using Serilog;
5+
using NetAPI.Features.Posts;
6+
using Microsoft.OpenApi.Models;
7+
using NetAPI.Common.Api;
58

69
[ExcludeFromCodeCoverage]
710
public 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
}

src/Features/Posts/GetPosts.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace NetAPI.Features.Posts;
2+
3+
using NetAPI.Common.Api;
4+
5+
public class GetPosts: IEndpoint
6+
{
7+
public record Request(string Title, string? Content);
8+
public record Response(int Id);
9+
10+
public static void Map(IEndpointRouteBuilder app) => app
11+
.MapGet("/", Handle)
12+
.WithSummary("Gets all posts");
13+
14+
private static Response Handle([AsParameters] Request request, CancellationToken cancellationToken)
15+
{
16+
return new Response(1);
17+
}
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace NetAPI.Infrastructure;
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Globalization;
5+
using Microsoft.AspNetCore.Builder;
6+
using Serilog;
7+
8+
[ExcludeFromCodeCoverage]
9+
public static class DependencyInjection
10+
{
11+
public static IServiceCollection AddInfra(this IServiceCollection services)
12+
{
13+
// var IsDevelopment = app.Environment.IsDevelopment();
14+
15+
16+
return services;
17+
}
18+
19+
}

0 commit comments

Comments
 (0)