f
This commit is contained in:
@@ -412,11 +412,8 @@ func NewContainer() *Container {
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
// AlicloudService - 阿里云服务
|
// AlicloudService - 阿里云服务
|
||||||
func(cfg *config.Config) *alicloud.AlicloudService {
|
func(cfg *config.Config) (*alicloud.AlicloudService, error) {
|
||||||
return alicloud.NewAlicloudService(
|
return alicloud.NewAlicloudServiceWithConfig(cfg)
|
||||||
cfg.Alicloud.Host,
|
|
||||||
cfg.Alicloud.AppCode,
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
sharedhttp.NewGinRouter,
|
sharedhttp.NewGinRouter,
|
||||||
ipgeo.NewLocator,
|
ipgeo.NewLocator,
|
||||||
|
|||||||
49
internal/infrastructure/external/alicloud/alicloud_factory.go
vendored
Normal file
49
internal/infrastructure/external/alicloud/alicloud_factory.go
vendored
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package alicloud
|
||||||
|
|
||||||
|
import (
|
||||||
|
"tyapi-server/internal/config"
|
||||||
|
"tyapi-server/internal/shared/external_logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewAlicloudServiceWithConfig 使用配置创建阿里云服务,并启用外部服务调用日志
|
||||||
|
func NewAlicloudServiceWithConfig(cfg *config.Config) (*AlicloudService, error) {
|
||||||
|
loggingConfig := external_logger.ExternalServiceLoggingConfig{
|
||||||
|
Enabled: true,
|
||||||
|
LogDir: "./logs/external_services",
|
||||||
|
ServiceName: "alicloud",
|
||||||
|
UseDaily: false,
|
||||||
|
EnableLevelSeparation: true,
|
||||||
|
LevelConfigs: map[string]external_logger.ExternalServiceLevelFileConfig{
|
||||||
|
"info": {
|
||||||
|
MaxSize: 100,
|
||||||
|
MaxBackups: 3,
|
||||||
|
MaxAge: 28,
|
||||||
|
Compress: true,
|
||||||
|
},
|
||||||
|
"error": {
|
||||||
|
MaxSize: 100,
|
||||||
|
MaxBackups: 3,
|
||||||
|
MaxAge: 28,
|
||||||
|
Compress: true,
|
||||||
|
},
|
||||||
|
"warn": {
|
||||||
|
MaxSize: 100,
|
||||||
|
MaxBackups: 3,
|
||||||
|
MaxAge: 28,
|
||||||
|
Compress: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
logger, err := external_logger.NewExternalServiceLogger(loggingConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewAlicloudService(
|
||||||
|
cfg.Alicloud.Host,
|
||||||
|
cfg.Alicloud.AppCode,
|
||||||
|
logger,
|
||||||
|
), nil
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package alicloud
|
package alicloud
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -8,6 +9,8 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"tyapi-server/internal/shared/external_logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -24,25 +27,47 @@ type AlicloudConfig struct {
|
|||||||
// AlicloudService 阿里云服务
|
// AlicloudService 阿里云服务
|
||||||
type AlicloudService struct {
|
type AlicloudService struct {
|
||||||
config AlicloudConfig
|
config AlicloudConfig
|
||||||
|
logger *external_logger.ExternalServiceLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAlicloudService 创建阿里云服务实例
|
// NewAlicloudService 创建阿里云服务实例
|
||||||
func NewAlicloudService(host, appCode string) *AlicloudService {
|
func NewAlicloudService(host, appCode string, logger ...*external_logger.ExternalServiceLogger) *AlicloudService {
|
||||||
|
var serviceLogger *external_logger.ExternalServiceLogger
|
||||||
|
if len(logger) > 0 {
|
||||||
|
serviceLogger = logger[0]
|
||||||
|
}
|
||||||
return &AlicloudService{
|
return &AlicloudService{
|
||||||
config: AlicloudConfig{
|
config: AlicloudConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
AppCode: appCode,
|
AppCode: appCode,
|
||||||
},
|
},
|
||||||
|
logger: serviceLogger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generateRequestID 生成请求ID
|
||||||
|
func (a *AlicloudService) generateRequestID() string {
|
||||||
|
timestamp := time.Now().UnixNano()
|
||||||
|
hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, a.config.Host)))
|
||||||
|
return fmt.Sprintf("alicloud_%x", hash[:8])
|
||||||
|
}
|
||||||
|
|
||||||
// CallAPI 调用阿里云API的通用方法
|
// CallAPI 调用阿里云API的通用方法
|
||||||
// path: API路径(如 "api-mall/api/id_card/check")
|
// path: API路径(如 "api-mall/api/id_card/check")
|
||||||
// params: 请求参数
|
// params: 请求参数
|
||||||
func (a *AlicloudService) CallAPI(path string, params map[string]interface{}) (respBytes []byte, err error) {
|
func (a *AlicloudService) CallAPI(path string, params map[string]interface{}) (respBytes []byte, err error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
requestID := a.generateRequestID()
|
||||||
|
transactionID := ""
|
||||||
|
|
||||||
// 构建请求URL
|
// 构建请求URL
|
||||||
reqURL := a.config.Host + "/" + path
|
reqURL := a.config.Host + "/" + path
|
||||||
|
|
||||||
|
// 记录请求日志
|
||||||
|
if a.logger != nil {
|
||||||
|
a.logger.LogRequest(requestID, transactionID, path, reqURL)
|
||||||
|
}
|
||||||
|
|
||||||
// 构建请求参数
|
// 构建请求参数
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
for key, value := range params {
|
for key, value := range params {
|
||||||
@@ -52,6 +77,9 @@ func (a *AlicloudService) CallAPI(path string, params map[string]interface{}) (r
|
|||||||
// 创建HTTP请求
|
// 创建HTTP请求
|
||||||
req, err := http.NewRequest("POST", reqURL, strings.NewReader(formData.Encode()))
|
req, err := http.NewRequest("POST", reqURL, strings.NewReader(formData.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if a.logger != nil {
|
||||||
|
a.logger.LogError(requestID, transactionID, path, errors.Join(ErrSystem, err), params)
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +106,14 @@ func (a *AlicloudService) CallAPI(path string, params map[string]interface{}) (r
|
|||||||
}
|
}
|
||||||
|
|
||||||
if isTimeout {
|
if isTimeout {
|
||||||
|
if a.logger != nil {
|
||||||
|
a.logger.LogError(requestID, transactionID, path, errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %s", err.Error())), params)
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("%w: API请求超时: %s", ErrDatasource, err.Error())
|
return nil, fmt.Errorf("%w: API请求超时: %s", ErrDatasource, err.Error())
|
||||||
}
|
}
|
||||||
|
if a.logger != nil {
|
||||||
|
a.logger.LogError(requestID, transactionID, path, errors.Join(ErrSystem, err), params)
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -87,9 +121,18 @@ func (a *AlicloudService) CallAPI(path string, params map[string]interface{}) (r
|
|||||||
// 读取响应体
|
// 读取响应体
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if a.logger != nil {
|
||||||
|
a.logger.LogError(requestID, transactionID, path, errors.Join(ErrSystem, err), params)
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 记录响应日志(不记录具体响应数据)
|
||||||
|
if a.logger != nil {
|
||||||
|
duration := time.Since(startTime)
|
||||||
|
a.logger.LogResponse(requestID, transactionID, path, resp.StatusCode, duration)
|
||||||
|
}
|
||||||
|
|
||||||
// 直接返回原始响应body,让调用方自己处理
|
// 直接返回原始响应body,让调用方自己处理
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user