Startup.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Http.Features;
  8. using Microsoft.AspNetCore.Rewrite;
  9. using Microsoft.AspNetCore.StaticFiles;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.FileProviders;
  13. using Microsoft.Extensions.Hosting;
  14. using System.Text;
  15. using Microsoft.IdentityModel.Tokens;
  16. using System.Linq;
  17. using Microsoft.AspNetCore.Mvc.Razor;
  18. using Microsoft.AspNetCore.Server.Kestrel.Core;
  19. namespace MySystem
  20. {
  21. public class Startup
  22. {
  23. public Startup(IConfiguration configuration)
  24. {
  25. Configuration = configuration;
  26. }
  27. public IConfiguration Configuration { get; }
  28. // This method gets called by the runtime. Use this method to add services to the container.
  29. public void ConfigureServices(IServiceCollection services)
  30. {
  31. services.AddControllersWithViews();
  32. services.AddRouting(options =>
  33. {
  34. options.LowercaseUrls = true;
  35. });
  36. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  37. services.Configure<Setting>(Configuration.GetSection("Setting"));
  38. // services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  39. services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
  40. services.AddMvc(options =>
  41. {
  42. options.EnableEndpointRouting = false;
  43. options.Filters.Add(typeof(GlobalExceptions));
  44. });
  45. //配置模版视图路径
  46. services.Configure<RazorViewEngineOptions>(options =>
  47. {
  48. options.ViewLocationExpanders.Add(new TemplateViewLocationExpander());
  49. });
  50. services.AddSession(options =>
  51. {
  52. // 设置 Session 过期时间
  53. options.IdleTimeout = TimeSpan.FromDays(1);
  54. options.Cookie.HttpOnly = true;
  55. });
  56. services.Configure<FormOptions>(x =>
  57. {
  58. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  59. });
  60. //生成密钥
  61. var symmetricKeyAsBase64 = Configuration["Setting:JwtSecret"];
  62. var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
  63. var signingKey = new SymmetricSecurityKey(keyByteArray);
  64. //认证参数
  65. services.AddAuthentication("Bearer").AddJwtBearer(o =>
  66. {
  67. o.TokenValidationParameters = new TokenValidationParameters
  68. {
  69. ValidateIssuerSigningKey = true,//是否验证签名,不验证的画可以篡改数据,不安全
  70. IssuerSigningKey = signingKey,//解密的密钥
  71. ValidateIssuer = true,//是否验证发行人,就是验证载荷中的Iss是否对应ValidIssuer参数
  72. // ValidIssuer = Configuration["Setting:JwtIss"],//发行人
  73. IssuerValidator = (m, n, z) =>
  74. {
  75. return n.Issuer;
  76. },
  77. ValidateAudience = true,//是否验证订阅人,就是验证载荷中的Aud是否对应ValidAudience参数
  78. // ValidAudience = Configuration["Setting:JwtAud"],//订阅人
  79. AudienceValidator = (m, n, z) =>
  80. {
  81. string check = RedisDbconn.Instance.Get<string>("utoken:" + n.Issuer);
  82. return m != null && m.FirstOrDefault().Equals(check);
  83. },
  84. ValidateLifetime = true,//是否验证过期时间,过期了就拒绝访问
  85. ClockSkew = TimeSpan.Zero,//这个是缓冲过期时间,也就是说,即使我们配置了过期时间,这里也要考虑进去,过期时间+缓冲,默认好像是7分钟,你可以直接设置为0
  86. RequireExpirationTime = true,
  87. };
  88. });
  89. MySystemLib.SystemPublicFuction.appcheck = "success";
  90. RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
  91. // TendisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:TendisConnStr"]);
  92. }
  93. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  94. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  95. {
  96. string Env = "Develop";
  97. if (env.IsDevelopment())
  98. {
  99. Env = "Develop";
  100. app.UseDeveloperExceptionPage();
  101. }
  102. else
  103. {
  104. Env = "Production";
  105. app.UseHsts();
  106. }
  107. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  108. app.UseStaticFiles();
  109. app.UseStaticFiles(new StaticFileOptions
  110. {
  111. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  112. {
  113. { ".apk", "application/vnd.android.package-archive" },
  114. { ".xlsx", "application/vnd.android.package-archive" }
  115. })
  116. });
  117. app.UseCors("cors");
  118. app.UseAuthentication();
  119. app.UseRouting();
  120. app.UseAuthorization();
  121. app.UseSession();
  122. app.UseEndpoints(endpoints =>
  123. {
  124. endpoints.MapControllerRoute(
  125. name: "default",
  126. pattern: "{controller=Home}/{action=Index}/{Id?}");
  127. });
  128. initMainServer(Env);
  129. initBsServer();
  130. initCashServer();
  131. initSpServer();
  132. initOperateServer();
  133. if(Env == "Develop")
  134. {
  135. }
  136. if(Env == "Production")
  137. {
  138. ResetUserTradeService.Instance.Start();
  139. ResetMerchantTradeService.Instance.Start();
  140. SycnProfitServiceV3.Instance.Start();
  141. SycnHelpProfitService.Instance.Start();
  142. ExcelHelper.Instance.Start();
  143. OpExcelHelper.Instance.Start();
  144. SycnUserMachineCountHelper.Instance.Start(); //重置创客机具数量
  145. TestHelper.Instance.Start(); //生成兑换券
  146. }
  147. }
  148. //初始化数据结构
  149. private void initMainServer(string Env)
  150. {
  151. string dbName = "KxsMainServer";
  152. // if(Env == "Production")
  153. // {
  154. // dbName = "KxsProfitServer";
  155. // }
  156. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  157. Library.OtherMySqlConn.connstr = Configuration["Setting:SqlConnStr"];
  158. System.Data.DataTable tablecollection = Library.OtherMySqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = '" + dbName + "'");
  159. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  160. {
  161. Dictionary<string, string> Columns = new Dictionary<string, string>();
  162. System.Data.DataTable columncollection = Library.OtherMySqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = '" + dbName + "' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'");
  163. foreach (System.Data.DataRow column in columncollection.Rows)
  164. {
  165. string datatype = column["DATA_TYPE"].ToString();
  166. if (datatype == "decimal")
  167. {
  168. datatype = "numeric";
  169. }
  170. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  171. }
  172. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  173. }
  174. Library.OtherMySqlConn.connstr = "";
  175. PublicFunction.MainTables = tables;
  176. }
  177. private void initBsServer()
  178. {
  179. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  180. Library.OtherMySqlConn.connstr = Configuration["Setting:BsSqlConnStr"];
  181. System.Data.DataTable tablecollection = Library.OtherMySqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'KxsBsServer'");
  182. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  183. {
  184. Dictionary<string, string> Columns = new Dictionary<string, string>();
  185. System.Data.DataTable columncollection = Library.OtherMySqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'KxsBsServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'");
  186. foreach (System.Data.DataRow column in columncollection.Rows)
  187. {
  188. string datatype = column["DATA_TYPE"].ToString();
  189. if (datatype == "decimal")
  190. {
  191. datatype = "numeric";
  192. }
  193. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  194. }
  195. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  196. }
  197. Library.OtherMySqlConn.connstr = "";
  198. PublicFunction.BsTables = tables;
  199. }
  200. private void initCashServer()
  201. {
  202. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  203. Library.OtherMySqlConn.connstr = Configuration["Setting:CashSqlConnStr"];
  204. System.Data.DataTable tablecollection = Library.OtherMySqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'KxsCashServer'");
  205. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  206. {
  207. Dictionary<string, string> Columns = new Dictionary<string, string>();
  208. System.Data.DataTable columncollection = Library.OtherMySqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'KxsCashServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'");
  209. foreach (System.Data.DataRow column in columncollection.Rows)
  210. {
  211. string datatype = column["DATA_TYPE"].ToString();
  212. if (datatype == "decimal")
  213. {
  214. datatype = "numeric";
  215. }
  216. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  217. }
  218. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  219. }
  220. Library.OtherMySqlConn.connstr = "";
  221. PublicFunction.CashTables = tables;
  222. }
  223. private void initSpServer()
  224. {
  225. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  226. Library.OtherMySqlConn.connstr = Configuration["Setting:SpSqlConnStr"];
  227. System.Data.DataTable tablecollection = Library.OtherMySqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'KxsSpServer'");
  228. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  229. {
  230. Dictionary<string, string> Columns = new Dictionary<string, string>();
  231. System.Data.DataTable columncollection = Library.OtherMySqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'KxsSpServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'");
  232. foreach (System.Data.DataRow column in columncollection.Rows)
  233. {
  234. string datatype = column["DATA_TYPE"].ToString();
  235. if (datatype == "decimal")
  236. {
  237. datatype = "numeric";
  238. }
  239. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  240. }
  241. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  242. }
  243. Library.OtherMySqlConn.connstr = "";
  244. PublicFunction.SpTables = tables;
  245. }
  246. //初始化运营中心数据结构
  247. private void initOperateServer()
  248. {
  249. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  250. Library.OtherMySqlConn.connstr = Configuration["Setting:OpSqlConnStr"];
  251. System.Data.DataTable tablecollection = Library.OtherMySqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'KxsOpServer'");
  252. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  253. {
  254. Dictionary<string, string> Columns = new Dictionary<string, string>();
  255. System.Data.DataTable columncollection = Library.OtherMySqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'KxsOpServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'");
  256. foreach (System.Data.DataRow column in columncollection.Rows)
  257. {
  258. string datatype = column["DATA_TYPE"].ToString();
  259. if (datatype == "decimal")
  260. {
  261. datatype = "numeric";
  262. }
  263. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  264. }
  265. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  266. }
  267. Library.OtherMySqlConn.connstr = "";
  268. PublicFunction.OpTables = tables;
  269. }
  270. }
  271. }