123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System.Text.Json;
- using Base;
- using Common;
- using Extensions;
- using Filters;
- using Infrastructure;
- using Infrastructure.Model;
- using Microsoft.AspNetCore.Http.Features;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Server.Kestrel.Core;
- using Microsoft.Extensions.FileProviders;
- using Middleware;
- using Services;
- using SummerBoot.Core;
- using Util;
- var builder = WebApplication.CreateBuilder(args);
- // Add services to the container.
- builder.Services.AddControllers();
- // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
- builder.Services.AddEndpointsApiExplorer();
- //注入HttpContextAccessor
- builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- //IPRatelimit
- // builder.Services.AddIPRate(builder.Configuration);
- builder.Services.AddSession();
- builder.Services.AddHttpContextAccessor();
- builder.Services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
- builder.Services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
- //绑定整个对象到Model上
- builder.Services.Configure<OptionsSetting>(builder.Configuration);
- // builder.Configuration.AddJsonFile("iprate.json");
- //配置文件
- builder.Services.AddSingleton(new AppSettings(builder.Configuration));
- //请求大小限制
- builder.Services.AddRequestLimit(builder.Configuration);
- //注册REDIS 服务
- var openRedis = builder.Configuration["RedisServer:open"];
- if (openRedis == "1")
- {
- RedisServer.Initalize();
- }
- //配置文件
- builder.Services.AddSingleton(new AppSettings(builder.Configuration));
- //app服务注册
- builder.Services.AddAppService();
- builder.Services.AddMvc(options =>
- {
- options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
- options.Filters.Add(typeof(AuthorizationFilter));
- })
- .AddJsonOptions(options =>
- {
- //options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString;
- options.JsonSerializerOptions.WriteIndented = true;
- options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
- options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
- options.JsonSerializerOptions.Converters.Add(new StringConverter());
- //PropertyNamingPolicy属性用于前端传过来的属性的格式策略,目前内置的仅有一种策略CamelCase
- options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
- //options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;//属性可以忽略大小写格式,开启后性能会降低
- });
- builder.Services.AddSummerBoot();
- builder.Services.AddSummerBootFeign();
- var app = builder.Build();
- InternalApp.ServiceProvider = app.Services;
- InternalApp.Configuration = builder.Configuration;
- InternalApp.WebHostEnvironment = app.Environment;
- //初始化db
- builder.Services.AddDb(app.Environment);
- var workId = builder.Configuration["workId"].ParseToInt();
- if (app.Environment.IsDevelopment())
- {
- workId += 1;
- }
- SnowFlakeSingle.WorkId = workId;
- //使用全局异常中间件
- app.UseMiddleware<GlobalExceptionMiddleware>();
- app.Use(next => new RequestDelegate(
- async context =>
- {
- context.Request.EnableBuffering();
- await next(context);
- }
- ));
- // Configure the HTTP request pipeline.
- if (app.Environment.IsDevelopment())
- {
- }
- app.UseStaticFiles();
- app.UseCors();
- app.UseHttpsRedirection();
- app.UseAuthorization();
- app.UseRouting();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{Id?}");
- });
- app.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{id?}");
- app.MapControllers();
- app.Urls.Add("http://*:5801");
- // PrizeDo.sendPrize("{\"PosSn\":\"CS00000000004720\"}");
- app.Run();
|