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.
249 lines
8.6 KiB
249 lines
8.6 KiB
2 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
|
using Microsoft.AspNetCore.Builder;
|
||
|
using Microsoft.AspNetCore.Cors;
|
||
|
using Microsoft.AspNetCore.DataProtection;
|
||
|
using Microsoft.AspNetCore.Hosting;
|
||
|
using Microsoft.Extensions.Configuration;
|
||
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.Extensions.Hosting;
|
||
|
using StackExchange.Redis;
|
||
|
using Volo.Abp;
|
||
|
using Volo.Abp.Application.Dtos;
|
||
|
using Volo.Abp.AspNetCore.ExceptionHandling;
|
||
|
using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy;
|
||
|
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.Caching;
|
||
|
using Volo.Abp.Caching.StackExchangeRedis;
|
||
|
using Volo.Abp.EntityFrameworkCore;
|
||
|
using Volo.Abp.EntityFrameworkCore.SqlServer;
|
||
|
using Volo.Abp.Localization;
|
||
|
using Volo.Abp.Modularity;
|
||
|
using Volo.Abp.MultiTenancy;
|
||
|
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
|
||
|
using Volo.Abp.SettingManagement.EntityFrameworkCore;
|
||
|
using Volo.Abp.Swashbuckle;
|
||
|
using WatchDog;
|
||
|
using WatchDog.src.Enums;
|
||
|
using Win_in.Sfs.Shared.Host;
|
||
|
|
||
|
namespace Win_in.Sfs.Wms.Print.Host
|
||
|
{
|
||
|
[DependsOn(
|
||
|
typeof(AbpSettingManagementEntityFrameworkCoreModule),
|
||
|
typeof(AbpPermissionManagementEntityFrameworkCoreModule),
|
||
|
typeof(AbpAuditLoggingEntityFrameworkCoreModule),
|
||
|
typeof(AbpCachingStackExchangeRedisModule),
|
||
|
typeof(AbpAspNetCoreMvcUiMultiTenancyModule),
|
||
|
typeof(AbpAutofacModule),
|
||
|
typeof(AbpAspNetCoreSerilogModule),
|
||
|
typeof(AbpSwashbuckleModule),
|
||
|
typeof(AbpEntityFrameworkCoreSqlServerModule)
|
||
|
)]
|
||
|
|
||
|
public class PrintHttpApiHostModule : AbpModule
|
||
|
{
|
||
|
private bool _isMultiTenancy = true;
|
||
|
|
||
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
||
|
{
|
||
|
|
||
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
||
|
|
||
|
context.SetConsoleTitleOfWebApp("Print.Host");
|
||
|
|
||
|
LimitedResultRequestDto.MaxMaxResultCount = 100000;
|
||
|
|
||
|
var configuration = context.Services.GetConfiguration();
|
||
|
_isMultiTenancy = Convert.ToBoolean(configuration["IsMultiTenancy"]);
|
||
|
|
||
|
ConfigureDbContext();
|
||
|
|
||
|
ConfigureExceptionHanding();
|
||
|
|
||
|
ConfigureMultiTenancy(configuration);
|
||
|
|
||
|
ConfigureSwaggerServices(context, configuration);
|
||
|
|
||
|
ConfigureLocalization();
|
||
|
|
||
|
ConfigureAuthorization(context, configuration);
|
||
|
|
||
|
ConfigureDistributedCache(context, hostingEnvironment, configuration);
|
||
|
|
||
|
ConfigureCors(context, configuration);
|
||
|
ConfigureWatchDog(context, configuration);
|
||
|
}
|
||
|
|
||
|
private void ConfigureWatchDog(ServiceConfigurationContext context, IConfiguration configuration)
|
||
|
{
|
||
|
context.Services.AddWatchDogServices(opt =>
|
||
|
{
|
||
|
opt.IsAutoClear = true;
|
||
|
opt.ClearTimeSchedule = WatchDogAutoClearScheduleEnum.Weekly;
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
private void ConfigureMultiTenancy(IConfiguration configuration)
|
||
|
{
|
||
|
_isMultiTenancy = Convert.ToBoolean(configuration["IsMultiTenancy"]);
|
||
|
Configure<AbpMultiTenancyOptions>(options => { options.IsEnabled = _isMultiTenancy; });
|
||
|
}
|
||
|
|
||
|
private void ConfigureExceptionHanding()
|
||
|
{
|
||
|
Configure<AbpExceptionHandlingOptions>(options =>
|
||
|
{
|
||
|
options.SendExceptionsDetailsToClients = true; //��ǰ�˷�������������־
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void ConfigureDbContext()
|
||
|
{
|
||
|
Configure<AbpDbContextOptions>(options =>
|
||
|
{
|
||
|
options.UseSqlServer();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private static void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
||
|
{
|
||
|
var origins = configuration.GetSection("App:CorsOrigins").Get<string[]>();
|
||
|
|
||
|
context.Services.AddCors(options =>
|
||
|
{
|
||
|
options.AddDefaultPolicy(builder =>
|
||
|
{
|
||
|
builder
|
||
|
.WithOrigins(
|
||
|
origins.Select(o => o.RemovePostFix("/"))
|
||
|
.ToArray()
|
||
|
)
|
||
|
.WithAbpExposedHeaders()
|
||
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
||
|
.AllowAnyHeader()
|
||
|
.AllowAnyMethod()
|
||
|
.AllowCredentials();
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void ConfigureLocalization()
|
||
|
{
|
||
|
Configure<AbpLocalizationOptions>(options =>
|
||
|
{
|
||
|
options.Languages.Add(new LanguageInfo("en", "en", "English"));
|
||
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "��������"));
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
||
|
{
|
||
|
var app = context.GetApplicationBuilder();
|
||
|
var env = context.GetEnvironment();
|
||
|
var configuration = context.GetConfiguration();
|
||
|
|
||
|
if (env.IsDevelopment())
|
||
|
{
|
||
|
app.UseDeveloperExceptionPage();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
app.UseErrorPage();
|
||
|
app.UseHsts();
|
||
|
}
|
||
|
|
||
|
app.UseHttpsRedirection();
|
||
|
app.UseCorrelationId();
|
||
|
app.UseStaticFiles();
|
||
|
app.UseRouting();
|
||
|
app.UseCors();
|
||
|
app.UseAuthentication();
|
||
|
if (_isMultiTenancy)
|
||
|
{
|
||
|
app.UseMultiTenancy();
|
||
|
}
|
||
|
app.UseAbpRequestLocalization();
|
||
|
app.UseAuthorization();
|
||
|
app.UseSwagger();
|
||
|
app.UseAbpSwaggerUI(options =>
|
||
|
{
|
||
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support APP API");
|
||
|
|
||
|
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
||
|
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
||
|
});
|
||
|
if (Convert.ToBoolean(configuration["WatchDog:Enable"]))
|
||
|
{
|
||
|
app.UseWatchDog(opt =>
|
||
|
{
|
||
|
opt.WatchPageUsername = configuration["WatchDog:Username"];
|
||
|
opt.WatchPagePassword = configuration["WatchDog:Password"];
|
||
|
});
|
||
|
}
|
||
|
app.UseAuditing();
|
||
|
app.UseAbpSerilogEnrichers();
|
||
|
app.UseConfiguredEndpoints();
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
private void ConfigureDistributedCache(ServiceConfigurationContext context, IWebHostEnvironment hostingEnvironment, IConfiguration configuration)
|
||
|
{
|
||
|
Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "Store:"; });
|
||
|
|
||
|
if (!hostingEnvironment.IsDevelopment())
|
||
|
{
|
||
|
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
|
||
|
context.Services
|
||
|
.AddDataProtection()
|
||
|
.PersistKeysToStackExchangeRedis(redis, "Store-Protection-Keys");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
|
||
|
{
|
||
|
context.Services.AddAbpSwaggerGenWithOAuth(
|
||
|
configuration["AuthServer:Authority"],
|
||
|
new Dictionary<string, string>
|
||
|
{
|
||
|
{"Core", "Core API"}
|
||
|
},
|
||
|
options =>
|
||
|
{
|
||
|
options.CustomSchemaIds(type => type.FullName);
|
||
|
|
||
|
});
|
||
|
}
|
||
|
|
||
|
|
||
|
private static void ConfigureAuthorization(ServiceConfigurationContext context, IConfiguration configuration)
|
||
|
{
|
||
|
var isAlwaysAllowAuthorization = configuration.GetValue<bool>("AlwaysAllowAuthorization");
|
||
|
if (isAlwaysAllowAuthorization)
|
||
|
{
|
||
|
//�ƹ���Ȩ����,���ڲ���
|
||
|
context.Services.AddAlwaysAllowAuthorization();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||
|
.AddJwtBearer(options =>
|
||
|
{
|
||
|
options.Authority = configuration["AuthServer:Authority"];
|
||
|
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
|
||
|
options.Audience = "Auth";
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|