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

151 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>();
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>ɵ<EFBFBD><C9B5><EFBFBD><E8B1B8>¼
//opt.Filters.Add<AuthonizationFilter>();
}).AddNewtonsoftJson(opt =>
{
//<2F><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//<2F><><EFBFBD>ı<EFBFBD><C4B1>ֶδ<D6B6>С
opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
opt.SerializerSettings.ContractResolver = new CustomContractResolver();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϸ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
opt.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
});
// <20><><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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<72><65><EFBFBD>",
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();
}
}
}