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.

247 lines
9.8KB

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