L1SvrController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. namespace LJProxy.Controllers
  16. {
  17. [Route("api/[controller]")]
  18. [ApiController]
  19. public class L1SvrController : Controller
  20. {
  21. private static object _syncRoot = new object();
  22. public static AppSettings _appSettingModel;
  23. public L1SvrController(IOptions<AppSettings> appSettingModel)
  24. {
  25. if (_appSettingModel == null)
  26. {
  27. _appSettingModel = appSettingModel.Value;
  28. }
  29. }
  30. private static ILJClient _PROPool { get; set; }
  31. private static ILJClient PROPool
  32. {
  33. get
  34. {
  35. if (_PROPool == null)
  36. {
  37. lock (_syncRoot)
  38. {
  39. var url = _appSettingModel.L1SvrPROUrl;
  40. var urlArr = url.Split(':');
  41. var ip = urlArr[0];
  42. var port = urlArr[1];
  43. var creator = new DirectP1ClientCreator(ip, Convert.ToInt32(port));
  44. _PROPool = new LJClientPool(creator, 1000);
  45. }
  46. }
  47. return _PROPool;
  48. }
  49. }
  50. private static ILJClient _CRPPool { get; set; }
  51. private static ILJClient CRPPool
  52. {
  53. get
  54. {
  55. if (_CRPPool == null)
  56. {
  57. lock (_syncRoot)
  58. {
  59. var url = _appSettingModel.L1SvrCRPUrl;
  60. var urlArr = url.Split(':');
  61. var ip = urlArr[0];
  62. var port = urlArr[1];
  63. var creator = new DirectP1ClientCreator(ip, Convert.ToInt32(port));
  64. _CRPPool = new LJClientPool(creator, 20);
  65. }
  66. }
  67. return _CRPPool;
  68. }
  69. }
  70. [Route("PRO/{apiName}")]
  71. [HttpPost]
  72. public string PRO(string apiName, [FromBody] object requestBody)
  73. {
  74. var rslt = PROPool.DoExcute(apiName, requestBody.ToString());
  75. return rslt;
  76. }
  77. [Route("CRP/{apiName}")]
  78. [HttpPost]
  79. [HttpGet]
  80. public async Task<IActionResult> CRP(string apiName)
  81. {
  82. string requestBody;
  83. using(StreamReader reader = new StreamReader(Request.Body,Encoding.UTF8))
  84. {
  85. requestBody = await reader.ReadToEndAsync();
  86. }
  87. var excuteResult = GlobalVar.Excute(apiName, requestBody, Request);
  88. if (excuteResult.Item1) return Json( excuteResult.Item2);
  89. else
  90. {
  91. var rslt = CRPPool.DoExcute(apiName, requestBody);
  92. return Json( rslt);
  93. }
  94. }
  95. }
  96. }