選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Program.cs 12KB

3ヶ月前
4ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
4ヶ月前
3ヶ月前
1年前
1年前
3ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. using Serilog;
  30. using Serilog.Core;
  31. using HealthMonitor.WebApi.HttpLog;
  32. using Microsoft.Extensions.Http;
  33. using Microsoft.Extensions.DependencyInjection.Extensions;
  34. using HealthMonitor.Service.Etcd;
  35. using HealthMonitor.WebApi.Middleware;
  36. using HealthMonitor.Service.Biz;
  37. using HealthMonitor.Service.MessageQueue.Kafka;
  38. using HealthMonitor.Service.MessageQueue;
  39. namespace HealthMonitor.WebApi
  40. {
  41. public class Program
  42. {
  43. public static void Main(string[] args)
  44. {
  45. //选择配置文件appsetting.json
  46. var config = new ConfigurationBuilder()
  47. .SetBasePath(Directory.GetCurrentDirectory())
  48. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  49. .Build();
  50. Log.Logger = new LoggerConfiguration()
  51. .ReadFrom.Configuration(config).Enrich.WithThreadInfo()
  52. .CreateLogger();
  53. var builder = WebApplication.CreateBuilder(args);
  54. // Add services to the container.
  55. builder.Services.AddHttpClient(Consts.DEFAULT_HTTPCLIENT_NAME, c =>
  56. {
  57. c.Timeout = TimeSpan.FromSeconds(60); //超时限制
  58. c.DefaultRequestHeaders.Add("Accept", "application/json");
  59. //c.DefaultRequestHeaders.Connection.Add("keep-alive");
  60. });
  61. // builder.Services.Configure<TDengineServiceConfig>(builder.Configuration.GetSection("TDengineServiceConfig"));
  62. builder.Services.AddSingleton<HttpHelper>();
  63. builder.Services.AddSingleton<TDengineService>();
  64. builder.Services.AddControllers();
  65. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  66. builder.Services.AddEndpointsApiExplorer();
  67. builder.Services.AddSwaggerGen();
  68. #region scoped MySQL 数据库
  69. builder.Services // gps_card
  70. .AddDbContextPool<GpsCardContext>((sp, options) =>
  71. {
  72. var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>();
  73. var mySqlCon = builder.Configuration.GetConnectionString("GpsCard_Connection_String");
  74. var serverVersion = ServerVersion.AutoDetect(mySqlCon);
  75. options.UseMySql(mySqlCon, serverVersion)
  76. .UseLoggerFactory(loggerFactory);
  77. }, poolSize: 64) // health_monitor
  78. .AddDbContextPool<HealthMonitorContext>((sp, options) =>
  79. {
  80. var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>();
  81. var mySqlCon = builder.Configuration.GetConnectionString("HealthMonitor_Connection_String");
  82. var serverVersion = ServerVersion.AutoDetect(mySqlCon);
  83. options.UseMySql(mySqlCon, serverVersion)
  84. .UseLoggerFactory(loggerFactory);
  85. }, poolSize: 64);
  86. builder.Services
  87. .AddScoped<IGpsCardDataAccessor, EfCoreDataAccessor>(sp =>
  88. {
  89. var context = sp.GetRequiredService<GpsCardContext>();
  90. return new EfCoreDataAccessor(context);
  91. })
  92. .AddScoped<IHealthMonitorDataAccessor, EfCoreDataAccessor>(sp =>
  93. {
  94. var context = sp.GetRequiredService<HealthMonitorContext>();
  95. return new EfCoreDataAccessor(context);
  96. });
  97. //builder.Services.AddDbContextPool<HealthMonitorContext>((sp, options) =>
  98. //{
  99. // var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>();
  100. // options.UseMySql(builder.Configuration.GetConnectionString("GpsCard_Connection_String"))
  101. // .UseLoggerFactory(loggerFactory);
  102. //}, poolSize: 64);
  103. //builder.Services.AddScoped<IHealthMonitorDataAccessor, EfCoreDataAccessor>(sp =>
  104. //{
  105. // var context = sp.GetRequiredService<HealthMonitorContext>();
  106. // return new EfCoreDataAccessor(context);
  107. //});
  108. #endregion
  109. #region AOP
  110. //builder.Services.Configure<RedisConfig>(builder.Configuration.GetSection("Redis"));
  111. builder.Services
  112. .Configure<ApiBehaviorOptions>(options =>
  113. {
  114. options.SuppressModelStateInvalidFilter = true;
  115. })
  116. .Configure<RedisConfig>(builder.Configuration.GetSection("Redis"))
  117. .Configure<TDengineServiceConfig>(builder.Configuration.GetSection("TDengineServiceConfig"))
  118. .Configure<ServiceConfig>(builder.Configuration.GetSection("ServiceConfig"))
  119. .Configure<BoodPressResolverConfig>(builder.Configuration.GetSection("BoodPressResolverConfig"));
  120. builder.Services
  121. .AddSingleton<IEfCoreLoggerProvider, EfCoreLogProvider>()
  122. .AddSingleton<IDurableEntityManager, DurableEntityManager>()
  123. .AddSingleton<EfCoreLoggerFactory>(sp =>
  124. {
  125. var provider = sp.GetRequiredService<IEfCoreLoggerProvider>();
  126. return new EfCoreLoggerFactory(new[] { provider });
  127. });
  128. //builder.Services.AddSingleton<IDurableEntityManager, DurableEntityManager>();
  129. //builder.Services.AddSingleton<EfCoreLoggerFactory>(sp =>
  130. //{
  131. // var provider = sp.GetRequiredService<IEfCoreLoggerProvider>();
  132. // return new EfCoreLoggerFactory(new[] { provider });
  133. //});
  134. builder.Services
  135. .AddSingleton<GpsCardAccessorFactory>()
  136. .AddSingleton<HealthMonitorAccessorFactory>();
  137. //builder.Services.AddSingleton<HealthMonitorAccessorFactory>();
  138. builder.Services
  139. .AddSingleton<IGpsCardOperatorManager, GpsCardOperatorRedisManager>()
  140. .AddSingleton<IHealthMonitorOperatorManager, HealthMonitorOperatorRedisManager>();
  141. //builder.Services.AddSingleton<IHealthMonitorOperatorManager, HealthMonitorOperatorRedisManager>();
  142. //用AspectCore替换默认的IOC容器
  143. builder.Host.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
  144. #endregion
  145. #region Cache
  146. builder.Services
  147. .AddSingleton<PersonCacheManager>()
  148. .AddSingleton<DeviceCacheManager>()
  149. .AddSingleton<FhrPhrMapCacheManager>()
  150. .AddSingleton<FetalMovementNormalValueRangeCacheManager>()
  151. .AddSingleton<BloodPressReferenceValueCacheManager>();
  152. #endregion
  153. #region TelpoDataServices
  154. builder.Services.AddTelpoDataServices(opt =>
  155. {
  156. opt.TelpoDataUrl = builder.Configuration.GetSection("ServiceConfig:TelpoDataUrl").Value;
  157. });
  158. #endregion
  159. #region Worker
  160. builder.Services.AddSingleton<MsgQueueManager>();
  161. builder.Services.AddSingleton<IResolverFactory, ResolverFactory>();
  162. builder.Services.AddSingleton<BloodpressResolver>();
  163. builder.Services.AddSingleton<PregnancyHeartRateResolver>();
  164. builder.Services.AddSingleton<PackageProcess>();
  165. builder.Services
  166. .AddSingleton<TDengineDataSubcribe>()
  167. .AddSingleton<IotApiService>()
  168. .AddSingleton<EtcdService>()
  169. .AddHostedService<Worker>();
  170. #endregion
  171. #region kafka
  172. builder.Services.AddSingleton<MqProcessLogic>();
  173. builder.Services.AddSingleton<KafkaService>();
  174. #endregion
  175. builder.Host.UseSerilog();
  176. builder.Services.Replace(ServiceDescriptor.Singleton<IHttpMessageHandlerBuilderFilter, CustomLoggingFilter>());
  177. // Register the Swagger generator, defining 1 or more Swagger documents
  178. builder.Services.AddSwaggerGen(c =>
  179. {
  180. //c.SwaggerDoc(AppConsts.SWAGGER_DOC_GpsCard, new OpenApiInfo { Title = "GpsCard模块", Version = "v1", Description = "gps_card数据库服务" }); //分组显示
  181. c.SwaggerDoc(AppConsts.SWAGGER_DOC_HealthMonitor, new OpenApiInfo { Title = "HealthMonitor模块", Version = "v1", Description = "health_monitor数据库服务" }); //分组显示
  182. c.DocumentFilter<EnumDocumentFilter>();
  183. //var utilXmlFile = Path.Combine(AppContext.BaseDirectory, "HealthMonitor.Util.xml");
  184. //var webapiXmlFile = Path.Combine(AppContext.BaseDirectory, "HealthMonitor.WebApi.xml");
  185. //c.IncludeXmlComments(utilXmlFile, true);
  186. //c.IncludeXmlComments(webapiXmlFile, true);
  187. });
  188. var app = builder.Build();
  189. // Configure the HTTP request pipeline.
  190. //if (app.Environment.IsDevelopment())
  191. //{
  192. // app.UseSwagger();
  193. // app.UseSwaggerUI();
  194. //}
  195. string prefix = string.Empty;
  196. if (!app.Environment.IsDevelopment())
  197. {
  198. //prefix = optConfigAppSettings.Value.NginxPrefix;
  199. //if (string.IsNullOrWhiteSpace(prefix)) prefix = SERVICE_PREFIX;
  200. prefix = "hm";
  201. }
  202. // Enable middleware to serve generated Swagger as a JSON endpoint.
  203. app.UseSwagger(c =>
  204. {
  205. c.PreSerializeFilters.Add((swagger, httpReq) =>
  206. {
  207. var scheme = httpReq.Headers.TryGetValue("X-Forwarded-Proto", out var v2) ? v2.ToString() : httpReq.Scheme;
  208. swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{scheme}://{httpReq.Host}/{prefix}" } };
  209. });
  210. c.RouteTemplate = "/{documentName}/api-docs/";
  211. });
  212. // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
  213. // specifying the Swagger JSON endpoint.
  214. app.UseSwaggerUI(c =>
  215. {
  216. string sub = string.IsNullOrEmpty(prefix) ? "" : $"/{prefix}";
  217. //c.SwaggerEndpoint($"{sub}/{AppConsts.SWAGGER_DOC_GpsCard}/api-docs/", "GpsCard模块"); //分组显示
  218. c.SwaggerEndpoint($"{sub}/{AppConsts.SWAGGER_DOC_HealthMonitor}/api-docs/", "HealthMonitor模块"); //分组显示
  219. c.RoutePrefix = string.Empty;
  220. });
  221. // redis default
  222. var csredis = new CSRedis.CSRedisClient(app.Services.GetService<IOptions<RedisConfig>>()!.Value.ToString());
  223. RedisHelper.Initialization(csredis);
  224. // redis db7
  225. var csredisDb7Con = app.Services.GetService<IOptions<RedisConfig>>()!.Value;
  226. csredisDb7Con.DefaultDatabase = 7;
  227. csredisDb7Con.Prefix = "TELPO";
  228. var csredisDb7 = new CSRedis.CSRedisClient(csredisDb7Con.ToString());
  229. RedisHelperDb7.Initialization(csredisDb7);
  230. // redis db10
  231. var csredisDb10Con = app.Services.GetService<IOptions<RedisConfig>>()!.Value;
  232. csredisDb10Con.DefaultDatabase = 10;
  233. csredisDb10Con.Prefix = "_GW_";
  234. var csredisDb10 = new CSRedis.CSRedisClient(csredisDb10Con.ToString());
  235. RedisHelperDb10.Initialization(csredisDb10);
  236. app.UseHttpsRedirection();
  237. app.UseAuthorization();
  238. app.UseMiddleware<LoggingMiddleware>();
  239. app.MapControllers();
  240. app.Run();
  241. }
  242. }
  243. }