-
Notifications
You must be signed in to change notification settings - Fork 9
/
Extensions.cs
241 lines (212 loc) · 9.54 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Reflection;
using Ben.Diagnostics;
using BuildingBlocks.Caching;
using BuildingBlocks.Exception;
using BuildingBlocks.Logging;
using BuildingBlocks.Resiliency;
using BuildingBlocks.Resiliency.Configs;
using BuildingBlocks.Security.ApiKey;
using BuildingBlocks.Security.ApiKey.Authorization;
using BuildingBlocks.Swagger;
using BuildingBlocks.Validation;
using BuildingBlocks.Web;
using Google;
using Hellang.Middleware.ProblemDetails;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MovieSearch.Application;
using MovieSearch.Application.Services.Clients;
using MovieSearch.Core;
using MovieSearch.Infrastructure.Security;
using MovieSearch.Infrastructure.Services.Clients.MovieDb;
using MovieSearch.Infrastructure.Services.Clients.Video;
using Newtonsoft.Json;
using Serilog;
namespace MovieSearch.Infrastructure;
public static class Extensions
{
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
{
// https://www.talkingdotnet.com/disable-automatic-model-state-validation-in-asp-net-core-2-1/
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
var appOptions = builder.Configuration.GetSection(nameof(AppOptions)).Get<AppOptions>();
builder
.Services.AddOptions<AppOptions>()
.Bind(builder.Configuration.GetSection(nameof(AppOptions)))
.ValidateDataAnnotations();
builder
.Services.AddOptions<TMDBOptions>()
.Bind(builder.Configuration.GetSection(nameof(TMDBOptions)))
.ValidateDataAnnotations();
builder
.Services.AddOptions<YoutubeVideoOptions>()
.Bind(builder.Configuration.GetSection(nameof(YoutubeVideoOptions)))
.ValidateDataAnnotations();
builder
.Services.AddOptions<PolicyConfig>()
.Bind(builder.Configuration.GetSection(nameof(PolicyConfig)))
.ValidateDataAnnotations();
builder.Services.AddHttpContextAccessor();
builder.Services.AddCustomHttpClients(builder.Configuration);
builder.Services.AddCustomVersioning();
builder.Services.AddCustomHealthCheck(healthBuilder => { });
builder.Services.AddCustomSwagger(builder.Configuration, Assembly.GetExecutingAssembly());
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
builder.Services.AddCustomApiKeyAuthentication();
builder.Services.AddSingleton<IMovieDbServiceClient, TMDBServiceClient>();
builder.Services.AddSingleton<IVideoServiceClient, YoutubeVideoServiceClient>();
builder.Services.AddCustomValidators(typeof(ApplicationRoot).Assembly);
builder.Services.AddAutoMapper(typeof(ApplicationRoot).Assembly, typeof(InfrastructureRoot).Assembly);
builder
.Services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(ApplicationRoot).Assembly))
.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>))
.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>))
.AddScoped(typeof(IPipelineBehavior<,>), typeof(CachingBehavior<,>))
.AddScoped(typeof(IPipelineBehavior<,>), typeof(InvalidateCachingBehavior<,>));
builder.Services.AddCachingRequestPolicies(new List<Assembly> { typeof(ApplicationRoot).Assembly });
builder.Services.AddEasyCaching(options =>
{
options.UseInMemory(builder.Configuration, "mem");
});
AddCustomProblemDetails(builder);
return builder;
}
public static void UseInfrastructure(this IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseProblemDetails();
//https://codeopinion.com/detecting-sync-over-async-code-in-asp-net-core/
app.UseBlockingDetection();
app.UseSerilogRequestLogging();
app.UseCustomHealthCheck();
}
private static void AddCustomProblemDetails(WebApplicationBuilder builder)
{
builder.Services.AddProblemDetails(x =>
{
// Control when an exception is included
x.IncludeExceptionDetails = (ctx, _) =>
{
// Fetch services from HttpContext.RequestServices
var env = ctx.RequestServices.GetRequiredService<IHostEnvironment>();
return env.IsDevelopment() || env.IsStaging();
};
x.Map<AppException>(ex => new ProblemDetails
{
Title = "Application rule broken",
Status = StatusCodes.Status409Conflict,
Detail = ex.Message,
Type = "https://somedomain/application-rule-validation-error"
});
// Exception will produce and returns from our FluentValidation RequestValidationBehavior
x.Map<ValidationException>(ex => new ProblemDetails
{
Title = "input validation rules broken",
Status = StatusCodes.Status400BadRequest,
Detail = JsonConvert.SerializeObject(ex.ValidationResultModel.Errors),
Type = "https://somedomain/input-validation-rules-error"
});
x.Map<BadRequestException>(ex => new ProblemDetails
{
Title = "bad request exception",
Status = StatusCodes.Status400BadRequest,
Detail = ex.Message,
Type = "https://somedomain/bad-request-error"
});
x.Map<NotFoundException>(ex => new ProblemDetails
{
Title = "not found exception",
Status = StatusCodes.Status404NotFound,
Detail = ex.Message,
Type = "https://somedomain/not-found-error"
});
x.Map<ApiException>(ex => new ProblemDetails
{
Title = "api server exception",
Status = StatusCodes.Status500InternalServerError,
Detail = ex.Message,
Type = "https://somedomain/api-server-error"
});
x.MapToStatusCode<ArgumentNullException>(StatusCodes.Status400BadRequest);
x.Map<GoogleApiException>(googleApiException =>
{
if (googleApiException.Error.Code == StatusCodes.Status403Forbidden)
return new ProblemDetails
{
Title = "youtube api forbidden exception",
Status = StatusCodes.Status403Forbidden,
Detail = googleApiException.Error.Message,
Type = "https://somedomain/forbiden"
};
if (googleApiException.Error.Code == StatusCodes.Status400BadRequest)
return new ProblemDetails
{
Title = "youtube api bad request exception",
Status = StatusCodes.Status400BadRequest,
Detail = googleApiException.Error.Message,
Type = "https://somedomain/bad-request-error"
};
return new ProblemDetails();
});
});
}
public static IServiceCollection AddCustomHttpClients(
this IServiceCollection services,
IConfiguration configuration,
string movieDbSectionName = "TMDBOptions",
string pollySectionName = "PolicyConfig"
)
{
var movieDbOptions = configuration.GetSection(movieDbSectionName).Get<TMDBOptions>();
services
.AddHttpClient(
nameof(TMDBServiceClient),
config =>
{
config.BaseAddress = new Uri(movieDbOptions.BaseApiAddress);
config.Timeout = new TimeSpan(0, 0, 30);
config.DefaultRequestHeaders.Clear();
}
)
.AddCustomPolicyHandlers(configuration, pollySectionName);
return services;
}
public static IServiceCollection AddCustomApiKeyAuthentication(this IServiceCollection services)
{
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = ApiKeyAuthenticationOptions.DefaultScheme;
options.DefaultChallengeScheme = ApiKeyAuthenticationOptions.DefaultScheme;
})
.AddApiKeySupport(options => { });
services.AddAuthorization(options =>
{
options.AddPolicy(
Policies.OnlyCustomers,
policy => policy.Requirements.Add(new OnlyCustomersRequirement())
);
options.AddPolicy(Policies.OnlyAdmins, policy => policy.Requirements.Add(new OnlyAdminsRequirement()));
options.AddPolicy(
Policies.OnlyThirdParties,
policy => policy.Requirements.Add(new OnlyThirdPartiesRequirement())
);
});
services.AddSingleton<IAuthorizationHandler, OnlyCustomersAuthorizationHandler>();
services.AddSingleton<IAuthorizationHandler, OnlyAdminsAuthorizationHandler>();
services.AddSingleton<IAuthorizationHandler, OnlyThirdPartiesAuthorizationHandler>();
services.AddSingleton<IGetApiKeyQuery, InMemoryGetApiKeyQuery>();
return services;
}
}