-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from canro91/clean-public-api-program.cs
Use extension methods to clean up PublicApi Program.cs file
- Loading branch information
Showing
5 changed files
with
158 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/PublicApi/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using BlazorShared; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.eShopWeb.ApplicationCore.Constants; | ||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using Microsoft.eShopWeb.ApplicationCore.Services; | ||
using Microsoft.eShopWeb.Infrastructure.Data; | ||
using Microsoft.eShopWeb.Infrastructure.Identity; | ||
using Microsoft.eShopWeb.Infrastructure.Logging; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.eShopWeb.PublicApi.Extensions; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void AddCustomServices(this IServiceCollection services, ConfigurationManager configuration) | ||
{ | ||
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>)); | ||
services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>)); | ||
services.Configure<CatalogSettings>(configuration); | ||
|
||
var catalogSettings = configuration.Get<CatalogSettings>() ?? new CatalogSettings(); | ||
services.AddSingleton<IUriComposer>(new UriComposer(catalogSettings)); | ||
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>)); | ||
services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>(); | ||
} | ||
|
||
public static void AddJwtAuthentication(this IServiceCollection services) | ||
{ | ||
var key = Encoding.ASCII.GetBytes(AuthorizationConstants.JWT_SECRET_KEY); | ||
|
||
services.AddAuthentication(config => | ||
{ | ||
config.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; | ||
}) | ||
.AddJwtBearer(config => | ||
{ | ||
config.RequireHttpsMetadata = false; | ||
config.SaveToken = true; | ||
config.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
ValidateIssuerSigningKey = true, | ||
IssuerSigningKey = new SymmetricSecurityKey(key), | ||
ValidateIssuer = false, | ||
ValidateAudience = false | ||
}; | ||
}); | ||
} | ||
|
||
public static void AddCorsPolicy(this IServiceCollection services, string policyName, BaseUrlConfiguration baseUrlConfig) | ||
{ | ||
services.AddCors(options => | ||
{ | ||
options.AddPolicy(name: policyName, | ||
corsPolicyBuilder => | ||
{ | ||
corsPolicyBuilder.WithOrigins(baseUrlConfig!.WebBase.Replace("host.docker.internal", "localhost").TrimEnd('/')); | ||
corsPolicyBuilder.AllowAnyMethod(); | ||
corsPolicyBuilder.AllowAnyHeader(); | ||
}); | ||
}); | ||
} | ||
|
||
public static void AddSwagger(this IServiceCollection services) | ||
{ | ||
// TODO: Update to use FastEndpoints approach to Swagger | ||
services.AddEndpointsApiExplorer(); | ||
services.AddSwaggerGen(c => | ||
{ | ||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); | ||
c.EnableAnnotations(); | ||
c.SchemaFilter<CustomSchemaFilters>(); | ||
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | ||
{ | ||
Description = @"JWT Authorization header using the Bearer scheme. \r\n\r\n | ||
Enter 'Bearer' [space] and then your token in the text input below. | ||
\r\n\r\nExample: 'Bearer 12345abcdef'", | ||
Name = "Authorization", | ||
In = ParameterLocation.Header, | ||
Type = SecuritySchemeType.ApiKey, | ||
Scheme = "Bearer" | ||
}); | ||
|
||
c.AddSecurityRequirement(new OpenApiSecurityRequirement() | ||
{ | ||
{ | ||
new OpenApiSecurityScheme | ||
{ | ||
Reference = new OpenApiReference | ||
{ | ||
Type = ReferenceType.SecurityScheme, | ||
Id = "Bearer" | ||
}, | ||
Scheme = "oauth2", | ||
Name = "Bearer", | ||
In = ParameterLocation.Header, | ||
}, | ||
new List<string>() | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.eShopWeb.Infrastructure.Data; | ||
using Microsoft.eShopWeb.Infrastructure.Identity; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.eShopWeb.PublicApi.Extensions; | ||
|
||
public static class WebApplicationExtensions | ||
{ | ||
public static async Task SeedDatabaseAsync(this WebApplication app) | ||
{ | ||
app.Logger.LogInformation("Seeding Database..."); | ||
|
||
using var scope = app.Services.CreateScope(); | ||
var scopedProvider = scope.ServiceProvider; | ||
try | ||
{ | ||
var catalogContext = scopedProvider.GetRequiredService<CatalogContext>(); | ||
await CatalogContextSeed.SeedAsync(catalogContext, app.Logger); | ||
|
||
var userManager = scopedProvider.GetRequiredService<UserManager<ApplicationUser>>(); | ||
var roleManager = scopedProvider.GetRequiredService<RoleManager<IdentityRole>>(); | ||
var identityContext = scopedProvider.GetRequiredService<AppIdentityDbContext>(); | ||
await AppIdentityDbContextSeed.SeedAsync(identityContext, userManager, roleManager); | ||
} | ||
catch (Exception ex) | ||
{ | ||
app.Logger.LogError(ex, "An error occurred seeding the DB."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters