You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
12 KiB
345 lines
12 KiB
using BookStore.Data; |
|
using BookStore.Localization; |
|
using Microsoft.AspNetCore.Cors; |
|
using Microsoft.AspNetCore.DataProtection; |
|
using Microsoft.OpenApi.Models; |
|
using Sanhe.Abp.AspNetCore.Mvc.Wrapper; |
|
using Sanhe.Abp.Wrapper; |
|
using StackExchange.Redis; |
|
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.Caching.StackExchangeRedis; |
|
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.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) |
|
)] |
|
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(); |
|
}); |
|
|
|
ConfigureBundles(); |
|
ConfigureMultiTenancy(); |
|
ConfigureUrls(configuration); |
|
ConfigureAutoMapper(); |
|
ConfigureSwagger(context.Services); |
|
ConfigureAutoApiControllers(); |
|
ConfigureVirtualFiles(hostingEnvironment); |
|
ConfigureLocalization(); |
|
ConfigureAuthentication(context.Services, configuration); |
|
ConfigureCors(context, configuration); |
|
//ConfigureDataProtection(context, configuration, hostingEnvironment); |
|
ConfigureEfCore(context); |
|
} |
|
|
|
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 ConfigureLocalization() |
|
{ |
|
Configure<AbpLocalizationOptions>(options => |
|
{ |
|
options.Resources |
|
.Add<BookStoreResource>("en") |
|
.AddBaseTypes(typeof(AbpValidationResource)) |
|
.AddVirtualJson("/Localization/BookStore"); |
|
|
|
options.DefaultResourceType = typeof(BookStoreResource); |
|
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English")); |
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); |
|
}); |
|
|
|
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 ConfigureSwagger(IServiceCollection services) |
|
{ |
|
services.AddAbpSwaggerGen( |
|
options => |
|
{ |
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "BookStore API", Version = "v1" }); |
|
options.DocInclusionPredicate((docName, description) => true); |
|
options.CustomSchemaIds(type => type.FullName); |
|
} |
|
); |
|
} |
|
|
|
private void ConfigureAutoMapper() |
|
{ |
|
Configure<AbpAutoMapperOptions>(options => |
|
{ |
|
/* Uncomment `validate: true` if you want to enable the Configuration Validation feature. |
|
* See AutoMapper's documentation to learn what it is: |
|
* https://docs.automapper.org/en/stable/Configuration-validation.html |
|
*/ |
|
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(); |
|
|
|
if (env.IsDevelopment()) |
|
{ |
|
app.UseDeveloperExceptionPage(); |
|
} |
|
|
|
app.UseAbpRequestLocalization(); |
|
|
|
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"); |
|
}); |
|
|
|
app.UseAuditing(); |
|
app.UseAbpSerilogEnrichers(); |
|
app.UseConfiguredEndpoints(); |
|
} |
|
}
|
|
|