Refactor: cleanup

- Introduce tracing
- Introduce cobra / viper framework
- Introduce resty client
- Seperate files in api/ and bot/
- Trim unused functions
This commit is contained in:
2025-12-12 23:51:48 +08:00
parent 344176063b
commit cb11672817
15 changed files with 575 additions and 466 deletions

View File

@@ -1,18 +1,17 @@
package api
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"errors"
"time"
"github.com/go-resty/resty/v2"
"github.com/spf13/viper"
)
var ErrRequestFailed = errors.New("request failed")
type Client struct {
baseURL string
httpClient *http.Client
token string
client *resty.Client
}
type Image struct {
@@ -31,234 +30,12 @@ type AliasesResponse struct {
Aliases []string `json:"aliases"`
}
type GenLoginURLRequest struct {
UserID string `json:"userId"`
}
func NewClient() *Client {
client := resty.New()
client.SetBaseURL(viper.GetString("api-endpoint"))
client.SetAuthToken(viper.GetString("preshared-key"))
type GenLoginURLResponse struct {
LoginURL string `json:"loginUrl"`
}
func NewClient(baseURL string) *Client {
return &Client{
baseURL: baseURL,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
client: client,
}
}
func (c *Client) SetToken(token string) {
c.token = token
}
func (c *Client) GetImages(search string, limit, page int) (*ImagesResponse, error) {
url := fmt.Sprintf("%s/api/images?search=%s&limit=%d&page=%d", c.baseURL, search, limit, page)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
var result ImagesResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) GetImage(id string) (*Image, error) {
url := fmt.Sprintf("%s/api/images/%s", c.baseURL, id)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
var image Image
if err := json.NewDecoder(resp.Body).Decode(&image); err != nil {
return nil, err
}
return &image, nil
}
func (c *Client) GetAliases() (*AliasesResponse, error) {
url := fmt.Sprintf("%s/api/aliases", c.baseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
var result AliasesResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) UploadImage(imageData []byte, aliases []string) (*Image, error) {
// TODO: Implement multipart form upload
return nil, fmt.Errorf("not implemented")
}
func (c *Client) DeleteImage(id string) error {
url := fmt.Sprintf("%s/api/images/%s", c.baseURL, id)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("API returned status %d", resp.StatusCode)
}
return nil
}
func (c *Client) AddAlias(imageID, alias string) (*Image, error) {
url := fmt.Sprintf("%s/api/images/%s/aliases", c.baseURL, imageID)
body := map[string]string{"alias": alias}
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
var image Image
if err := json.NewDecoder(resp.Body).Decode(&image); err != nil {
return nil, err
}
return &image, nil
}
func (c *Client) GetImageFile(id string) ([]byte, error) {
url := fmt.Sprintf("%s/api/images/%s/file", c.baseURL, id)
resp, err := c.httpClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}
func (c *Client) GenerateLoginURL(userID string) (string, error) {
url := fmt.Sprintf("%s/auth/gen-login-url", c.baseURL)
reqBody := GenLoginURLRequest{UserID: userID}
jsonBody, err := json.Marshal(reqBody)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
// TODO: Set preshared key authorization
req.Header.Set("Authorization", "Bearer poop")
resp, err := c.httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("API returned status %d: %s", resp.StatusCode, string(body))
}
var result GenLoginURLResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", err
}
return result.LoginURL, nil
}

33
api/getImages.go Normal file
View File

@@ -0,0 +1,33 @@
package api
import (
"net/http"
"strconv"
"github.com/Penguin-71630/meme-bot-frontend-dc/tracing"
"go.uber.org/zap"
)
func (c *Client) GetImages(
search string,
limit, page int,
) (ImagesResponse, error) {
var res ImagesResponse
resp, err := c.client.R().
SetResult(&res).
SetQueryParam("search", search).
SetQueryParam("limit", strconv.Itoa(limit)).
SetQueryParam("page", strconv.Itoa(page)).
Get("/api/images")
if err != nil || resp.StatusCode() != http.StatusOK {
tracing.Logger.Ctx(resp.Request.Context()).
Error("failed to get api images",
zap.String("search", search),
zap.Int("limit", limit),
zap.Int("page", page),
zap.Error(err))
return ImagesResponse{}, err
}
return res, nil
}

32
api/postGenLoginUrl.go Normal file
View File

@@ -0,0 +1,32 @@
package api
import (
"net/http"
"github.com/Penguin-71630/meme-bot-frontend-dc/tracing"
"go.uber.org/zap"
)
type GenLoginURLRequest struct {
UserId string `json:"userId"`
}
type GenLoginURLResponse struct {
LoginURL string `json:"loginUrl"`
}
func (c *Client) PostGenLoginURL(
userId string,
) (string, error) {
var res GenLoginURLResponse
resp, err := c.client.R().
SetBody(GenLoginURLRequest{UserId: userId}).
Post("/auth/gen-login-url")
if err != nil || resp.StatusCode() != http.StatusOK {
tracing.Logger.Ctx(resp.Request.Context()).
Error("failed to post gen-login-url",
zap.Error(err))
return "", ErrRequestFailed
}
return res.LoginURL, nil
}