You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
10KB

  1. using AspectCore.Extensions.DependencyInjection;
  2. using HealthMonitor.Common;
  3. using HealthMonitor.Common.helper;
  4. using HealthMonitor.Core.Cache;
  5. using HealthMonitor.Core.Context;
  6. using HealthMonitor.Core.Dal.EfCoreImpl;
  7. using HealthMonitor.Core.Dal;
  8. using HealthMonitor.Core.Dal.Factory;
  9. using HealthMonitor.Core.DbLog;
  10. using HealthMonitor.Core.Operator;
  11. using HealthMonitor.Core.Operator.Redis;
  12. using HealthMonitor.Model.Config;
  13. using HealthMonitor.Service.Biz.db;
  14. using HealthMonitor.WebApi.Configs;
  15. using HealthMonitor.WebApi.DbLog;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Microsoft.EntityFrameworkCore;
  18. using Microsoft.Extensions.Configuration;
  19. using Microsoft.Extensions.DependencyInjection;
  20. using Microsoft.Extensions.Options;
  21. using Microsoft.OpenApi.Models;
  22. using HealthMonitor.WebApi.Swagger;
  23. using HealthMonitor.Service.Cache;
  24. using TelpoDataService.Util.Clients;
  25. using HealthMonitor.Service.Sub;
  26. using HealthMonitor.Service.Resolver;
  27. using HealthMonitor.Service.Resolver.Factory;
  28. using HealthMonitor.Service.Resolver.Interface;
  29. namespace HealthMonitor.WebApi
  30. {
  31. public class Program
  32. {
  33. public static void Main(string[] args)
  34. {
  35. var builder = WebApplication.CreateBuilder(args);
  36. // Add services to the container.
  37. builder.Services.AddHttpClient(Consts.DEFAULT_HTTPCLIENT_NAME, c =>
  38. {
  39. c.Timeout = TimeSpan.FromSeconds(10); //超时限制
  40. c.DefaultRequestHeaders.Add("Accept", "application/json");
  41. //c.DefaultRequestHeaders.Connection.Add("keep-alive");
  42. });
  43. // builder.Services.Configure<TDengineServiceConfig>(builder.Configuration.GetSection("TDengineServiceConfig"));
  44. builder.Services.AddSingleton<HttpHelper>();
  45. builder.Services.AddSingleton<TDengineService>();
  46. builder.Services.AddControllers();
  47. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  48. builder.Services.AddEndpointsApiExplorer();
  49. builder.Services.AddSwaggerGen();
  50. #region scoped MySQL 数据库
  51. builder.Services // gps_card
  52. .AddDbContextPool<GpsCardContext>((sp, options) =>
  53. {
  54. var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>();
  55. var mySqlCon = builder.Configuration.GetConnectionString("GpsCard_Connection_String");
  56. var serverVersion = ServerVersion.AutoDetect(mySqlCon);
  57. options.UseMySql(mySqlCon, serverVersion)
  58. .UseLoggerFactory(loggerFactory);
  59. }, poolSize: 64) // health_monitor
  60. .AddDbContextPool<HealthMonitorContext>((sp, options) =>
  61. {
  62. var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>();
  63. var mySqlCon = builder.Configuration.GetConnectionString("HealthMonitor_Connection_String");
  64. var serverVersion = ServerVersion.AutoDetect(mySqlCon);
  65. options.UseMySql(mySqlCon, serverVersion)
  66. .UseLoggerFactory(loggerFactory);
  67. }, poolSize: 64); ;
  68. builder.Services
  69. .AddScoped<IGpsCardDataAccessor, EfCoreDataAccessor>(sp =>
  70. {
  71. var context = sp.GetRequiredService<GpsCardContext>();
  72. return new EfCoreDataAccessor(context);
  73. })
  74. .AddScoped<IHealthMonitorDataAccessor, EfCoreDataAccessor>(sp =>
  75. {
  76. var context = sp.GetRequiredService<HealthMonitorContext>();
  77. return new EfCoreDataAccessor(context);
  78. });
  79. //builder.Services.AddDbContextPool<HealthMonitorContext>((sp, options) =>
  80. //{
  81. // var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>();
  82. // options.UseMySql(builder.Configuration.GetConnectionString("GpsCard_Connection_String"))
  83. // .UseLoggerFactory(loggerFactory);
  84. //}, poolSize: 64);
  85. //builder.Services.AddScoped<IHealthMonitorDataAccessor, EfCoreDataAccessor>(sp =>
  86. //{
  87. // var context = sp.GetRequiredService<HealthMonitorContext>();
  88. // return new EfCoreDataAccessor(context);
  89. //});
  90. #endregion
  91. #region AOP
  92. //builder.Services.Configure<RedisConfig>(builder.Configuration.GetSection("Redis"));
  93. builder.Services
  94. .Configure<ApiBehaviorOptions>(options =>
  95. {
  96. options.SuppressModelStateInvalidFilter = true;
  97. })
  98. .Configure<RedisConfig>(builder.Configuration.GetSection("Redis"))
  99. .Configure<TDengineServiceConfig>(builder.Configuration.GetSection("TDengineServiceConfig"))
  100. .Configure<ServiceConfig>(builder.Configuration.GetSection("ServiceConfig"));
  101. builder.Services
  102. .AddSingleton<IEfCoreLoggerProvider, EfCoreLogProvider>()
  103. .AddSingleton<IDurableEntityManager, DurableEntityManager>()
  104. .AddSingleton<EfCoreLoggerFactory>(sp =>
  105. {
  106. var provider = sp.GetRequiredService<IEfCoreLoggerProvider>();
  107. return new EfCoreLoggerFactory(new[] { provider });
  108. });
  109. //builder.Services.AddSingleton<IDurableEntityManager, DurableEntityManager>();
  110. //builder.Services.AddSingleton<EfCoreLoggerFactory>(sp =>
  111. //{
  112. // var provider = sp.GetRequiredService<IEfCoreLoggerProvider>();
  113. // return new EfCoreLoggerFactory(new[] { provider });
  114. //});
  115. builder.Services
  116. .AddSingleton<GpsCardAccessorFactory>()
  117. .AddSingleton<HealthMonitorAccessorFactory>();
  118. //builder.Services.AddSingleton<HealthMonitorAccessorFactory>();
  119. builder.Services
  120. .AddSingleton<IGpsCardOperatorManager, GpsCardOperatorRedisManager>()
  121. .AddSingleton<IHealthMonitorOperatorManager, HealthMonitorOperatorRedisManager>();
  122. //builder.Services.AddSingleton<IHealthMonitorOperatorManager, HealthMonitorOperatorRedisManager>();
  123. //用AspectCore替换默认的IOC容器
  124. builder.Host.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
  125. #endregion
  126. #region Cache
  127. builder.Services
  128. .AddSingleton<PersonCacheManager>()
  129. .AddSingleton<BloodPressReferenceValueCacheManager>();
  130. #endregion
  131. #region TelpoDataServices
  132. builder.Services.AddTelpoDataServices(opt =>
  133. {
  134. opt.TelpoDataUrl = builder.Configuration.GetSection("ServiceConfig:TelpoDataUrl").Value;
  135. });
  136. #endregion
  137. #region Worker
  138. builder.Services.AddSingleton<IResolverFactory, ResolverFactory>();
  139. builder.Services.AddSingleton<BloodpressResolver>();
  140. builder.Services.AddSingleton<PackageProcess>();
  141. builder.Services
  142. .AddSingleton<TDengineDataSubcribe>()
  143. .AddSingleton<MsgManager>()
  144. .AddHostedService<Worker>();
  145. #endregion
  146. // Register the Swagger generator, defining 1 or more Swagger documents
  147. builder.Services.AddSwaggerGen(c =>
  148. {
  149. //c.SwaggerDoc(AppConsts.SWAGGER_DOC_GpsCard, new OpenApiInfo { Title = "GpsCard模块", Version = "v1", Description = "gps_card数据库服务" }); //分组显示
  150. c.SwaggerDoc(AppConsts.SWAGGER_DOC_HealthMonitor, new OpenApiInfo { Title = "HealthMonitor模块", Version = "v1", Description = "health_monitor数据库服务" }); //分组显示
  151. c.DocumentFilter<EnumDocumentFilter>();
  152. //var utilXmlFile = Path.Combine(AppContext.BaseDirectory, "HealthMonitor.Util.xml");
  153. //var webapiXmlFile = Path.Combine(AppContext.BaseDirectory, "HealthMonitor.WebApi.xml");
  154. //c.IncludeXmlComments(utilXmlFile, true);
  155. //c.IncludeXmlComments(webapiXmlFile, true);
  156. });
  157. var app = builder.Build();
  158. // Configure the HTTP request pipeline.
  159. //if (app.Environment.IsDevelopment())
  160. //{
  161. // app.UseSwagger();
  162. // app.UseSwaggerUI();
  163. //}
  164. string prefix = string.Empty;
  165. //if (!app.Environment.IsDebugOrDevelop())
  166. //{
  167. // prefix = optConfigAppSettings.Value.NginxPrefix;
  168. // if (string.IsNullOrWhiteSpace(prefix)) prefix = SERVICE_PREFIX;
  169. //}
  170. // Enable middleware to serve generated Swagger as a JSON endpoint.
  171. app.UseSwagger(c =>
  172. {
  173. c.PreSerializeFilters.Add((swagger, httpReq) =>
  174. {
  175. var scheme = httpReq.Headers.TryGetValue("X-Forwarded-Proto", out var v2) ? v2.ToString() : httpReq.Scheme;
  176. swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{scheme}://{httpReq.Host}/{prefix}" } };
  177. });
  178. c.RouteTemplate = "/{documentName}/api-docs/";
  179. });
  180. // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
  181. // specifying the Swagger JSON endpoint.
  182. app.UseSwaggerUI(c =>
  183. {
  184. string sub = string.IsNullOrEmpty(prefix) ? "" : $"/{prefix}";
  185. //c.SwaggerEndpoint($"{sub}/{AppConsts.SWAGGER_DOC_GpsCard}/api-docs/", "GpsCard模块"); //分组显示
  186. c.SwaggerEndpoint($"{sub}/{AppConsts.SWAGGER_DOC_HealthMonitor}/api-docs/", "HealthMonitor模块"); //分组显示
  187. c.RoutePrefix = string.Empty;
  188. });
  189. // redis default
  190. var csredis = new CSRedis.CSRedisClient(app.Services.GetService<IOptions<RedisConfig>>()!.Value.ToString());
  191. RedisHelper.Initialization(csredis);
  192. // redis db7
  193. var csredisDb7Con = app.Services.GetService<IOptions<RedisConfig>>()!.Value;
  194. csredisDb7Con.DefaultDatabase = 7;
  195. csredisDb7Con.Prefix = "TELPO";
  196. var csredisDb7 = new CSRedis.CSRedisClient(csredisDb7Con.ToString());
  197. RedisHelperDb7.Initialization(csredisDb7);
  198. app.UseHttpsRedirection();
  199. app.UseAuthorization();
  200. app.MapControllers();
  201. app.Run();
  202. }
  203. }
  204. }