123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using Microsoft.AspNetCore.Http.Connections;
- using Microsoft.AspNetCore.Http.Features;
- using Microsoft.AspNetCore.Server.Kestrel.Core;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using OASystem.API.Middlewares;
- using OASystem.API.OAMethodLib;
- using OASystem.API.OAMethodLib.AMapApi;
- using OASystem.API.OAMethodLib.APNs;
- using OASystem.API.OAMethodLib.Hub.Hubs;
- using OASystem.API.OAMethodLib.JuHeAPI;
- using OASystem.API.OAMethodLib.QiYeWeChatAPI;
- using OASystem.API.OAMethodLib.Quartz.Jobs;
- using OASystem.API.OAMethodLib.SignalR.HubService;
- using Quartz;
- using Quartz.Impl;
- using Quartz.Spi;
- using QuzrtzJob.Factory;
- Console.Title = $"FMGJ OASystem Server";
- var builder = WebApplication.CreateBuilder(args);
- var basePath = AppContext.BaseDirectory;
- //引入配置文件
- var _config = new ConfigurationBuilder()
- .SetBasePath(basePath)
- .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
- .AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
- .AddEnvironmentVariables()
- .Build();
- builder.Services.AddSingleton(new AppSettingsHelper(_config));
- //设置请求参数可不填
- builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
- // Add services to the container.
- builder.Services.AddControllersWithViews();
- builder.Services.AddControllers()
- .AddJsonOptions(options =>
- {
- //空字段不响应Response
- //options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
- options.JsonSerializerOptions.Converters.Add(new NullJsonConverter());
- //时间格式化响应
- options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
- //decimal 四位小数
- //options.JsonSerializerOptions.Converters.Add(new DecimalConverter(_decimalPlaces)); // 将保留小数位数参数传递给自定义序列化器
- });
- builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- #region Cors
- builder.Services.AddCors(policy =>
- {
- //policy.AddPolicy("Cors", opt => opt
- //.AllowAnyOrigin()
- //.AllowAnyHeader()
- //.AllowAnyMethod()
- //.WithExposedHeaders("X-Pagination"));
- policy.AddPolicy("Cors", opt => opt
- .SetIsOriginAllowed(origin => true)
- //.AllowAnyOrigin()
- .AllowAnyHeader()
- .WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")
- .AllowCredentials());
- });
- #endregion
- #region 上传文件
- builder.Services.AddCors(policy =>
- {
- policy.AddPolicy("Cors", opt => opt
- .AllowAnyOrigin()
- .AllowAnyHeader()
- .AllowAnyMethod()
- .WithExposedHeaders("X-Pagination"));
- });
- builder.Services.Configure<FormOptions>(options =>
- {
- options.KeyLengthLimit = int.MaxValue;
- options.ValueLengthLimit = int.MaxValue;
- options.MultipartBodyLengthLimit = int.MaxValue;
- options.MultipartHeadersLengthLimit = int.MaxValue;
- });
- builder.Services.Configure<KestrelServerOptions>(options =>
- {
- options.Limits.MaxRequestBodySize = int.MaxValue;
- options.Limits.MaxRequestBufferSize = int.MaxValue;
- });
- #endregion
- #region 接口分组
- var groups = new List<Tuple<string, string>>
- {
- //new Tuple<string, string>("Group1","分组一"),
- //new Tuple<string, string>("Group2","分组二")
- };
- #endregion
- #region 注入数据库
- builder.Services.AddScoped(options =>
- {
- return new SqlSugarClient(new List<ConnectionConfig>()
- {
- new ConnectionConfig() {
- ConfigId = DBEnum.OA2023DB,
- ConnectionString = _config.GetConnectionString("OA2023DB"),
- DbType = DbType.SqlServer,
- IsAutoCloseConnection = true },
- new ConnectionConfig()
- {
- ConfigId = DBEnum.OA2014DB,
- ConnectionString = _config.GetConnectionString("OA2014DB"),
- DbType = DbType.SqlServer,
- IsAutoCloseConnection = true },
- }
- // , db =>
- //{
- // //SQL执行完
- // db.Aop.OnLogExecuted = (sql, pars) =>
- // {
- // //if (db.Ado.SqlExecutionTime.TotalSeconds > 1)
- // //{
- // //代码CS文件名
- // var fileName = db.Ado.SqlStackTrace.FirstFileName;
- // //代码行数
- // var fileLine = db.Ado.SqlStackTrace.FirstLine;
- // //方法名
- // var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName;
- // //执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
- // Console.WriteLine("NowTime:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"));
- // Console.WriteLine("MethodName:" + FirstMethodName);
- // Console.WriteLine("ElapsedTime:" + db.Ado.SqlExecutionTime.ToString());
- // Console.WriteLine("ExecuteSQL:" + sql);
- // //}
- // };
- // //SQL执行前
- // db.Aop.OnLogExecuting = (sql, pars) =>
- // {
- // //获取原生SQL推荐 5.1.4.63 性能OK
- // //UtilMethods.GetNativeSql(sql, pars);
- // //获取无参数化SQL 影响性能只适合调试
- // //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
- // };
- // //SQL报错
- // db.Aop.OnError = (exp) =>
- // {
- // //获取原生SQL推荐 5.1.4.63 性能OK
- // //UtilMethods.GetNativeSql(exp.sql, exp.parameters);
- // //获取无参数SQL对性能有影响,特别大的SQL参数多的,调试使用
- // //UtilMethods.GetSqlString(DbType.SqlServer, exp.sql, exp.parameters);
- // };
- // //修改SQL和参数的值
- // db.Aop.OnExecutingChangeSql = (sql, pars) =>
- // {
- // //sql=newsql
- // //foreach(var p in pars) //修改
- // return new KeyValuePair<string, SugarParameter[]>(sql, pars);
- // };
- //}
- );
- });
- #endregion
- //#region Identity 配置
- //builder.Services.AddDataProtection();
- ////不要用 AddIdentity , AddIdentity 是于MVC框架中的
- //builder.Services.AddIdentityCore<User>(opt =>
- //{
- // opt.Password.RequireDigit = false; //数字
- // opt.Password.RequireLowercase = false;//小写字母
- // opt.Password.RequireNonAlphanumeric = false;//特殊符号 例如 ¥#@!
- // opt.Password.RequireUppercase = false; //大写字母
- // opt.Password.RequiredLength = 6;//密码长度 6
- // opt.Password.RequiredUniqueChars = 1;//相同字符可以出现几次
- // opt.Lockout.MaxFailedAccessAttempts = 5; //允许最多输入五次用户名/密码错误
- // opt.Lockout.DefaultLockoutTimeSpan = new TimeSpan(0, 5, 0);//锁定五分钟
- // opt.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider; // 修改密码使用邮件【验证码模式】
- // opt.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider; ////
- //});
- //var idBuilder = new IdentityBuilder(typeof(User), typeof(UserRole), services);
- //idBuilder.AddEntityFrameworkStores<swapDbContext>().AddDefaultTokenProviders().AddRoleManager<RoleManager<UserRole>>().AddUserManager<UserManager<User>>();
- //#endregion
- #region 注入Swagger注释(启用)
- if (AppSettingsHelper.Get("UseSwagger").ToBool())
- {
- builder.Services.AddSwaggerGen(a =>
- {
- a.SwaggerDoc("v1", new OpenApiInfo
- {
- Version = "v1",
- Title = "Api",
- Description = "Api接口文档"
- });
- foreach (var item in groups)
- {
- a.SwaggerDoc(item.Item1, new OpenApiInfo { Version = item.Item1, Title = item.Item2, Description = $"{item.Item2}接口文档" });
- }
- a.DocumentFilter<SwaggerApi>();
- a.IncludeXmlComments(Path.Combine(basePath, "OASystem.Api.xml"), true);
- a.IncludeXmlComments(Path.Combine(basePath, "OASystem.Domain.xml"), true);
- a.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
- {
- Description = "Value: Bearer {token}",
- Name = "Authorization",
- In = ParameterLocation.Header,
- Type = SecuritySchemeType.ApiKey,
- Scheme = "Bearer"
- });
- a.AddSecurityRequirement(new OpenApiSecurityRequirement()
- {{
- new OpenApiSecurityScheme
- {
- Reference = new OpenApiReference
- {
- Type = ReferenceType.SecurityScheme,
- Id = "Bearer"
- }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header }, new List<string>()
- }
- });
- });
- }
- #endregion
- #region 添加校验
- builder.Services.AddTransient<OASystemAuthentication>();
- builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
- .AddJwtBearer(options =>
- {
- options.TokenValidationParameters = new TokenValidationParameters
- {
- ValidateIssuer = true,
- ValidateAudience = true,
- ValidateLifetime = true,
- ValidateIssuerSigningKey = true,
- ValidAudience = "OASystem.com",
- ValidIssuer = "OASystem.com",
- IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"])),
- ClockSkew = TimeSpan.FromSeconds(30), //过期时间容错值,解决服务器端时间不同步问题(秒)
- RequireExpirationTime = true,
- };
- options.Events = new JwtBearerEvents
- {
- OnMessageReceived = context =>
- {
- var path = context.HttpContext.Request.Path;
- //如果是signalr请求,需要将token转存,否则JWT获取不到token。OPTIONS请求需要过滤到,因为OPTIONS请求获取不到Token,用NGINX过滤掉OPTION请求.
- if (path.StartsWithSegments("/ChatHub"))
- {
- string accessToken = context.Request.Query["access_token"].ToString();
- if (string.IsNullOrWhiteSpace(accessToken))
- {
- accessToken = context.Request.Headers["Authorization"].ToString();
- }
- context.Token = accessToken.Replace("Bearer ", "").Trim();
- }
- return Task.CompletedTask;
- }
- };
- });
- #endregion
- #region 初始化日志
- Log.Logger = new LoggerConfiguration()
- .MinimumLevel.Debug()
- .WriteTo.File(Path.Combine("Logs", @"Log.txt"), rollingInterval: RollingInterval.Day)
- .CreateLogger();
- #endregion
- #region 引入注册Autofac Module
- builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
- var hostBuilder = builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
- {
- try
- {
- builder.RegisterModule(new AutofacRegister());
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message + "\n" + ex.InnerException);
- }
- });
- #endregion
- #region AutoMapper
- AutoMapper.IConfigurationProvider config = new MapperConfiguration(cfg =>
- {
- cfg.AddProfile<_baseMappingProfile>();
- });
- builder.Services.AddSingleton(config);
- builder.Services.AddScoped<IMapper, Mapper>();
- #endregion
- #region 聚合API 服务
- builder.Services.AddControllersWithViews();
- builder.Services.AddSingleton<IJuHeApiService, JuHeApiService>();
- builder.Services.AddHttpClient("PublicJuHeApi", c => c.BaseAddress = new Uri("http://web.juhe.cn"));
- builder.Services.AddHttpClient("PublicJuHeTranslateApi", c => c.BaseAddress = new Uri("http://apis.juhe.cn"));
- #endregion
- #region 企业微信API 服务
- builder.Services.AddControllersWithViews();
- builder.Services.AddSingleton<IQiYeWeChatApiService, QiYeWeChatApiService>();
- builder.Services.AddHttpClient("PublicQiYeWeChatApi", c => c.BaseAddress = new Uri("https://qyapi.weixin.qq.com"));
- #endregion
- #region 有道API 服务
- //builder.Services.AddControllersWithViews();
- //builder.Services.AddSingleton<IYouDaoApiService, YouDaoApiService>();
- //builder.Services.AddHttpClient("PublicYouDaoApi", c => c.BaseAddress = new Uri("https://openapi.youdao.com"));
- #endregion
- #region 高德地图API 服务
- builder.Services.AddHttpClient<GeocodeService>();
- #endregion
- #region Quartz
- builder.Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
- builder.Services.AddSingleton<QuartzFactory>();
- builder.Services.AddSingleton<ALiYunPostMessageJob>();
- builder.Services.AddSingleton<TaskJob>();
- builder.Services.AddSingleton<TaskNewsFeedJob>();
- //# new business
- builder.Services.AddControllersWithViews();
- builder.Services.AddSingleton<IAPNsService, APNsService>();
- builder.Services.AddSingleton<IJobFactory, IOCJobFactory>();
- #endregion
- #region SignalR
- builder.Services.AddSignalR()
- .AddJsonProtocol(options =>
- {
- options.PayloadSerializerOptions.PropertyNamingPolicy = null;
- });
- builder.Services.TryAddSingleton(typeof(CommonService));
- #endregion
- var app = builder.Build();
- AutofacIocManager.Instance.Container = app.UseHostFiltering().ApplicationServices.GetAutofacRoot();//AutofacIocManager
- // Configure the HTTP request pipeline.
- if (!app.Environment.IsDevelopment())
- {
- app.UseExceptionHandler("/Home/Error");
- }
- app.UseStaticFiles();
- app.UseMiddleware<ExceptionHandlingMiddleware>();
- app.UseRouting();
- app.UseCors("Cors"); //Cors
- app.UseAuthentication(); // 认证
- app.UseAuthorization(); // 授权
- // 授权路径
- //app.MapGet("generatetoken", c => c.Response.WriteAsync(JWTBearer.GenerateToken(c)));
- #region 启用swaggerUI
- if (AppSettingsHelper.Get("UseSwagger").ToBool())
- {
- app.UseSwagger();
- app.UseSwaggerUI(c =>
- {
- c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ver0.1");
- foreach (var item in groups)
- {
- c.SwaggerEndpoint($"/swagger/{item.Item1}/swagger.json", item.Item2);
- }
- c.RoutePrefix = string.Empty;
- c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
- c.DefaultModelsExpandDepth(-1);
- //c.EnableFilter();// 添加搜索功能
- //c.EnableDeepLinking(); // 启用深度链接
- });
- }
- #endregion
- #region Quartz
- //获取容器中的QuartzFactory
- var quartz = app.Services.GetRequiredService<QuartzFactory>();
- app.Lifetime.ApplicationStarted.Register(async () =>
- {
- await quartz.Start();
- });
- app.Lifetime.ApplicationStopped.Register(() =>
- {
- //Quzrtz关闭方法
- //quartz.Stop();
- });
- #endregion
- #region SignalR
- app.MapHub<ChatHub>("/ChatHub", options =>
- {
- options.Transports =
- HttpTransportType.WebSockets |
- HttpTransportType.LongPolling;
- });
- #endregion
- app.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{id?}");
- app.Run();
|