L1SvrController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using LJLib.Client;
  2. using LJLib.Net.SPI.Client;
  3. using LJLib.Net.SPI.Com;
  4. using LJProxy.Models;
  5. using LJProxy.Settings;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using Newtonsoft.Json;
  13. using System.IO;
  14. using System.Text;
  15. using Newtonsoft.Json.Linq;
  16. namespace LJProxy.Controllers
  17. {
  18. [Route("api/[controller]")]
  19. [ApiController]
  20. public class L1SvrController : Controller
  21. {
  22. private static object _syncRoot = new object();
  23. public static AppSettings _appSettingModel;
  24. public L1SvrController(IOptions<AppSettings> appSettingModel)
  25. {
  26. if (_appSettingModel == null)
  27. {
  28. _appSettingModel = appSettingModel.Value;
  29. }
  30. }
  31. private static ILJClient _PROPool { get; set; }
  32. private static ILJClient PROPool
  33. {
  34. get
  35. {
  36. if (_PROPool == null)
  37. {
  38. lock (_syncRoot)
  39. {
  40. var url = _appSettingModel.L1SvrPROUrl;
  41. var urlArr = url.Split(':');
  42. var ip = urlArr[0];
  43. var port = urlArr[1];
  44. var creator = new DirectP1ClientCreator(ip, Convert.ToInt32(port));
  45. _PROPool = new LJClientPool(creator, 1000);
  46. }
  47. }
  48. return _PROPool;
  49. }
  50. }
  51. private static ILJClient _CRPPool { get; set; }
  52. private static ILJClient CRPPool
  53. {
  54. get
  55. {
  56. if (_CRPPool == null)
  57. {
  58. lock (_syncRoot)
  59. {
  60. var url = _appSettingModel.L1SvrCRPUrl;
  61. var urlArr = url.Split(':');
  62. var ip = urlArr[0];
  63. var port = urlArr[1];
  64. var creator = new DirectP1ClientCreator(ip, Convert.ToInt32(port));
  65. _CRPPool = new LJClientPool(creator, 20);
  66. }
  67. }
  68. return _CRPPool;
  69. }
  70. }
  71. [Route("PRO/{apiName}")]
  72. [HttpPost]
  73. public string PRO(string apiName, [FromBody] object requestBody)
  74. {
  75. var rslt = PROPool.DoExcute(apiName, requestBody.ToString());
  76. return rslt;
  77. }
  78. [Route("CRP/{apiName}/{**restPath}")]
  79. [HttpPost]
  80. [HttpGet]
  81. public async Task<IActionResult> CRP(string apiName)
  82. {
  83. string requestBody;
  84. using(StreamReader reader = new StreamReader(Request.Body,Encoding.UTF8))
  85. {
  86. requestBody = await reader.ReadToEndAsync();
  87. }
  88. var excuteResult = GlobalVar.Excute(apiName, requestBody, Request);
  89. if (excuteResult.Item1) return excuteResult.Item2;
  90. else
  91. {
  92. var rslt = CRPPool.DoExcute(apiName, requestBody);
  93. return Content(rslt,"application/json");
  94. }
  95. }
  96. }
  97. }