mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	实现在服务中使用分组
This commit is contained in:
		@@ -85,7 +85,7 @@ func (this *ServerDAO) FindEnabledServerType(serverId int64) (string, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 创建服务
 | 
			
		||||
func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serverconfigs.ServerType, name string, description string, serverNamesJSON string, httpJSON string, httpsJSON string, tcpJSON string, tlsJSON string, unixJSON string, udpJSON string, webId int64, reverseProxyJSON []byte, clusterId int64, includeNodesJSON string, excludeNodesJSON string) (serverId int64, err error) {
 | 
			
		||||
func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serverconfigs.ServerType, name string, description string, serverNamesJSON string, httpJSON string, httpsJSON string, tcpJSON string, tlsJSON string, unixJSON string, udpJSON string, webId int64, reverseProxyJSON []byte, clusterId int64, includeNodesJSON string, excludeNodesJSON string, groupIds []int64) (serverId int64, err error) {
 | 
			
		||||
	op := NewServerOperator()
 | 
			
		||||
	op.UserId = userId
 | 
			
		||||
	op.AdminId = adminId
 | 
			
		||||
@@ -127,7 +127,16 @@ func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serv
 | 
			
		||||
		op.ExcludeNodes = excludeNodesJSON
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(groupIds) == 0 {
 | 
			
		||||
		op.GroupIds = "[]"
 | 
			
		||||
	} else {
 | 
			
		||||
		groupIdsJSON, err := json.Marshal(groupIds)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return 0, err
 | 
			
		||||
		}
 | 
			
		||||
		op.GroupIds = groupIdsJSON
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.Version = 1
 | 
			
		||||
	op.IsOn = 1
 | 
			
		||||
	op.State = ServerStateEnabled
 | 
			
		||||
@@ -153,7 +162,7 @@ func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serv
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 修改服务基本信息
 | 
			
		||||
func (this *ServerDAO) UpdateServerBasic(serverId int64, name string, description string, clusterId int64, isOn bool) error {
 | 
			
		||||
func (this *ServerDAO) UpdateServerBasic(serverId int64, name string, description string, clusterId int64, isOn bool, groupIds []int64) error {
 | 
			
		||||
	if serverId <= 0 {
 | 
			
		||||
		return errors.New("serverId should not be smaller than 0")
 | 
			
		||||
	}
 | 
			
		||||
@@ -163,6 +172,17 @@ func (this *ServerDAO) UpdateServerBasic(serverId int64, name string, descriptio
 | 
			
		||||
	op.Description = description
 | 
			
		||||
	op.ClusterId = clusterId
 | 
			
		||||
	op.IsOn = isOn
 | 
			
		||||
 | 
			
		||||
	if len(groupIds) == 0 {
 | 
			
		||||
		op.GroupIds = "[]"
 | 
			
		||||
	} else {
 | 
			
		||||
		groupIdsJSON, err := json.Marshal(groupIds)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		op.GroupIds = groupIdsJSON
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err := this.Save(op)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
 
 | 
			
		||||
@@ -20,7 +20,7 @@ func (this *ServerService) CreateServer(ctx context.Context, req *pb.CreateServe
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	serverId, err := models.SharedServerDAO.CreateServer(req.AdminId, req.UserId, req.Type, req.Name, req.Description, string(req.ServerNamesJON), string(req.HttpJSON), string(req.HttpsJSON), string(req.TcpJSON), string(req.TlsJSON), string(req.UnixJSON), string(req.UdpJSON), req.WebId, req.ReverseProxyJSON, req.ClusterId, string(req.IncludeNodesJSON), string(req.ExcludeNodesJSON))
 | 
			
		||||
	serverId, err := models.SharedServerDAO.CreateServer(req.AdminId, req.UserId, req.Type, req.Name, req.Description, string(req.ServerNamesJON), string(req.HttpJSON), string(req.HttpsJSON), string(req.TcpJSON), string(req.TlsJSON), string(req.UnixJSON), string(req.UdpJSON), req.WebId, req.ReverseProxyJSON, req.ClusterId, string(req.IncludeNodesJSON), string(req.ExcludeNodesJSON), req.GroupIds)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -55,7 +55,7 @@ func (this *ServerService) UpdateServerBasic(ctx context.Context, req *pb.Update
 | 
			
		||||
		return nil, errors.New("can not find server")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedServerDAO.UpdateServerBasic(req.ServerId, req.Name, req.Description, req.ClusterId, req.IsOn)
 | 
			
		||||
	err = models.SharedServerDAO.UpdateServerBasic(req.ServerId, req.Name, req.Description, req.ClusterId, req.IsOn, req.GroupIds)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -379,6 +379,30 @@ func (this *ServerService) ListEnabledServers(ctx context.Context, req *pb.ListE
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 分组信息
 | 
			
		||||
		pbGroups := []*pb.ServerGroup{}
 | 
			
		||||
		if len(server.GroupIds) > 0 {
 | 
			
		||||
			groupIds := []int64{}
 | 
			
		||||
			err = json.Unmarshal([]byte(server.GroupIds), &groupIds)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
			for _, groupId := range groupIds {
 | 
			
		||||
				group, err := models.SharedServerGroupDAO.FindEnabledServerGroup(groupId)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return nil, err
 | 
			
		||||
				}
 | 
			
		||||
				if group == nil {
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				pbGroups = append(pbGroups, &pb.ServerGroup{
 | 
			
		||||
					Id:   int64(group.Id),
 | 
			
		||||
					Name: group.Name,
 | 
			
		||||
				})
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		result = append(result, &pb.Server{
 | 
			
		||||
			Id:           int64(server.Id),
 | 
			
		||||
			IsOn:         server.IsOn == 1,
 | 
			
		||||
@@ -399,6 +423,7 @@ func (this *ServerService) ListEnabledServers(ctx context.Context, req *pb.ListE
 | 
			
		||||
				Id:   int64(server.ClusterId),
 | 
			
		||||
				Name: clusterName,
 | 
			
		||||
			},
 | 
			
		||||
			Groups: pbGroups,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -454,11 +479,35 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn
 | 
			
		||||
		return &pb.FindEnabledServerResponse{}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 集群信息
 | 
			
		||||
	clusterName, err := models.SharedNodeClusterDAO.FindNodeClusterName(int64(server.ClusterId))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 分组信息
 | 
			
		||||
	pbGroups := []*pb.ServerGroup{}
 | 
			
		||||
	if len(server.GroupIds) > 0 {
 | 
			
		||||
		groupIds := []int64{}
 | 
			
		||||
		err = json.Unmarshal([]byte(server.GroupIds), &groupIds)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		for _, groupId := range groupIds {
 | 
			
		||||
			group, err := models.SharedServerGroupDAO.FindEnabledServerGroup(groupId)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
			if group == nil {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			pbGroups = append(pbGroups, &pb.ServerGroup{
 | 
			
		||||
				Id:   int64(group.Id),
 | 
			
		||||
				Name: group.Name,
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindEnabledServerResponse{Server: &pb.Server{
 | 
			
		||||
		Id:               int64(server.Id),
 | 
			
		||||
		IsOn:             server.IsOn == 1,
 | 
			
		||||
@@ -483,6 +532,7 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn
 | 
			
		||||
			Id:   int64(server.ClusterId),
 | 
			
		||||
			Name: clusterName,
 | 
			
		||||
		},
 | 
			
		||||
		Groups: pbGroups,
 | 
			
		||||
	}}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user