Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

291 Zeilen
11KB

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