Program.cs 3.8 KB

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