|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + app_errors "gpt-load/internal/errors" |
| 7 | + "gpt-load/internal/models" |
| 8 | + "gpt-load/internal/response" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | +) |
| 13 | + |
| 14 | +// IntegrationGroupInfo represents group info for integration response |
| 15 | +type IntegrationGroupInfo struct { |
| 16 | + Name string `json:"name"` |
| 17 | + DisplayName string `json:"display_name"` |
| 18 | + ChannelType string `json:"channel_type"` |
| 19 | + Path string `json:"path"` |
| 20 | +} |
| 21 | + |
| 22 | +// IntegrationInfoResponse represents the integration info response |
| 23 | +type IntegrationInfoResponse struct { |
| 24 | + Code int `json:"code"` |
| 25 | + Message string `json:"message"` |
| 26 | + Data []IntegrationGroupInfo `json:"data"` |
| 27 | +} |
| 28 | + |
| 29 | +// GetIntegrationInfo handles the integration info request |
| 30 | +func (s *Server) GetIntegrationInfo(c *gin.Context) { |
| 31 | + key := c.Query("key") |
| 32 | + if key == "" { |
| 33 | + response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Proxy key is required")) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + path := c.Request.URL.Path |
| 38 | + isGroupSpecific := strings.HasPrefix(path, "/proxy/") |
| 39 | + |
| 40 | + var groupsToCheck []*models.Group |
| 41 | + |
| 42 | + if isGroupSpecific { |
| 43 | + parts := strings.Split(strings.TrimPrefix(path, "/proxy/"), "/") |
| 44 | + if len(parts) == 0 || parts[0] == "" { |
| 45 | + response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Invalid group path")) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + groupName := parts[0] |
| 50 | + |
| 51 | + // Get group from GroupManager cache (already has ProxyKeysMap parsed) |
| 52 | + group, err := s.GroupManager.GetGroupByName(groupName) |
| 53 | + if err != nil { |
| 54 | + response.Error(c, app_errors.NewAPIError(app_errors.ErrResourceNotFound, "Group not found")) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + groupsToCheck = []*models.Group{group} |
| 59 | + } else { |
| 60 | + // Get all groups |
| 61 | + groups, err := s.GroupService.ListGroups(c.Request.Context()) |
| 62 | + if err != nil { |
| 63 | + response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, "Internal server error")) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + // Convert to pointer slice and load from cache to get ProxyKeysMap |
| 68 | + for i := range groups { |
| 69 | + cachedGroup, err := s.GroupManager.GetGroupByName(groups[i].Name) |
| 70 | + if err != nil { |
| 71 | + logrus.Warnf("Failed to get group %s from cache: %v", groups[i].Name, err) |
| 72 | + continue |
| 73 | + } |
| 74 | + groupsToCheck = append(groupsToCheck, cachedGroup) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + var result []IntegrationGroupInfo |
| 79 | + for _, group := range groupsToCheck { |
| 80 | + if hasProxyKeyPermission(group, key) { |
| 81 | + channelType := getEffectiveChannelType(group) |
| 82 | + path := buildPath(isGroupSpecific, group.Name, channelType, group.ValidationEndpoint) |
| 83 | + |
| 84 | + result = append(result, IntegrationGroupInfo{ |
| 85 | + Name: group.Name, |
| 86 | + DisplayName: group.DisplayName, |
| 87 | + ChannelType: channelType, |
| 88 | + Path: path, |
| 89 | + }) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if len(result) == 0 { |
| 94 | + response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Invalid or unauthorized proxy key")) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + response.Success(c, result) |
| 99 | +} |
| 100 | + |
| 101 | +// getEffectiveChannelType returns the effective channel type |
| 102 | +func getEffectiveChannelType(group *models.Group) string { |
| 103 | + if group.ChannelType != "openai" { |
| 104 | + return group.ChannelType |
| 105 | + } |
| 106 | + |
| 107 | + if group.ValidationEndpoint == "" { |
| 108 | + return "openai" |
| 109 | + } |
| 110 | + |
| 111 | + defaultEndpoint := "/v1/chat/completions" |
| 112 | + |
| 113 | + if group.ValidationEndpoint == defaultEndpoint { |
| 114 | + return "openai" |
| 115 | + } |
| 116 | + |
| 117 | + return "custom" |
| 118 | +} |
| 119 | + |
| 120 | +// hasProxyKeyPermission checks if the key has permission to access the group |
| 121 | +func hasProxyKeyPermission(group *models.Group, key string) bool { |
| 122 | + _, exists1 := group.ProxyKeysMap[key] |
| 123 | + _, exists2 := group.EffectiveConfig.ProxyKeysMap[key] |
| 124 | + return exists1 || exists2 |
| 125 | +} |
| 126 | + |
| 127 | +// buildPath returns the appropriate path based on request type and channel type |
| 128 | +func buildPath(isGroupSpecific bool, groupName string, channelType string, validationEndpoint string) string { |
| 129 | + if channelType == "custom" { |
| 130 | + if isGroupSpecific { |
| 131 | + return validationEndpoint |
| 132 | + } |
| 133 | + return "/proxy/" + groupName + validationEndpoint |
| 134 | + } |
| 135 | + |
| 136 | + if isGroupSpecific { |
| 137 | + return "" |
| 138 | + } |
| 139 | + return "/proxy/" + groupName |
| 140 | +} |
0 commit comments