61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/songquanpeng/one-api/common/config"
|
|
"github.com/songquanpeng/one-api/common/logger"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
var HTTPClient *http.Client
|
|
var ImpatientHTTPClient *http.Client
|
|
var UserContentRequestHTTPClient *http.Client
|
|
|
|
func Init() {
|
|
if config.UserContentRequestProxy != "" {
|
|
logger.SysLog(fmt.Sprintf("using %s as proxy to fetch user content", config.UserContentRequestProxy))
|
|
proxyURL, err := url.Parse(config.UserContentRequestProxy)
|
|
if err != nil {
|
|
logger.FatalLog(fmt.Sprintf("USER_CONTENT_REQUEST_PROXY set but invalid: %s", config.UserContentRequestProxy))
|
|
}
|
|
transport := &http.Transport{
|
|
Proxy: http.ProxyURL(proxyURL),
|
|
}
|
|
UserContentRequestHTTPClient = &http.Client{
|
|
Transport: transport,
|
|
Timeout: time.Second * time.Duration(config.UserContentRequestTimeout),
|
|
}
|
|
} else {
|
|
UserContentRequestHTTPClient = &http.Client{}
|
|
}
|
|
var transport http.RoundTripper
|
|
if config.RelayProxy != "" {
|
|
logger.SysLog(fmt.Sprintf("using %s as api relay proxy", config.RelayProxy))
|
|
proxyURL, err := url.Parse(config.RelayProxy)
|
|
if err != nil {
|
|
logger.FatalLog(fmt.Sprintf("USER_CONTENT_REQUEST_PROXY set but invalid: %s", config.UserContentRequestProxy))
|
|
}
|
|
transport = &http.Transport{
|
|
Proxy: http.ProxyURL(proxyURL),
|
|
}
|
|
}
|
|
|
|
if config.RelayTimeout == 0 {
|
|
HTTPClient = &http.Client{
|
|
Transport: transport,
|
|
}
|
|
} else {
|
|
HTTPClient = &http.Client{
|
|
Timeout: time.Duration(config.RelayTimeout) * time.Second,
|
|
Transport: transport,
|
|
}
|
|
}
|
|
|
|
ImpatientHTTPClient = &http.Client{
|
|
Timeout: 5 * time.Second,
|
|
Transport: transport,
|
|
}
|
|
}
|