2024-09-02 09:31:34 +08:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
|
using Bunny.Common;
|
2024-09-02 21:28:21 +08:00
|
|
|
|
using Bunny.Common.Context.Middleware;
|
2024-08-08 22:23:36 +08:00
|
|
|
|
using Bunny.Service.WebSocket;
|
2024-09-02 09:31:34 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2024-08-08 22:23:36 +08:00
|
|
|
|
|
2024-09-02 09:31:34 +08:00
|
|
|
|
namespace Bunny.WebApi.Configuration;
|
2024-08-08 22:23:36 +08:00
|
|
|
|
|
2024-08-10 18:11:48 +08:00
|
|
|
|
public class BaseConfig(WebApplicationBuilder builder)
|
2024-08-08 22:23:36 +08:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2024-08-10 18:11:48 +08:00
|
|
|
|
/// 初始化
|
2024-08-08 22:23:36 +08:00
|
|
|
|
/// </summary>
|
2024-08-10 18:11:48 +08:00
|
|
|
|
public void Initialize()
|
2024-08-08 22:23:36 +08:00
|
|
|
|
{
|
2024-08-30 20:27:48 +08:00
|
|
|
|
// 配置跨域
|
2024-08-10 18:11:48 +08:00
|
|
|
|
UseCors();
|
2024-09-03 13:48:02 +08:00
|
|
|
|
|
2024-08-08 22:23:36 +08:00
|
|
|
|
// 配置日志相关
|
2024-09-03 13:48:02 +08:00
|
|
|
|
builder.Logging.AddLog4Net("Configuration/log4net.config");
|
2024-09-12 13:56:09 +08:00
|
|
|
|
// 添加 ASP.NET 自带缓存
|
|
|
|
|
builder.Services.AddMemoryCache();
|
2024-09-02 09:00:50 +08:00
|
|
|
|
// 自定义时间格式
|
|
|
|
|
builder.Services.AddControllers().AddJsonOptions(options =>
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 添加使用自定义配置文件
|
2024-08-08 22:23:36 +08:00
|
|
|
|
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
2024-09-02 09:00:50 +08:00
|
|
|
|
builder.WebHost.ConfigureKestrel((_, options) =>
|
2024-08-20 10:14:15 +08:00
|
|
|
|
{
|
|
|
|
|
// 设置文件最大上传大小
|
|
|
|
|
options.Limits.MaxRequestBodySize = 1024 * 1024 * 100;
|
|
|
|
|
});
|
2024-09-02 09:31:34 +08:00
|
|
|
|
// 使用 AddAutofac 注册服务
|
|
|
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
|
|
|
|
builder.Host.ConfigureContainer<ContainerBuilder>(AddAutofacConfig.BuildContainer);
|
|
|
|
|
// 让控制器实例由容器创建
|
|
|
|
|
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
2024-09-02 09:00:50 +08:00
|
|
|
|
// 添加定时任务
|
2024-08-15 22:55:32 +08:00
|
|
|
|
builder.AddApplicationBackendServices();
|
2024-08-08 22:23:36 +08:00
|
|
|
|
// 设置过滤器
|
2024-08-15 22:55:32 +08:00
|
|
|
|
builder.AddFilterConfigInitialize();
|
2024-08-08 22:23:36 +08:00
|
|
|
|
// 初始化Redis
|
2024-08-14 23:57:43 +08:00
|
|
|
|
builder.AddRedisContext();
|
|
|
|
|
// 初始化Minio
|
|
|
|
|
builder.AddMinioContext();
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 初始化 Knife4Net 文档
|
2024-08-14 23:57:43 +08:00
|
|
|
|
builder.AddKnife4Net();
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 启动 webSocket
|
2024-08-14 23:57:43 +08:00
|
|
|
|
builder.AddWebSocketInitial();
|
2024-09-02 09:00:50 +08:00
|
|
|
|
// 添加验证码
|
|
|
|
|
builder.AddCaptcha();
|
2024-08-08 22:23:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 配置跨域
|
|
|
|
|
/// </summary>
|
2024-08-10 18:11:48 +08:00
|
|
|
|
private void UseCors()
|
2024-08-08 22:23:36 +08:00
|
|
|
|
{
|
|
|
|
|
// SignalR 配置跨域
|
|
|
|
|
builder.Services.AddSignalRCore();
|
|
|
|
|
// 配置跨域
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddDefaultPolicy(policyBuilder => policyBuilder.WithOrigins("*")
|
|
|
|
|
.WithHeaders("*")
|
|
|
|
|
.WithMethods("*"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|