using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Swashbuckle.AspNetCore.Swagger; using Volo.Abp; using Volo.Abp.AuditLogging.EntityFrameworkCore; using Volo.Abp.Autofac; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.SqlServer; using Volo.Abp.EventBus.Distributed.RabbitMq; using Volo.Abp.Guids; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.EntityFrameworkCore; using Volo.Abp.Security.Claims; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.Threading; using Volo.Blogging; using Volo.Blogging.Blogs; using Volo.Blogging.MongoDB; namespace BloggingService.Host { [DependsOn( typeof(AbpAutofacModule), typeof(AbpEventBusRabbitMqModule), typeof(AbpEntityFrameworkCoreSqlServerModule), typeof(AbpAuditLoggingEntityFrameworkCoreModule), typeof(AbpPermissionManagementEntityFrameworkCoreModule), typeof(AbpSettingManagementEntityFrameworkCoreModule), typeof(BloggingHttpApiModule), typeof(BloggingMongoDbModule), typeof(BloggingApplicationModule) )] public class BloggingServiceHostModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); context.Services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.Authority = configuration["AuthServer:Authority"]; options.ApiName = configuration["AuthServer:ApiName"]; options.RequireHttpsMetadata = false; //TODO: Should create an extension method for that (may require to create a new ABP package depending on the IdentityServer4.AccessTokenValidation) options.InboundJwtClaimTypeMap["sub"] = AbpClaimTypes.UserId; options.InboundJwtClaimTypeMap["role"] = AbpClaimTypes.Role; options.InboundJwtClaimTypeMap["email"] = AbpClaimTypes.Email; options.InboundJwtClaimTypeMap["email_verified"] = AbpClaimTypes.EmailVerified; options.InboundJwtClaimTypeMap["phone_number"] = AbpClaimTypes.PhoneNumber; options.InboundJwtClaimTypeMap["phone_number_verified"] = AbpClaimTypes.PhoneNumberVerified; options.InboundJwtClaimTypeMap["name"] = AbpClaimTypes.UserName; }); context.Services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info {Title = "Blogging Service API", Version = "v1"}); options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); }); Configure(options => { options.Languages.Add(new LanguageInfo("en", "en", "English")); }); Configure(options => { options.UseSqlServer(); }); context.Services.AddDistributedRedisCache(options => { options.Configuration = configuration["Redis:Configuration"]; }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); app.UseVirtualFiles(); app.UseAuthentication(); app.UseAbpRequestLocalization(); //TODO: localization? app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Blogging Service API"); }); app.UseAuditing(); app.UseMvcWithDefaultRouteAndArea(); AsyncHelper.RunSync(() => SeedDataAsync(context.ServiceProvider)); } public async Task SeedDataAsync(IServiceProvider serviceProvider) { using (var scope = serviceProvider.CreateScope()) { var blogRepository = scope.ServiceProvider.GetRequiredService(); var guidGenerator = scope.ServiceProvider.GetRequiredService(); if (await blogRepository.FindByShortNameAsync("abp") == null) { await blogRepository.InsertAsync( new Blog( guidGenerator.Create(), "ABP Blog", "abp" ) ); } } } } }