CSharp.Template/YuanXuan.IM.Api/Program.cs

145 lines
5.3 KiB
C#

using Hangfire;
using Hangfire.Dashboard;
using LearningOfficer.OA.Mobile.Api.Filters;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Text;
using Yitter.IdGenerator;
using YuanXuan.IM.Api.CollectionExtensions;
using YuanXuan.IM.Api.Hangfire;
using YuanXuan.IM.Api.Helper;
using YuanXuan.IM.Api.Proxy;
namespace YuanXuan.IM.Api
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var Configuration = builder.Configuration;
var services = builder.Services;
//Services=============================
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddNacos(Configuration, builder.Host);
services.AddYarpWithNacos(Configuration);
services.AddPollyPolicies();
services.AddControllers().AddJsonOptions(option =>
{
option.JsonSerializerOptions.PropertyNamingPolicy = new CustomJsonNamingPolicy();
});
services.AddControllers(opt =>
{
opt.Filters.Add<GlobalExceptionCatchFilter>();
opt.Filters.Add<UniformResultActionFilter>();
opt.Filters.Add<GlobalOperationLogFilter>();
}).AddNewtonsoftJson(opt =>
{
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
opt.SerializerSettings.ContractResolver = new CustomContractResolver();
opt.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
});
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
services.BatchRegisterServices();
services.AddApiVersion();
services.AddJwtAuth(Configuration);
services.AddSwagger();
services.AddHttpClient();
services.AddSerilog(Configuration, builder.Host, builder.Environment);
services.AddRedis(Configuration);
services.AddSqlSugar(Configuration, builder.Environment);
services.AddConfigureOptions(Configuration);
services.AddHttpContextAccessor();
services.AddWskService(Configuration);
services.AddHangfireServer();
YitIdHelper.SetIdGenerator(WorkerIdAutoRegisterHelper.GetIdGeneratorOptions());
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "YuanXuan-IM v1");
});
app.UseCors();
}
app.UseAuthentication();
app.UseAuthorization();
app.Use(async (context, next) =>
{
if (context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
{
context.Request.EnableBuffering();
using (var reader = new StreamReader(context.Request.Body, encoding: Encoding.UTF8
, detectEncodingFromByteOrderMarks: false, leaveOpen: true))
{
var body = await reader.ReadToEndAsync();
context.Items.Add("body", body);
context.Request.Body.Position = 0;
}
}
if (context.Request.Method.Equals("Put", StringComparison.OrdinalIgnoreCase))
{
context.Request.EnableBuffering();
using (var reader = new StreamReader(context.Request.Body, encoding: Encoding.UTF8
, detectEncodingFromByteOrderMarks: false, leaveOpen: true))
{
var body = await reader.ReadToEndAsync();
context.Items.Add("body", body);
context.Request.Body.Position = 0;
}
}
await next.Invoke();
});
app.UseHangfireDashboard("/hang", new DashboardOptions
{
IgnoreAntiforgeryToken = true,
DashboardTitle = "Hangfire",
Authorization = new[] { new MyHangfireFilter() },
IsReadOnlyFunc = (DashboardContext context) => false
});
var LocalTimeZone = new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local
};
//RecurringJob.AddOrUpdate<IAdminHangfireJobs>("OverdueTasks", x => x.OverdueTasks(), "0 5 0 * * ?", LocalTimeZone);
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
// 映射YARP反向代理
app.MapReverseProxy();
app.Run();
}
}
}