chore(deps): upgrade gomod dependencies

This commit is contained in:
Fu Diwei
2024-12-23 15:05:25 +08:00
parent cda54085b9
commit d7bff599b7
6 changed files with 339 additions and 379 deletions

View File

@@ -5,14 +5,15 @@ import (
"context"
"encoding/json"
"errors"
"net/http"
"io"
"strings"
"time"
"github.com/gojek/heimdall/v7/httpclient"
xerrors "github.com/pkg/errors"
"github.com/usual2970/certimate/internal/pkg/core/deployer"
"github.com/usual2970/certimate/internal/pkg/utils/x509"
xhttp "github.com/usual2970/certimate/internal/utils/http"
)
type WebhookDeployerConfig struct {
@@ -23,8 +24,9 @@ type WebhookDeployerConfig struct {
}
type WebhookDeployer struct {
config *WebhookDeployerConfig
logger deployer.Logger
config *WebhookDeployerConfig
logger deployer.Logger
httpClient *httpclient.Client
}
var _ deployer.Deployer = (*WebhookDeployer)(nil)
@@ -42,9 +44,12 @@ func NewWithLogger(config *WebhookDeployerConfig, logger deployer.Logger) (*Webh
return nil, errors.New("logger is nil")
}
client := httpclient.NewClient(httpclient.WithHTTPTimeout(30 * time.Second))
return &WebhookDeployer{
config: config,
logger: logger,
config: config,
logger: logger,
httpClient: client,
}, nil
}
@@ -61,25 +66,28 @@ func (d *WebhookDeployer) Deploy(ctx context.Context, certPem string, privkeyPem
return nil, xerrors.Wrap(err, "failed to parse x509")
}
data := &webhookData{
reqBody, _ := json.Marshal(&webhookData{
SubjectAltNames: strings.Join(certX509.DNSNames, ","),
Certificate: certPem,
PrivateKey: privkeyPem,
Variables: d.config.Variables,
}
body, _ := json.Marshal(data)
resp, err := xhttp.Req(d.config.Url, http.MethodPost, bytes.NewReader(body), map[string]string{
"Content-Type": "application/json",
})
resp, err := d.httpClient.Post(d.config.Url, bytes.NewReader(reqBody), map[string][]string{"Content-Type": {"application/json"}})
if err != nil {
return nil, xerrors.Wrap(err, "failed to send webhook request")
}
d.logger.Logt("Webhook Response", string(resp))
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, xerrors.Wrap(err, "failed to read response body")
}
d.logger.Logt("Webhook Response", string(respBody))
return &deployer.DeployResult{
DeploymentData: map[string]any{
"responseText": string(resp),
"responseText": string(respBody),
},
}, nil
}

View File

@@ -1,71 +0,0 @@
package http
import (
"fmt"
"io"
"net/http"
"time"
"github.com/gojek/heimdall/v7/httpclient"
)
type Options struct {
Timeout time.Duration
}
type Option func(o *Options)
func WithTimeout(timeout time.Duration) Option {
return func(o *Options) {
o.Timeout = timeout
}
}
func Req(url string, method string, body io.Reader, head map[string]string, opts ...Option) ([]byte, error) {
reader, err := Req2GetReader(url, method, body, head, opts...)
if err != nil {
return nil, err
}
defer reader.Close()
return io.ReadAll(reader)
}
func Req2GetReader(url string, method string, body io.Reader, head map[string]string, opts ...Option) (io.ReadCloser, error) {
req := BuildReq(url, method, body, head)
return ToRequest(req, opts...)
}
func BuildReq(url string, method string, body io.Reader, head map[string]string) *http.Request {
// Create an http.Request instance
req, _ := http.NewRequest(method, url, body)
for k, v := range head {
req.Header.Set(k, v)
}
return req
}
func ToRequest(req *http.Request, opts ...Option) (io.ReadCloser, error) {
options := &Options{
Timeout: 30000 * time.Millisecond,
}
for _, opt := range opts {
opt(options)
}
client := httpclient.NewClient(httpclient.WithHTTPTimeout(options.Timeout))
// Call the `Do` method, which has a similar interface to the `http.Do` method
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status code is not 200: %d", res.StatusCode)
}
return res.Body, nil
}