添加服务相关方法&增加日志

This commit is contained in:
2025-09-15 15:53:11 +08:00
parent 917ae30a86
commit ed3bd2bd80
6 changed files with 83 additions and 12 deletions

View File

@@ -13,6 +13,7 @@ import (
"os"
"time"
"go.uber.org/zap"
"golang.org/x/net/http2"
)
@@ -24,6 +25,7 @@ type Request struct {
port uint16
rootCertPath string
client *http.Client
logger *zap.Logger
}
// 发送请求
@@ -58,11 +60,19 @@ func (r *Request) Send(methodName string, body any, responseBody any) error {
return fmt.Errorf("response body json unmarshal fail: %v", err)
}
if r.logger != nil {
r.logger.Debug("request: ",
zap.String("method", methodName),
zap.String("request", string(bodyByte)),
zap.String("response", string(responseBodyByte)),
)
}
return nil
}
// 初始化
func Initiate(host string, port uint16, rootCertPath string) error {
func Initiate(host string, port uint16, rootCertPath string, logger *zap.Logger) error {
rootCert, err := os.ReadFile(rootCertPath)
if err != nil {
return fmt.Errorf("cert file read fail")
@@ -70,6 +80,7 @@ func Initiate(host string, port uint16, rootCertPath string) error {
defaultRequest.host = host
defaultRequest.port = port
defaultRequest.rootCertPath = rootCertPath
defaultRequest.logger = logger
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(rootCert)