system_info_controller.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package system
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "xg_dba/api"
  7. "xg_dba/internal/global"
  8. system "xg_dba/internal/services/system"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // //获取缓存中所有连接信息
  12. func GetSingleSystemInfo_controller(c *gin.Context) {
  13. //获取连接信息
  14. connectInfo := api.ServerInfoRequest{}
  15. if err := c.BindJSON(&connectInfo); err != nil {
  16. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  17. return
  18. }
  19. if sysInfo, err := global.Cache.GetSingleSystemInfoCache(connectInfo.BaseFileName ,connectInfo.Host); err != nil {
  20. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  21. return
  22. } else {
  23. c.JSON(http.StatusOK, gin.H{"message": "success",
  24. "data": sysInfo,
  25. })
  26. }
  27. }
  28. // 设置系统信息(todo)
  29. func SetSystemInfo_controller(c *gin.Context) {
  30. // 获取连接信息
  31. connectInfo := []api.ServerInfoRequest{}
  32. if err := c.BindJSON(&connectInfo); err != nil {
  33. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  34. return
  35. }
  36. if connectInfo[0].BaseFileName == "" {
  37. c.JSON(http.StatusBadRequest, gin.H{"error": "BaseFileName is required"})
  38. return
  39. }
  40. // 创建新的进度 channel 并返回进度 ID
  41. progressID := global.Progress.CreateProgress()
  42. // 启动任务的 goroutine
  43. go func() {
  44. system.SetSystemInfo_service(connectInfo, fmt.Sprintf("./config/%s/", connectInfo[0].BaseFileName), progressID)
  45. }()
  46. // 返回给前端 progress ID
  47. // c.JSON(http.StatusOK, gin.H{"progress_id": progressID})
  48. c.JSON(http.StatusOK, gin.H{"message": "success",
  49. "progressID": progressID,
  50. })
  51. // //使用 Stream 处理实时推送
  52. // c.Stream(func(w io.Writer) bool {
  53. // if message, ok := <-progressChan; ok {
  54. // c.SSEvent("progress", gin.H{"message": message})
  55. // fmt.Println("Progress给:", message)
  56. // return true
  57. // }
  58. // return false
  59. // })
  60. }
  61. // 设置系统信息并推送进度
  62. func GetSystemInfoProgress_controller(c *gin.Context) {
  63. // 解析请求参数
  64. progressID := c.Query("progressID") // 获取查询参数 `id`,也就是 `progressID`
  65. if progressID == "" {
  66. c.JSON(400, gin.H{
  67. "error": "progressID is required",
  68. })
  69. return
  70. }
  71. // 获取对应的进度 channel
  72. progressChan, ok := global.Progress.GetProgressChan(progressID)
  73. if !ok {
  74. c.JSON(http.StatusNotFound, gin.H{"error": "Progress ID not found"})
  75. return
  76. }
  77. // //使用 Stream 处理实时推送
  78. c.Stream(func(w io.Writer) bool {
  79. if message, ok := <-progressChan; ok {
  80. c.SSEvent("progress", gin.H{"message": message})
  81. fmt.Println("Progress给:", message)
  82. return true
  83. }
  84. return false
  85. })
  86. }