Restructured - new game changer
This commit is contained in:
@@ -2,21 +2,16 @@ package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type HttpClient struct {
|
||||
ua string
|
||||
redis *redis.Client
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func NewHttpClient(ua string, redis *redis.Client) *HttpClient {
|
||||
return &HttpClient{ua, redis}
|
||||
func NewHttpClient() *HttpClient {
|
||||
return &HttpClient{userAgent: "internal-http-client"}
|
||||
}
|
||||
|
||||
func (c *HttpClient) SendGet(api, url string, data, out any) error {
|
||||
@@ -50,7 +45,7 @@ func (c *HttpClient) SendPost(api, url string, data, out any) (any, error) {
|
||||
}
|
||||
|
||||
func (c *HttpClient) sendRequest(api, url, method string, data any) (*http.Response, error) {
|
||||
apiUrl := c.getApiUrl(api) + url
|
||||
apiUrl := api + url // FIXME
|
||||
client := &http.Client{}
|
||||
|
||||
json, err := json.Marshal(&data)
|
||||
@@ -73,23 +68,3 @@ func (c *HttpClient) sendRequest(api, url, method string, data any) (*http.Respo
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *HttpClient) getApiUrl(api string) string {
|
||||
ctx, key, apiAddr := context.Background(), "internal__"+api+"__ips", api
|
||||
// FIXME: key name ^^
|
||||
cmd := c.redis.LLen(ctx, key)
|
||||
if cmd.Err() == nil {
|
||||
len := int(cmd.Val())
|
||||
if len == 0 {
|
||||
apiAddr = c.redis.LIndex(ctx, key, 0).Val()
|
||||
} else {
|
||||
apiAddr = c.redis.LIndex(ctx, key, int64(rand.Intn(len-0)+0)).Val()
|
||||
}
|
||||
}
|
||||
|
||||
if apiAddr == "" {
|
||||
apiAddr = api // default api run on 80 int port
|
||||
}
|
||||
|
||||
return "https://" + apiAddr
|
||||
}
|
||||
Reference in New Issue
Block a user