2024-08-08 22:23:36 +08:00
|
|
|
|
using Bunny.Common;
|
2024-08-09 10:06:17 +08:00
|
|
|
|
using Bunny.Common.Connect;
|
2024-08-08 22:23:36 +08:00
|
|
|
|
using Bunny.Service.WebSocket;
|
|
|
|
|
|
|
|
|
|
namespace Bunny.WebApi.Config;
|
|
|
|
|
|
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-10 18:11:48 +08:00
|
|
|
|
// 注入服务依赖、Host服务、插件等
|
|
|
|
|
AddApplicationService();
|
2024-08-08 22:23:36 +08:00
|
|
|
|
// 使用扩展,第三方如Redis、Minio、SQLSugar
|
2024-08-10 18:11:48 +08:00
|
|
|
|
UseExtend();
|
2024-08-08 22:23:36 +08:00
|
|
|
|
// 配置跨域
|
2024-08-10 18:11:48 +08:00
|
|
|
|
UseCors();
|
2024-08-08 22:23:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-10 18:11:48 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 注入控制器、服务依赖、Host服务等
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void AddApplicationService()
|
2024-08-08 22:23:36 +08:00
|
|
|
|
{
|
|
|
|
|
// 配置日志相关
|
2024-08-09 22:31:06 +08:00
|
|
|
|
// builder.Logging.AddLog4Net("Config/log4net.config");
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 添加Service服务
|
|
|
|
|
builder.AddApplicationServices();
|
|
|
|
|
// 添加后台服务
|
|
|
|
|
builder.AddApplicationBackendServices();
|
|
|
|
|
// 添加使用自定义配置文件
|
2024-08-08 22:23:36 +08:00
|
|
|
|
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
|
|
|
|
// 添加 SignalR
|
|
|
|
|
builder.Services.AddSignalR();
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 添加验证码
|
|
|
|
|
builder.AddCaptcha();
|
2024-08-08 22:23:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扩展服务
|
|
|
|
|
/// </summary>
|
2024-08-10 18:11:48 +08:00
|
|
|
|
private void UseExtend()
|
2024-08-08 22:23:36 +08:00
|
|
|
|
{
|
|
|
|
|
// 设置过滤器
|
|
|
|
|
FilterConfig.Initialize(builder);
|
|
|
|
|
// 初始化Redis
|
2024-08-09 10:06:17 +08:00
|
|
|
|
RedisContext.Initial();
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 初始化 Knife4Net 文档
|
2024-08-08 22:23:36 +08:00
|
|
|
|
Knife4Net.Initialize(builder);
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 启动 webSocket
|
2024-08-08 22:23:36 +08:00
|
|
|
|
WebSocketInitial.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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("*"));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|