feat: add cdnfly deployer

This commit is contained in:
Fu Diwei
2025-02-18 15:14:24 +08:00
parent 46f02331fd
commit 03d2f4ca32
122 changed files with 955 additions and 243 deletions

View File

@@ -30,11 +30,9 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
}
func (c *Client) sendRequest(method string, path string, params map[string]any) (*resty.Response, error) {
url := "https://cdn.api.baishan.com" + path
req := c.client.R()
req.Method = method
req.URL = url
req.URL = "https://cdn.api.baishan.com" + path
if strings.EqualFold(method, http.MethodGet) {
data := make(map[string]string)
for k, v := range params {
@@ -68,8 +66,8 @@ func (c *Client) sendRequestWithResult(method string, path string, params map[st
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("baishan api error: failed to parse response: %w", err)
} else if result.GetCode() != 0 {
return fmt.Errorf("baishan api error: %d - %s", result.GetCode(), result.GetMessage())
} else if errcode := result.GetCode(); errcode != 0 {
return fmt.Errorf("baishan api error: %d - %s", errcode, result.GetMessage())
}
return nil

View File

@@ -14,14 +14,15 @@ import (
type Client struct {
apiHost string
apiKey string
client *resty.Client
client *resty.Client
}
func NewClient(apiHost, apiKey string) *Client {
client := resty.New()
return &Client{
apiHost: apiHost,
apiHost: strings.TrimRight(apiHost, "/"),
apiKey: apiKey,
client: client,
}
@@ -50,7 +51,7 @@ func (c *Client) sendRequest(path string, params map[string]any) (*resty.Respons
params["request_time"] = timestamp
params["request_token"] = c.generateSignature(fmt.Sprintf("%d", timestamp))
url := strings.TrimRight(c.apiHost, "/") + path
url := c.apiHost + path
req := c.client.R().
SetHeader("Content-Type", "application/json").
SetBody(params)
@@ -72,7 +73,7 @@ func (c *Client) sendRequestWithResult(path string, params map[string]any, resul
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("baota api error: failed to parse response: %w", err)
} else if result.GetStatus() != nil && !*result.GetStatus() {
} else if errstatus := result.GetStatus(); errstatus != nil && !*errstatus {
if result.GetMsg() == nil {
return fmt.Errorf("baota api error: unknown error")
} else {

View File

@@ -30,11 +30,9 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
}
func (c *Client) sendRequest(method string, path string, params map[string]any) (*resty.Response, error) {
url := "https://api.cachefly.com/api/2.5" + path
req := c.client.R()
req.Method = method
req.URL = url
req.URL = "https://api.cachefly.com/api/2.5" + path
req = req.SetHeader("x-cf-authorization", "Bearer "+c.apiToken)
if strings.EqualFold(method, http.MethodGet) {
data := make(map[string]string)

59
internal/pkg/vendors/cdnfly-sdk/api.go vendored Normal file
View File

@@ -0,0 +1,59 @@
package cdnflysdk
import (
"encoding/json"
"fmt"
"net/http"
)
func (c *Client) GetSite(req *GetSiteRequest) (*GetSiteResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := GetSiteResponse{}
err := c.sendRequestWithResult(http.MethodGet, fmt.Sprintf("/v1/sites/%s", req.Id), params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) UpdateSite(req *UpdateSiteRequest) (*UpdateSiteResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := UpdateSiteResponse{}
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/v1/sites/%s", req.Id), params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := CreateCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v1/certs", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := UpdateCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/v1/certs/%s", req.Id), params, &result)
if err != nil {
return nil, err
}
return &result, nil
}

View File

@@ -0,0 +1,79 @@
package cdnflysdk
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
apiHost string
apiKey string
apiSecret string
client *resty.Client
}
func NewClient(apiHost, apiKey, apiSecret string) *Client {
client := resty.New()
return &Client{
apiHost: strings.TrimRight(apiHost, "/"),
apiKey: apiKey,
apiSecret: apiSecret,
client: client,
}
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *Client) sendRequest(method string, path string, params map[string]any) (*resty.Response, error) {
req := c.client.R()
req.Method = method
req.URL = c.apiHost + path
req = req.
SetHeader("api-key", c.apiKey).
SetHeader("api-secret", c.apiSecret)
if strings.EqualFold(method, http.MethodGet) {
data := make(map[string]string)
for k, v := range params {
data[k] = fmt.Sprintf("%v", v)
}
req = req.SetQueryParams(data)
} else {
req = req.
SetHeader("Content-Type", "application/json").
SetBody(params)
}
resp, err := req.Send()
if err != nil {
return nil, fmt.Errorf("cdnfly api error: failed to send request: %w", err)
} else if resp.IsError() {
return nil, fmt.Errorf("cdnfly api error: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
}
return resp, nil
}
func (c *Client) sendRequestWithResult(method string, path string, params map[string]any, result BaseResponse) error {
resp, err := c.sendRequest(method, path, params)
if err != nil {
return err
}
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("cdnfly api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode != "" && errcode != "0" {
return fmt.Errorf("cdnfly api error: %s - %s", errcode, result.GetMessage())
}
return nil
}

View File

@@ -0,0 +1,70 @@
package cdnflysdk
type BaseResponse interface {
GetCode() string
GetMessage() string
}
type baseResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
}
func (r *baseResponse) GetCode() string {
return r.Code
}
func (r *baseResponse) GetMessage() string {
return r.Message
}
type GetSiteRequest struct {
Id string `json:"-"`
}
type GetSiteResponse struct {
baseResponse
Data *struct {
Id string `json:"id"`
Name string `json:"name"`
Domain string `json:"domain"`
HttpsListen string `json:"https_listen"`
} `json:"data"`
}
type UpdateSiteRequest struct {
Id string `json:"-"`
HttpsListen *string `json:"https_listen,omitempty"`
Enable *bool `json:"enable,omitempty"`
}
type UpdateSiteResponse struct {
baseResponse
}
type CreateCertificateRequest struct {
Name string `json:"name"`
Description *string `json:"des,omitempty"`
Type string `json:"type"`
Cert string `json:"cert"`
Key string `json:"key"`
}
type CreateCertificateResponse struct {
baseResponse
Data string `json:"data"`
}
type UpdateCertificateRequest struct {
Id string `json:"-"`
Name *string `json:"name,omitempty"`
Description *string `json:"des,omitempty"`
Type *string `json:"type,omitempty"`
Cert *string `json:"cert,omitempty"`
Key *string `json:"key,omitempty"`
Enable *bool `json:"enable,omitempty"`
}
type UpdateCertificateResponse struct {
baseResponse
}

View File

@@ -15,6 +15,7 @@ import (
type Client struct {
appId string
appKey string
client *resty.Client
}
@@ -92,8 +93,8 @@ func (c *Client) sendRequestWithResult(path string, params map[string]any, resul
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("gname api error: failed to parse response: %w", err)
} else if result.GetCode() != 1 {
return fmt.Errorf("gname api error: %d - %s", result.GetCode(), result.GetMsg())
} else if errcode := result.GetCode(); errcode != 1 {
return fmt.Errorf("gname api error: %d - %s", errcode, result.GetMsg())
}
return nil

View File

@@ -12,14 +12,15 @@ import (
type Client struct {
apiHost string
apiToken string
client *resty.Client
client *resty.Client
}
func NewClient(apiHost, apiToken string) *Client {
client := resty.New()
return &Client{
apiHost: apiHost,
apiHost: strings.TrimRight(apiHost, "/"),
apiToken: apiToken,
client: client,
}
@@ -35,7 +36,7 @@ func (c *Client) sendRequest(path string, params map[string]any) (*resty.Respons
params = make(map[string]any)
}
url := strings.TrimRight(c.apiHost, "/") + path
url := c.apiHost + path
req := c.client.R().
SetHeader("Content-Type", "application/json").
SetHeader("X-SLCE-API-TOKEN", c.apiToken).
@@ -58,11 +59,11 @@ func (c *Client) sendRequestWithResult(path string, params map[string]any, resul
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("safeline api error: failed to parse response: %w", err)
} else if result.GetErrCode() != nil && *result.GetErrCode() != "" {
} else if errcode := result.GetErrCode(); errcode != nil && *errcode != "" {
if result.GetErrMsg() == nil {
return fmt.Errorf("safeline api error: %s", *result.GetErrCode())
return fmt.Errorf("safeline api error: %s", *errcode)
} else {
return fmt.Errorf("safeline api error: %s - %s", *result.GetErrCode(), *result.GetErrMsg())
return fmt.Errorf("safeline api error: %s - %s", *errcode, *result.GetErrMsg())
}
}