Restructured - new game changer

This commit is contained in:
PB
2025-10-26 19:20:59 +01:00
parent 2ac68aed86
commit 65105d6982
9 changed files with 49 additions and 61 deletions

View File

@@ -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
}