157 lines
6.0 KiB
C#
157 lines
6.0 KiB
C#
using Hangfire;
|
||
using Masuit.Tools;
|
||
using Microsoft.AspNetCore.Http.Features;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Converters;
|
||
using Newtonsoft.Json.Serialization;
|
||
using SqlSugar;
|
||
using WGShare.API.BackgroudServices;
|
||
using WGShare.API.Helpers;
|
||
using WGShare.API.Hubs;
|
||
using WGShare.API.ServiceConfigs;
|
||
using WGShare.Domain.Constant;
|
||
using Yitter.IdGenerator;
|
||
|
||
namespace WGShare.API
|
||
{
|
||
public class Program
|
||
{
|
||
public static void Main(string[] args)
|
||
{
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
var configuration = builder.Configuration;
|
||
// Add services to the container.
|
||
|
||
RedisHelper.Initialization(new FreeRedis.RedisClient(configuration["Redis:master"])
|
||
{
|
||
Serialize = (x) => x.ToJsonString(),
|
||
Deserialize = (x, t) => JsonConvert.DeserializeObject(x, t),
|
||
});
|
||
#if !DEBUG
|
||
ResetRedisKey();
|
||
#endif
|
||
|
||
builder.Services.Configure<FormOptions>(options =>
|
||
{
|
||
options.ValueLengthLimit = int.MaxValue;
|
||
options.MultipartBodyLengthLimit = long.MaxValue; // If we are multipart
|
||
options.MemoryBufferThreshold = int.MaxValue;
|
||
});
|
||
|
||
builder.Services.AddControllers(options =>
|
||
{
|
||
// 全局异常捕获,无需在代码中 写 try catch
|
||
options.Filters.Add<GlobalExceptionFilter>();
|
||
// 全局模型赋值默认值 和 统一返回格式处理
|
||
options.Filters.Add<ModelActionFilter>();
|
||
options.Filters.Add<AuthonizationFilter>();
|
||
}).AddNewtonsoftJson(options =>
|
||
{
|
||
//修改属性名称的序列化方式,首字母小写,即驼峰样式
|
||
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||
//日期类型默认格式化处理 方式1
|
||
options.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
|
||
//忽略循环引用
|
||
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||
//空值处理
|
||
//options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
||
});
|
||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||
builder.Services.AddEndpointsApiExplorer();
|
||
builder.Services.AddSwagger();
|
||
builder.Services.AddSignalR();
|
||
builder.Services.AddHttpClient();
|
||
builder.Services.ConfigureHangfire();
|
||
builder.Services.AddSingleton(new JwtHelper(configuration));
|
||
builder.Services.AddSingleton(new AgoraHelper(configuration));
|
||
builder.Services.AddSingleton(new MiniProgramHelper(configuration));
|
||
builder.Services.AddSingleton(new OssHelper(configuration));
|
||
builder.Services.AddHostedService<AgoraCallbackComsuerService>();
|
||
builder.Services.AddAuth(configuration["Jwt:Issuer"],
|
||
configuration["Jwt:Audience"],
|
||
configuration["Jwt:SecretKey"]);
|
||
builder.Services.AddSqlsugar(builder.Environment.EnvironmentName, new ConnectionConfig
|
||
{
|
||
DbType = DbType.MySql,
|
||
ConfigId = "metting",
|
||
ConnectionString = configuration.GetConnectionString("metting"),
|
||
IsAutoCloseConnection = true
|
||
},
|
||
new ConnectionConfig
|
||
{
|
||
DbType = DbType.MySql,
|
||
ConfigId = "usercenter",
|
||
ConnectionString = configuration.GetConnectionString("usercenter"),
|
||
IsAutoCloseConnection = true
|
||
});
|
||
YitIdHelper.SetIdGenerator(new IdGeneratorOptions(1));
|
||
|
||
var app = builder.Build();
|
||
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.UseSwagger();
|
||
app.UseSwaggerUI(options =>
|
||
{
|
||
options.SwaggerEndpoint($"/swagger/frontend/swagger.json", "前端");
|
||
options.SwaggerEndpoint($"/swagger/backend/swagger.json", "后端");
|
||
options.SwaggerEndpoint($"/swagger/public/swagger.json", "公共接口");
|
||
|
||
});
|
||
app.UseCustomCors();
|
||
}
|
||
// 添加Hangfire Dashboard
|
||
app.UseHangfireDashboard();
|
||
//调用中间件:UseAuthentication(认证),必须在所有需要身份认证的中间件前调用,比如 UseAuthorization(授权)。
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
|
||
|
||
app.MapControllers();
|
||
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
|
||
app.MapHub<SessionManageHub>("/session-manage").RequireCors(q => q.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
|
||
}
|
||
else
|
||
{
|
||
app.MapHub<SessionManageHub>("/session-manage");
|
||
}
|
||
|
||
app.Run();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 程序启动前,删除redis中得数据
|
||
/// </summary>
|
||
private static void ResetRedisKey()
|
||
{
|
||
Console.WriteLine($@"程序开始前执行Redis清除操作开始...");
|
||
|
||
var keys = new List<string>();
|
||
long nextCursor = 0;
|
||
do
|
||
{
|
||
var scanResult = RedisHelper.Instance.Scan(nextCursor, "wgshare:*", 1000, "");
|
||
nextCursor = scanResult.cursor;
|
||
var items = scanResult.items;
|
||
keys.AddRange(items);
|
||
}
|
||
while (nextCursor != 0);
|
||
|
||
var keysArr = keys.ConvertAll(x => x.Replace("wgshare:", "")).Where(x => x.StartsWith("SessionManage")).Distinct().ToArray();
|
||
if (!keysArr.IsNullOrEmpty())
|
||
{
|
||
Console.WriteLine($@"删除键值:{Environment.NewLine}{string.Join(Environment.NewLine, keysArr)}");
|
||
RedisHelper.Instance.Del(keysArr);
|
||
}
|
||
|
||
Console.WriteLine("执行完毕,初始化Redis完成");
|
||
}
|
||
}
|
||
}
|