feat: group api

This commit is contained in:
tbphp
2025-07-03 16:29:12 +08:00
parent a94412353a
commit 80662af9de
8 changed files with 288 additions and 156 deletions

View File

@@ -3,16 +3,40 @@ package channel
import (
"fmt"
"gpt-load/internal/models"
"net/http"
"net/url"
)
// GetChannel returns a channel proxy based on the group's channel type.
func GetChannel(group *models.Group) (ChannelProxy, error) {
switch group.ChannelType {
case "openai":
return NewOpenAIChannel(group)
return NewOpenAIChannel(group.Upstreams)
case "gemini":
return NewGeminiChannel(group)
return NewGeminiChannel(group.Upstreams)
default:
return nil, fmt.Errorf("unsupported channel type: %s", group.ChannelType)
}
}
}
// newBaseChannelWithUpstreams is a helper function to create and configure a BaseChannel.
func newBaseChannelWithUpstreams(name string, upstreams []string) (BaseChannel, error) {
if len(upstreams) == 0 {
return BaseChannel{}, fmt.Errorf("at least one upstream is required for %s channel", name)
}
var upstreamURLs []*url.URL
for _, us := range upstreams {
u, err := url.Parse(us)
if err != nil {
return BaseChannel{}, fmt.Errorf("failed to parse upstream url '%s' for %s channel: %w", us, name, err)
}
upstreamURLs = append(upstreamURLs, u)
}
return BaseChannel{
Name: name,
Upstreams: upstreamURLs,
HTTPClient: &http.Client{},
}, nil
}