Program.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Text.Json;
  2. using Base;
  3. using Common;
  4. using Extensions;
  5. using Filters;
  6. using Infrastructure;
  7. using Infrastructure.Model;
  8. using Microsoft.AspNetCore.Http.Features;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.AspNetCore.Server.Kestrel.Core;
  11. using Microsoft.Extensions.FileProviders;
  12. using Middleware;
  13. using MySystem;
  14. using Services;
  15. using SummerBoot.Core;
  16. using Util;
  17. var builder = WebApplication.CreateBuilder(args);
  18. // Add services to the container.
  19. builder.Services.AddControllers();
  20. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  21. builder.Services.AddEndpointsApiExplorer();
  22. //注入HttpContextAccessor
  23. builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  24. //IPRatelimit
  25. // builder.Services.AddIPRate(builder.Configuration);
  26. builder.Services.AddSession();
  27. builder.Services.AddHttpContextAccessor();
  28. builder.Services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  29. builder.Services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
  30. //绑定整个对象到Model上
  31. builder.Services.Configure<OptionsSetting>(builder.Configuration);
  32. // builder.Configuration.AddJsonFile("iprate.json");
  33. //配置文件
  34. builder.Services.AddSingleton(new AppSettings(builder.Configuration));
  35. //请求大小限制
  36. builder.Services.AddRequestLimit(builder.Configuration);
  37. //注册REDIS 服务
  38. var openRedis = builder.Configuration["RedisServer:open"];
  39. if (openRedis == "1")
  40. {
  41. RedisServer.Initalize();
  42. }
  43. //配置文件
  44. builder.Services.AddSingleton(new AppSettings(builder.Configuration));
  45. //app服务注册
  46. builder.Services.AddAppService();
  47. builder.Services.AddMvc(options =>
  48. {
  49. options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
  50. options.Filters.Add(typeof(AuthorizationFilter));
  51. })
  52. .AddJsonOptions(options =>
  53. {
  54. //options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString;
  55. options.JsonSerializerOptions.WriteIndented = true;
  56. options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
  57. options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
  58. options.JsonSerializerOptions.Converters.Add(new StringConverter());
  59. //PropertyNamingPolicy属性用于前端传过来的属性的格式策略,目前内置的仅有一种策略CamelCase
  60. options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
  61. //options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;//属性可以忽略大小写格式,开启后性能会降低
  62. });
  63. builder.Services.AddSummerBoot();
  64. builder.Services.AddSummerBootFeign();
  65. var app = builder.Build();
  66. InternalApp.ServiceProvider = app.Services;
  67. InternalApp.Configuration = builder.Configuration;
  68. InternalApp.WebHostEnvironment = app.Environment;
  69. //初始化db
  70. builder.Services.AddDb(app.Environment);
  71. var workId = builder.Configuration["workId"].ParseToInt();
  72. if (app.Environment.IsDevelopment())
  73. {
  74. workId += 1;
  75. }
  76. SnowFlakeSingle.WorkId = workId;
  77. //使用全局异常中间件
  78. app.UseMiddleware<GlobalExceptionMiddleware>();
  79. app.Use(next => new RequestDelegate(
  80. async context =>
  81. {
  82. context.Request.EnableBuffering();
  83. await next(context);
  84. }
  85. ));
  86. // Configure the HTTP request pipeline.
  87. if (app.Environment.IsDevelopment())
  88. {
  89. }
  90. app.UseStaticFiles();
  91. app.UseCors();
  92. app.UseHttpsRedirection();
  93. app.UseAuthorization();
  94. app.UseRouting();
  95. app.UseEndpoints(endpoints =>
  96. {
  97. endpoints.MapControllerRoute(
  98. name: "default",
  99. pattern: "{controller=Home}/{action=Index}/{Id?}");
  100. });
  101. app.MapControllerRoute(
  102. name: "default",
  103. pattern: "{controller=Home}/{action=Index}/{id?}");
  104. app.MapControllers();
  105. app.Urls.Add("http://*:5802");
  106. // PrizeDo.sendPrize(1, "{\"PosSn\":\"00005702880118401003\"}");
  107. // PrizeDo.sendPrize(1, "{\"PosSn\":\"00003102722406215502730\"}");
  108. // PrizeDo.sendPrize(4, "{\"PosSn\":\"CS00000000015859\"}");
  109. // PrizeDo.sendPrize(5, "{\"PosSn\":\"00000302T1NL90664172\"}");
  110. // PrizeDo.sendPrize(6, "{\"PosSn\":\"1152131239\"}");
  111. // PrizeDo.sendPrize("KXS_MALL_ORDER_PRIZE", "{\"order_id\":\"319698\"}");
  112. // PrizeDo.sendPrize(7, "{\"OrderNo\":\"BM2024090817330511847441249\"}");
  113. // PrizeDo.sendPrize(7, "{\"OrderNo\":\"BM2024090817360962275551251\"}");
  114. RabbitMQClient.Instance.Start();
  115. // PrizeDo.sendPrize("{\"prize_tag\":\"1\",\"content\":\"{\\\"PosSn\\\":\\\"00002402045980195457\\\"}\"}");
  116. app.Run();