gatewayipadmin.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package admin
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. v1admin "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  9. "github.com/go-nunu/nunu-layout-advanced/internal/repository/admin"
  10. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  11. "github.com/hashicorp/go-multierror"
  12. "github.com/spf13/viper"
  13. )
  14. type GatewayIpAdminService interface {
  15. GetGatewayIpAdmin(ctx context.Context, id int64) (*model.Gatewayip, error)
  16. GetGatewayGroupIpList(ctx context.Context,req v1admin.SearchGatewayIpParams) (*v1.PaginatedResponse[model.Gatewayip], error)
  17. AddGatewayIp(ctx context.Context,req model.Gatewayip) error
  18. EditGatewayIp(ctx context.Context,req model.Gatewayip) error
  19. DeleteGatewayIp(ctx context.Context,id int64) error
  20. DeleteGatewayIps(ctx context.Context, ids []int64) error
  21. }
  22. func NewGatewayIpAdminService(
  23. service *service.Service,
  24. gatewayIpAdminRepository admin.GatewayIpAdminRepository,
  25. config *viper.Viper,
  26. request service.RequestService,
  27. ) GatewayIpAdminService {
  28. return &gatewayIpAdminService{
  29. Service: service,
  30. gatewayIpAdminRepository: gatewayIpAdminRepository,
  31. request: request,
  32. config: config,
  33. }
  34. }
  35. type gatewayIpAdminService struct {
  36. *service.Service
  37. gatewayIpAdminRepository admin.GatewayIpAdminRepository
  38. config *viper.Viper
  39. request service.RequestService
  40. }
  41. func (s *gatewayIpAdminService) sendIp(ctx context.Context, ip string, action string,nodeArea string) error {
  42. serverIps := s.config.GetStringSlice("addServerIp." + nodeArea)
  43. for _, serverIp := range serverIps {
  44. apiUrl := "http://" + serverIp + ":3075/" + action
  45. formData := map[string]interface{}{
  46. "ip": ip,
  47. }
  48. resBody, err := s.request.Request(ctx, formData, apiUrl, "", "")
  49. if err != nil {
  50. return err
  51. }
  52. var res v1.GeneralResponse[any]
  53. err = json.Unmarshal(resBody, &res)
  54. if err != nil {
  55. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  56. }
  57. if res.Code != 0 {
  58. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  59. }
  60. }
  61. return nil
  62. }
  63. func (s *gatewayIpAdminService) GetGatewayIpAdmin(ctx context.Context, id int64) (*model.Gatewayip, error) {
  64. return s.gatewayIpAdminRepository.GetGatewayIpAdmin(ctx, id)
  65. }
  66. func (s *gatewayIpAdminService) GetGatewayGroupIpList(ctx context.Context,req v1admin.SearchGatewayIpParams) (*v1.PaginatedResponse[model.Gatewayip], error) {
  67. return s.gatewayIpAdminRepository.GetGatewayGroupIpList(ctx,req)
  68. }
  69. func (s *gatewayIpAdminService) AddGatewayIp(ctx context.Context,req model.Gatewayip) error {
  70. // 启动网关组IP
  71. if req.NodeArea != "" {
  72. err := s.sendIp(ctx,req.Ip,"addIp",req.NodeArea)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. return s.gatewayIpAdminRepository.AddGatewayIp(ctx,req)
  78. }
  79. func (s *gatewayIpAdminService) EditGatewayIp(ctx context.Context,req model.Gatewayip) error {
  80. return s.gatewayIpAdminRepository.EditGatewayIp(ctx,req)
  81. }
  82. func (s *gatewayIpAdminService) DeleteGatewayIp(ctx context.Context,id int64) error {
  83. oldData, err := s.GetGatewayIpAdmin(ctx, id)
  84. if err != nil {
  85. return err
  86. }
  87. // 启动网关组IP
  88. if oldData.NodeArea != "" {
  89. err := s.sendIp(ctx,oldData.Ip,"deleteIp",oldData.NodeArea)
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. return s.gatewayIpAdminRepository.DeleteGatewayIp(ctx,id)
  95. }
  96. func (s *gatewayIpAdminService) DeleteGatewayIps(ctx context.Context, ids []int64) error {
  97. var allErrors *multierror.Error
  98. for _, id := range ids {
  99. oldData, err := s.GetGatewayIpAdmin(ctx, id)
  100. if err != nil {
  101. return err
  102. }
  103. // 启动网关组IP
  104. if oldData.NodeArea != "" {
  105. err := s.sendIp(ctx,oldData.Ip,"deleteIp",oldData.NodeArea)
  106. if err != nil {
  107. allErrors = multierror.Append(allErrors, err)
  108. }
  109. }
  110. }
  111. if allErrors != nil {
  112. return allErrors.ErrorOrNil()
  113. }
  114. return s.gatewayIpAdminRepository.DeleteGatewayIps(ctx,ids)
  115. }