feat: add baishan cdn deployer
This commit is contained in:
45
internal/pkg/vendors/baishan-sdk/api.go
vendored
Normal file
45
internal/pkg/vendors/baishan-sdk/api.go
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
package baishansdk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := CreateCertificateResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/certificate", params, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetDomainConfig(req *GetDomainConfigRequest) (*GetDomainConfigResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := GetDomainConfigResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodGet, "/v2/domain/config", params, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetDomainConfig(req *SetDomainConfigRequest) (*SetDomainConfigResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := SetDomainConfigResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/config", params, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
84
internal/pkg/vendors/baishan-sdk/client.go
vendored
Normal file
84
internal/pkg/vendors/baishan-sdk/client.go
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
package baishansdk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
apiToken string
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(apiToken string) *Client {
|
||||
client := resty.New()
|
||||
|
||||
return &Client{
|
||||
apiToken: apiToken,
|
||||
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) {
|
||||
url := "https://cdn.api.baishan.com" + path
|
||||
|
||||
req := c.client.R()
|
||||
if method == http.MethodGet {
|
||||
data := make(map[string]string)
|
||||
for k, v := range params {
|
||||
data[k] = fmt.Sprintf("%v", v)
|
||||
}
|
||||
req = req.
|
||||
SetQueryParams(data).
|
||||
SetQueryParam("token", c.apiToken)
|
||||
} else if method == http.MethodPost {
|
||||
req = req.
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetQueryParam("token", c.apiToken).
|
||||
SetBody(params)
|
||||
}
|
||||
|
||||
var resp *resty.Response
|
||||
var err error
|
||||
if method == http.MethodGet {
|
||||
resp, err = req.Get(url)
|
||||
} else if method == http.MethodPost {
|
||||
resp, err = req.Post(url)
|
||||
} else {
|
||||
return nil, fmt.Errorf("baishan: unsupported method: %s", method)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("baishan: failed to send request: %w", err)
|
||||
} else if resp.IsError() {
|
||||
return nil, fmt.Errorf("baishan: 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("baishan: failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
if result.GetCode() != 0 {
|
||||
return fmt.Errorf("baishan api error: %d, %s", result.GetCode(), result.GetMessage())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
77
internal/pkg/vendors/baishan-sdk/models.go
vendored
Normal file
77
internal/pkg/vendors/baishan-sdk/models.go
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
package baishansdk
|
||||
|
||||
type BaseResponse interface {
|
||||
GetCode() int
|
||||
GetMessage() string
|
||||
}
|
||||
|
||||
type baseResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetCode() int {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetMessage() string {
|
||||
return r.Message
|
||||
}
|
||||
|
||||
type CreateCertificateRequest struct {
|
||||
Certificate string `json:"certificate"`
|
||||
Key string `json:"key"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type CreateCertificateResponse struct {
|
||||
baseResponse
|
||||
Data *DomainCertificate `json:"data"`
|
||||
}
|
||||
|
||||
type GetDomainConfigRequest struct {
|
||||
Domains string `json:"domains"`
|
||||
Config string `json:"config"`
|
||||
}
|
||||
|
||||
type GetDomainConfigResponse struct {
|
||||
baseResponse
|
||||
Data []*GetDomainConfigResponseData `json:"data"`
|
||||
}
|
||||
|
||||
type GetDomainConfigResponseData struct {
|
||||
Domain string `json:"domain"`
|
||||
Config *DomainConfig `json:"config"`
|
||||
}
|
||||
|
||||
type SetDomainConfigRequest struct {
|
||||
Domains string `json:"domains"`
|
||||
Config *DomainConfig `json:"config"`
|
||||
}
|
||||
|
||||
type SetDomainConfigResponse struct {
|
||||
baseResponse
|
||||
Data *SetDomainConfigResponseData `json:"data"`
|
||||
}
|
||||
|
||||
type SetDomainConfigResponseData struct {
|
||||
Config *DomainConfig `json:"config"`
|
||||
}
|
||||
|
||||
type DomainCertificate struct {
|
||||
CertId int64 `json:"cert_id"`
|
||||
Name string `json:"name"`
|
||||
CertStartTime string `json:"cert_start_time"`
|
||||
CertExpireTime string `json:"cert_expire_time"`
|
||||
}
|
||||
|
||||
type DomainConfig struct {
|
||||
Https *DomainConfigHttps `json:"https"`
|
||||
}
|
||||
|
||||
type DomainConfigHttps struct {
|
||||
CertId int64 `json:"cert_id"`
|
||||
ForceHttps *string `json:"force_https,omitempty"`
|
||||
EnableHttp2 *string `json:"http2,omitempty"`
|
||||
EnableOcsp *string `json:"ocsp,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user