CoreController.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using System;
  8. using TEAMModelOS.SDK;
  9. using System.IO;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.DI;
  12. using Microsoft.Extensions.Options;
  13. using System.Net.Http;
  14. using TEAMModelOS.Models;
  15. namespace TEAMModelBI.Controllers.BISystem
  16. {
  17. [Route("core")]
  18. [ApiController]
  19. public class CoreController : ControllerBase
  20. {
  21. private readonly AzureCosmosFactory _azureCosmos;
  22. private readonly AzureRedisFactory _azureRedis;
  23. private readonly AzureStorageFactory _azureStorage;
  24. private readonly DingDing _dingDing;
  25. private readonly IPSearcher _searcher;
  26. private readonly Option _option;
  27. public CoreController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IPSearcher searcher, IHttpClientFactory httpClient)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _azureRedis = azureRedis;
  31. _azureStorage= azureStorage;
  32. _dingDing = dingDing;
  33. _searcher = searcher;
  34. _option = option?.Value;
  35. }
  36. /// <summary>
  37. ///获取版本信息
  38. /// </summary>
  39. /// <param name="request"></param>
  40. /// <returns></returns>
  41. [HttpPost("system-info")]
  42. [RequestSizeLimit(100_000_000)] //最大100m左右
  43. public async Task<IActionResult> SystemInfo(JsonElement request)
  44. {
  45. Type attr = this.GetType();
  46. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  47. Assembly assembly = Assembly.LoadFrom($"{currentDirectory}\\TEAMModelBI.dll");
  48. var description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  49. //var v1 = Assembly.GetEntryAssembly().GetName().Version;
  50. //var v2 = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  51. // var description = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  52. var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  53. long nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  54. //Console.WriteLine($"Assembly.GetEntryAssembly().GetName().Version: " +
  55. // $"{Assembly.GetEntryAssembly().GetName().Version}");5.2107.12.1
  56. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version:" +
  57. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}");5.2107.12.1
  58. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion:" +
  59. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion}");5.2107.12
  60. var IpPort = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
  61. if (string.IsNullOrEmpty(IpPort))
  62. {
  63. IpPort = HttpContext.Connection.RemoteIpAddress.ToString();
  64. }
  65. if (IpPort.Contains("::"))
  66. {
  67. IpPort = "127.0.0.1";
  68. }
  69. string ip = IpPort.Split(":")[0];
  70. string os = "";
  71. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  72. {//Linux
  73. os = "Linux";
  74. }
  75. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  76. {//Windows
  77. os = "Windows";
  78. }
  79. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  80. {//OSX
  81. os = "OSX";
  82. }
  83. string region = await _searcher.SearchIpAsync(ip);
  84. return Ok(new { os, version, description, nowtime, region, ip });
  85. }
  86. }
  87. }