|
|
|
using BookStore.Data;
|
|
|
|
using BookStore.Localization;
|
|
|
|
using Hangfire;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Localization;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
using Sanhe.Abp.AspNetCore.Mvc.Wrapper;
|
|
|
|
using Sanhe.Abp.ExceptionHandling;
|
|
|
|
using Sanhe.Abp.ExceptionHandling.Emailing;
|
|
|
|
using Sanhe.Abp.Features.LimitValidation.Redis;
|
|
|
|
using Sanhe.Abp.Hangfire.Dashboard;
|
|
|
|
using Sanhe.Abp.Hangfire.Storage.PostgreSql;
|
|
|
|
using StackExchange.Redis;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Volo.Abp;
|
|
|
|
using Volo.Abp.Account;
|
|
|
|
using Volo.Abp.Account.Web;
|
|
|
|
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Volo.Abp.AspNetCore.MultiTenancy;
|
|
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
|
|
using Volo.Abp.AspNetCore.Mvc.Localization;
|
|
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
|
|
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
|
|
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Bundling;
|
|
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
|
|
|
|
using Volo.Abp.AspNetCore.Serilog;
|
|
|
|
using Volo.Abp.AuditLogging.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.Autofac;
|
|
|
|
using Volo.Abp.AutoMapper;
|
|
|
|
using Volo.Abp.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.EntityFrameworkCore.PostgreSql;
|
|
|
|
using Volo.Abp.FeatureManagement;
|
|
|
|
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.Identity;
|
|
|
|
using Volo.Abp.Identity.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.IdentityServer.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.Localization;
|
|
|
|
using Volo.Abp.Localization.ExceptionHandling;
|
|
|
|
using Volo.Abp.Modularity;
|
|
|
|
using Volo.Abp.MultiTenancy;
|
|
|
|
using Volo.Abp.PermissionManagement;
|
|
|
|
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.PermissionManagement.HttpApi;
|
|
|
|
using Volo.Abp.PermissionManagement.Identity;
|
|
|
|
using Volo.Abp.SettingManagement;
|
|
|
|
using Volo.Abp.SettingManagement.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.Settings;
|
|
|
|
using Volo.Abp.Swashbuckle;
|
|
|
|
using Volo.Abp.TenantManagement;
|
|
|
|
using Volo.Abp.TenantManagement.EntityFrameworkCore;
|
|
|
|
using Volo.Abp.UI.Navigation.Urls;
|
|
|
|
using Volo.Abp.Validation.Localization;
|
|
|
|
using Volo.Abp.VirtualFileSystem;
|
|
|
|
|
|
|
|
namespace BookStore;
|
|
|
|
|
|
|
|
[DependsOn(
|
|
|
|
// ABP Framework packages
|
|
|
|
typeof(AbpAspNetCoreMvcModule),
|
|
|
|
typeof(AbpAspNetCoreMultiTenancyModule),
|
|
|
|
typeof(AbpAutofacModule),
|
|
|
|
typeof(AbpAutoMapperModule),
|
|
|
|
//typeof(AbpCachingStackExchangeRedisModule),
|
|
|
|
typeof(AbpEntityFrameworkCorePostgreSqlModule),
|
|
|
|
typeof(AbpAspNetCoreMvcUiBasicThemeModule),
|
|
|
|
typeof(AbpSwashbuckleModule),
|
|
|
|
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
|
|
|
typeof(AbpAspNetCoreSerilogModule),
|
|
|
|
|
|
|
|
// Account module packages
|
|
|
|
typeof(AbpAccountApplicationModule),
|
|
|
|
typeof(AbpAccountHttpApiModule),
|
|
|
|
typeof(AbpAccountWebIdentityServerModule),
|
|
|
|
|
|
|
|
// Identity module packages
|
|
|
|
typeof(AbpPermissionManagementDomainIdentityModule),
|
|
|
|
typeof(AbpIdentityApplicationModule),
|
|
|
|
typeof(AbpIdentityHttpApiModule),
|
|
|
|
typeof(AbpIdentityEntityFrameworkCoreModule),
|
|
|
|
typeof(AbpIdentityServerEntityFrameworkCoreModule),
|
|
|
|
|
|
|
|
// Audit logging module packages
|
|
|
|
typeof(AbpAuditLoggingEntityFrameworkCoreModule),
|
|
|
|
|
|
|
|
// Permission Management module packages
|
|
|
|
typeof(AbpPermissionManagementApplicationModule),
|
|
|
|
typeof(AbpPermissionManagementHttpApiModule),
|
|
|
|
typeof(AbpPermissionManagementEntityFrameworkCoreModule),
|
|
|
|
|
|
|
|
// Tenant Management module packages
|
|
|
|
typeof(AbpTenantManagementApplicationModule),
|
|
|
|
typeof(AbpTenantManagementHttpApiModule),
|
|
|
|
typeof(AbpTenantManagementEntityFrameworkCoreModule),
|
|
|
|
|
|
|
|
// Feature Management module packages
|
|
|
|
typeof(AbpFeatureManagementApplicationModule),
|
|
|
|
typeof(AbpFeatureManagementEntityFrameworkCoreModule),
|
|
|
|
typeof(AbpFeatureManagementHttpApiModule),
|
|
|
|
|
|
|
|
// Setting Management module packages
|
|
|
|
typeof(AbpSettingManagementApplicationModule),
|
|
|
|
typeof(AbpSettingManagementEntityFrameworkCoreModule),
|
|
|
|
typeof(AbpSettingManagementHttpApiModule),
|
|
|
|
|
|
|
|
typeof(AbpAspNetCoreMvcWrapperModule),
|
|
|
|
typeof(AbpEmailingExceptionHandlingModule),
|
|
|
|
typeof(AbpFeaturesValidationRedisModule),
|
|
|
|
typeof(AbpHangfireDashboardModule),
|
|
|
|
typeof(AbpHangfireStoragePostgreSqlModule)
|
|
|
|
)]
|
|
|
|
public class BookStoreModule : AbpModule
|
|
|
|
{
|
|
|
|
/* Single point to enable/disable multi-tenancy */
|
|
|
|
private const bool IsMultiTenant = true;
|
|
|
|
|
|
|
|
public override void PreConfigureServices(ServiceConfigurationContext context)
|
|
|
|
{
|
|
|
|
context.Services.PreConfigure<AbpMvcDataAnnotationsLocalizationOptions>(options =>
|
|
|
|
{
|
|
|
|
options.AddAssemblyResource(
|
|
|
|
typeof(BookStoreResource)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
|
|
{
|
|
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
var configuration = context.Services.GetConfiguration();
|
|
|
|
|
|
|
|
context.Services.AddAlwaysAllowAuthorization();
|
|
|
|
// wrap
|
|
|
|
//Configure<AbpWrapperOptions>(options =>
|
|
|
|
//{
|
|
|
|
// options.IsEnabled = true;
|
|
|
|
// options.IgnoreNamespaces.Clear();
|
|
|
|
//});
|
|
|
|
// limit
|
|
|
|
Configure<AbpRedisRequiresLimitFeatureOptions>(options =>
|
|
|
|
{
|
|
|
|
options.Configuration = "127.0.0.1:6379";
|
|
|
|
});
|
|
|
|
ConfigureExceptionHandling();
|
|
|
|
ConfigureBundles();
|
|
|
|
ConfigureMultiTenancy();
|
|
|
|
ConfigureUrls(configuration);
|
|
|
|
ConfigureAutoMapper();
|
|
|
|
ConfigureSwaggerServices(context, configuration);
|
|
|
|
ConfigureAutoApiControllers();
|
|
|
|
ConfigureVirtualFiles(hostingEnvironment);
|
|
|
|
ConfigureLocalization();
|
|
|
|
ConfigureAuthentication(context.Services, configuration);
|
|
|
|
ConfigureCors(context, configuration);
|
|
|
|
//ConfigureDataProtection(context, configuration, hostingEnvironment);
|
|
|
|
ConfigureEfCore(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureExceptionHandling()
|
|
|
|
{
|
|
|
|
// 自定义需要处理的异常
|
|
|
|
Configure<AbpExceptionHandlingOptions>(options =>
|
|
|
|
{
|
|
|
|
// 加入需要处理的异常类型
|
|
|
|
options.Handlers.Add<Volo.Abp.Data.AbpDbConcurrencyException>();
|
|
|
|
options.Handlers.Add<AbpInitializationException>();
|
|
|
|
options.Handlers.Add<ObjectDisposedException>();
|
|
|
|
options.Handlers.Add<StackOverflowException>();
|
|
|
|
options.Handlers.Add<OutOfMemoryException>();
|
|
|
|
options.Handlers.Add<System.Data.Common.DbException>();
|
|
|
|
options.Handlers.Add<Microsoft.EntityFrameworkCore.DbUpdateException>();
|
|
|
|
options.Handlers.Add<System.Data.DBConcurrencyException>();
|
|
|
|
});
|
|
|
|
|
|
|
|
// 自定义需要发送邮件通知的异常类型
|
|
|
|
Configure<AbpEmailExceptionHandlingOptions>(options =>
|
|
|
|
{
|
|
|
|
// 是否发送堆栈信息
|
|
|
|
options.SendStackTrace = true;
|
|
|
|
// 未指定异常接收者的默认接收邮件
|
|
|
|
// 请指定自己的邮件地址
|
|
|
|
// options.DefaultReceiveEmail = "";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureBundles()
|
|
|
|
{
|
|
|
|
Configure<AbpBundlingOptions>(options =>
|
|
|
|
{
|
|
|
|
options.StyleBundles.Configure(
|
|
|
|
BasicThemeBundles.Styles.Global,
|
|
|
|
bundle => { bundle.AddFiles("/global-styles.css"); }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureMultiTenancy()
|
|
|
|
{
|
|
|
|
Configure<AbpMultiTenancyOptions>(options =>
|
|
|
|
{
|
|
|
|
options.IsEnabled = IsMultiTenant;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureUrls(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
Configure<AppUrlOptions>(options =>
|
|
|
|
{
|
|
|
|
options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"];
|
|
|
|
options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"].Split(','));
|
|
|
|
|
|
|
|
options.Applications["Angular"].RootUrl = configuration["App:ClientUrl"];
|
|
|
|
options.Applications["Angular"].Urls[AccountUrlNames.PasswordReset] = "account/reset-password";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureAuthentication(IServiceCollection services, IConfiguration configuration)
|
|
|
|
{
|
|
|
|
services.AddAuthentication()
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
{
|
|
|
|
options.Authority = configuration["AuthServer:Authority"];
|
|
|
|
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
|
|
|
|
options.Audience = "BookStore";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
|
|
|
|
{
|
|
|
|
context.Services.AddAbpSwaggerGenWithOAuth(
|
|
|
|
configuration["AuthServer:Authority"],
|
|
|
|
new Dictionary<string, string> { { "BookStore", "BookStore API" } },
|
|
|
|
options =>
|
|
|
|
{
|
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "BookStore API", Version = "v1" });
|
|
|
|
options.DocInclusionPredicate((docName, description) => true);
|
|
|
|
options.CustomSchemaIds(type => type.FullName);
|
|
|
|
var currentPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
|
|
|
|
if (!currentPath.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
var allXml = Directory.GetFiles(currentPath, "*.xml", SearchOption.TopDirectoryOnly);
|
|
|
|
foreach (var xml in allXml)
|
|
|
|
{
|
|
|
|
options.IncludeXmlComments(xml, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureLocalization()
|
|
|
|
{
|
|
|
|
Configure<AbpLocalizationOptions>(options =>
|
|
|
|
{
|
|
|
|
options.Resources
|
|
|
|
.Add<BookStoreResource>("zh-Hans")
|
|
|
|
.AddBaseTypes(typeof(AbpValidationResource))
|
|
|
|
.AddVirtualJson("/Localization/BookStore");
|
|
|
|
|
|
|
|
options.DefaultResourceType = typeof(BookStoreResource);
|
|
|
|
|
|
|
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
|
|
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English"));
|
|
|
|
});
|
|
|
|
|
|
|
|
Configure<AbpExceptionLocalizationOptions>(options =>
|
|
|
|
{
|
|
|
|
options.MapCodeNamespace("BookStore", typeof(BookStoreResource));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureVirtualFiles(IWebHostEnvironment hostingEnvironment)
|
|
|
|
{
|
|
|
|
Configure<AbpVirtualFileSystemOptions>(options =>
|
|
|
|
{
|
|
|
|
options.FileSets.AddEmbedded<BookStoreModule>();
|
|
|
|
if (hostingEnvironment.IsDevelopment())
|
|
|
|
{
|
|
|
|
/* Using physical files in development, so we don't need to recompile on changes */
|
|
|
|
options.FileSets.ReplaceEmbeddedByPhysical<BookStoreModule>(hostingEnvironment.ContentRootPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureAutoApiControllers()
|
|
|
|
{
|
|
|
|
//Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
|
|
//{
|
|
|
|
// options.ConventionalControllers.Create(typeof(BookStoreModule).Assembly);
|
|
|
|
//});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureAutoMapper()
|
|
|
|
{
|
|
|
|
Configure<AbpAutoMapperOptions>(options =>
|
|
|
|
{
|
|
|
|
options.AddMaps<BookStoreModule>(/* validate: true */);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
|
|
|
{
|
|
|
|
context.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddDefaultPolicy(builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.WithOrigins(
|
|
|
|
configuration["App:CorsOrigins"]
|
|
|
|
.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
.Select(o => o.RemovePostFix("/"))
|
|
|
|
.ToArray()
|
|
|
|
)
|
|
|
|
.WithAbpExposedHeaders()
|
|
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
|
|
.AllowAnyHeader()
|
|
|
|
.AllowAnyMethod()
|
|
|
|
.AllowCredentials();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureDataProtection(
|
|
|
|
ServiceConfigurationContext context,
|
|
|
|
IConfiguration configuration,
|
|
|
|
IWebHostEnvironment hostingEnvironment)
|
|
|
|
{
|
|
|
|
var dataProtectionBuilder = context.Services.AddDataProtection().SetApplicationName("BookStore");
|
|
|
|
if (!hostingEnvironment.IsDevelopment())
|
|
|
|
{
|
|
|
|
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
|
|
|
|
dataProtectionBuilder.PersistKeysToStackExchangeRedis(redis, "BookStore-Protection-Keys");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConfigureEfCore(ServiceConfigurationContext context)
|
|
|
|
{
|
|
|
|
context.Services.AddAbpDbContext<BookStoreDbContext>(options =>
|
|
|
|
{
|
|
|
|
options.AddDefaultRepositories(includeAllEntities: true);
|
|
|
|
});
|
|
|
|
|
|
|
|
Configure<AbpDbContextOptions>(options =>
|
|
|
|
{
|
|
|
|
options.Configure(configurationContext =>
|
|
|
|
{
|
|
|
|
configurationContext.UseNpgsql();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
|
|
{
|
|
|
|
var app = context.GetApplicationBuilder();
|
|
|
|
var env = context.GetEnvironment();
|
|
|
|
|
|
|
|
app.ApplicationServices.GetService<ISettingDefinitionManager>().Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans";
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 微软内置从三个地方获取本地化:QueryString、Cookie、Header:accept-heade
|
|
|
|
// 浏览器中文的Culture字符串是zh-CN与Abp的zh-Hans不匹配
|
|
|
|
// 故而清除此provider
|
|
|
|
app.UseAbpRequestLocalization(
|
|
|
|
options => options.RequestCultureProviders.RemoveAll(provider => provider is AcceptLanguageHeaderRequestCultureProvider));
|
|
|
|
|
|
|
|
if (!env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseErrorPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseCorrelationId();
|
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseCors();
|
|
|
|
app.UseAuthentication();
|
|
|
|
app.UseJwtTokenMiddleware();
|
|
|
|
|
|
|
|
if (IsMultiTenant)
|
|
|
|
{
|
|
|
|
app.UseMultiTenancy();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseUnitOfWork();
|
|
|
|
app.UseIdentityServer();
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseAbpSwaggerUI(options =>
|
|
|
|
{
|
|
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "BookStore API");
|
|
|
|
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
|
|
|
|
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
|
|
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
|
|
|
options.OAuthScopes("BookStore");
|
|
|
|
options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.UseAuditing();
|
|
|
|
app.UseAbpSerilogEnrichers();
|
|
|
|
app.UseHangfireDashboard();
|
|
|
|
app.UseConfiguredEndpoints();
|
|
|
|
}
|
|
|
|
}
|