feat: support configuration file (#117)

* ♻️ refactor: move file directory

* ♻️ refactor: move file directory

* ♻️ refactor: support multiple config methods

* 🔥 del: remove unused code

* 💩 refactor: Refactor channel management and synchronization

* 💄 improve: add channel website

*  feat: allow recording 0 consumption
This commit is contained in:
Buer
2024-03-20 14:12:47 +08:00
committed by GitHub
parent 0409de0ea9
commit 71171c63f5
50 changed files with 581 additions and 481 deletions

View File

@@ -39,7 +39,7 @@ func proxyFunc(req *http.Request) (*url.URL, error) {
func socks5ProxyFunc(ctx context.Context, network, addr string) (net.Conn, error) {
// 设置TCP超时
dialer := &net.Dialer{
Timeout: time.Duration(common.ConnectTimeout) * time.Second,
Timeout: time.Duration(common.GetOrDefault("CONNECT_TIMEOUT", 5)) * time.Second,
KeepAlive: 30 * time.Second,
}
@@ -64,7 +64,7 @@ func socks5ProxyFunc(ctx context.Context, network, addr string) (net.Conn, error
var HTTPClient *http.Client
func init() {
func InitHttpClient() {
trans := &http.Transport{
DialContext: socks5ProxyFunc,
Proxy: proxyFunc,
@@ -74,7 +74,8 @@ func init() {
Transport: trans,
}
if common.RelayTimeout != 0 {
HTTPClient.Timeout = time.Duration(common.RelayTimeout) * time.Second
relayTimeout := common.GetOrDefault("RELAY_TIMEOUT", 600)
if relayTimeout != 0 {
HTTPClient.Timeout = time.Duration(relayTimeout) * time.Second
}
}

View File

@@ -0,0 +1,15 @@
package requester
import (
"encoding/json"
)
type Marshaller interface {
Marshal(value any) ([]byte, error)
}
type JSONMarshaller struct{}
func (jm *JSONMarshaller) Marshal(value any) ([]byte, error) {
return json.Marshal(value)
}

View File

@@ -5,7 +5,6 @@ import (
"context"
"io"
"net/http"
"one-api/common"
)
type RequestBuilder interface {
@@ -13,12 +12,12 @@ type RequestBuilder interface {
}
type HTTPRequestBuilder struct {
marshaller common.Marshaller
marshaller Marshaller
}
func NewRequestBuilder() *HTTPRequestBuilder {
return &HTTPRequestBuilder{
marshaller: &common.JSONMarshaller{},
marshaller: &JSONMarshaller{},
}
}