connect_controllers.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package connect
  2. import (
  3. "net/http"
  4. "xg_dba/api"
  5. "xg_dba/internal/global"
  6. services "xg_dba/internal/services/connect"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // 获取连接信息
  10. func GetConnectInfo_controller(c *gin.Context) {
  11. if data, err := global.Cache.GetConnectCache(); err != nil {
  12. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  13. return
  14. } else {
  15. // fmt.Printf("get connect info from cache :%#v :", data)
  16. c.JSON(http.StatusOK, gin.H{"message": "success",
  17. "data": data,
  18. })
  19. }
  20. }
  21. // 保存连接信息
  22. func AddConnectInfo_controller(c *gin.Context) {
  23. connectInfo := []api.ConnectInfoRequest{}
  24. if err := c.BindJSON(&connectInfo); err != nil {
  25. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  26. return
  27. }
  28. //判断地址合法性
  29. if connectInfo[0].Ssh.Host == "" {
  30. c.JSON(http.StatusBadRequest, gin.H{"error": "host不能为空"})
  31. return
  32. }
  33. //存储到本地
  34. localPath := "./config/" + connectInfo[0].Ssh.Host + "/" + "listen" + ".toml"
  35. if err := services.SetConnectInfo_service(connectInfo, localPath); err != nil {
  36. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  37. return
  38. }
  39. c.JSON(http.StatusOK, gin.H{"message": "success"})
  40. }